博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
扁平数组构建DOM树
阅读量:6383 次
发布时间:2019-06-23

本文共 5402 字,大约阅读时间需要 18 分钟。

interface IOrganizationNode {        id: string;        code: string;        name: string;        localName: string;        localNameLocale: string;        parentCode: string;        description: string;        children?: IOrganizationNode[];    }    interface IOrganizationTree {        organizationTree: IOrganizationNode[];    }interface IOrganization {    id: string;    code: string;    name: string;    localName: string;    localNameLocale: string;    parentCode: string;    description: string;}    export class OrganizationTree {        public static GenerateOrganizationsDom: string = "GenerateOrganizationsDom" + _gcEditingInput;        public static createTreeDom(array: IOrganization[]): HTMLElement {            let DOMObject: HTMLElement = document.createElement("div");            DOMObject.className = "select-user-tree-container";            let organizationTree: IOrganizationTree = OrganizationTree.CreateTreeData(array);            DOMObject.appendChild(OrganizationTree.CreateTreeDom(organizationTree.organizationTree, true));            this._bindEvent(DOMObject);            return DOMObject;        }        private static CreateTreeData(array: IOrganization[]): IOrganizationTree {            let r: IOrganizationNode[] = [];            let codeToOrganizationMap: Dictionary
= {}; let len = array.length; let orgs: IOrganization[] = []; //deep copy for (let i = 0; i < len; i++) { let org: any = {}; for (var attr in array[i]) { org[attr] = array[i][attr]; } orgs.push(org); } for (let i = 0; i < len; i++) { codeToOrganizationMap[orgs[i].code] = orgs[i]; } for (let j = 0; j < len; j++) { let org: IOrganizationNode = orgs[j]; let parentOrg: IOrganizationNode = codeToOrganizationMap[org.parentCode];

if (parentOrg) {

if (parentOrg.children) {
parentOrg.children.push(org);
} else {
parentOrg.children = [];
parentOrg.children.push(org);
}
} else {
r.push(org);
}

}            let result: any = {};            result.organizationTree = r;            return result;        }        private static CreateTreeDom(treeNodes: IOrganizationNode[], top?: boolean): HTMLElement {            let ul = document.createElement("ul") as HTMLUListElement;            if (top) {                ul.classList.add("tree");                ul.classList.add("tree-root");                //ul.classList.add("tree", "tree-root");//chrome unsupport            } else {                ul.classList.add("tree");            }            for (let i = 0; i < treeNodes.length; i++) {                let li = document.createElement("li") as HTMLLIElement;                li.classList.add("tree-node");                let a = document.createElement("a") as HTMLAnchorElement;                a.classList.add("tree-node-label");                let spanName = document.createElement("span") as HTMLSpanElement;                spanName.classList.add("tree-node-name");                spanName.innerText = treeNodes[i].name;                spanName["GCSK_OrganizationCode"] = treeNodes[i].code;                let spanArrow = document.createElement("span") as HTMLSpanElement;                spanArrow.classList.add("tree-node-arrow");                a.appendChild(spanName);                a.appendChild(spanArrow);                li.appendChild(a);                if (treeNodes[i].children) {                    li.classList.add("tree-node-haschildren");                    li.appendChild(OrganizationTree.CreateTreeDom(treeNodes[i].children));                }                ul.appendChild(li);            }            return ul;        }        private static _bindEvent(DOMObject: HTMLElement) {            let self = this;            GC$(DOMObject).bind("click", (event) => {                let srcElement = 
(event.srcElement || event.target); if (srcElement.classList.contains("tree-node-name")) { let liElement = srcElement.parentElement.parentElement; // first remove all
  • classname Array.prototype.forEach.call(DOMObject.querySelectorAll(".tree-node"), item => { item.classList.remove("tree-node-selected"); }); //then add current
  • if (liElement.classList.contains("tree-node-haschildren")) { liElement.classList.add("tree-node-opened"); } liElement.classList.add("tree-node-selected"); var evt = document.createEvent("UIEvent") as UIEvent; evt.initEvent(OrganizationTree.GenerateOrganizationsDom, true, false); evt["GCSK_OrganizationCode"] = srcElement["GCSK_OrganizationCode"]; liElement.dispatchEvent(evt); } else if (srcElement.classList.contains("tree-node-arrow")) { let liElement = srcElement.parentElement.parentElement; if (srcElement.parentElement.parentElement.classList.contains("tree-node-opened")) { liElement.classList.remove("tree-node-opened"); } else { liElement.classList.add("tree-node-opened"); } } }); } }
  •  

    转载地址:http://nzzha.baihongyu.com/

    你可能感兴趣的文章
    Linux系统诊断小技巧(14):启停问题之如何修复initrd损坏
    查看>>
    Python数据科学分析速查表
    查看>>
    jmeter测试教程
    查看>>
    Trie 树内存消耗问题
    查看>>
    区块链教程btcpool矿池源码分析slparser
    查看>>
    OC 中,覆盖属性会有怎么样的化学反应?
    查看>>
    Linux MySQL 8.0 忘记密码
    查看>>
    Android:随笔——我们用什么来替代 Enum 这个内存大户
    查看>>
    TCP连接的状态详解以及故障排查
    查看>>
    RFC2616-HTTP1.1-Methods(方法规定部分—译文)
    查看>>
    集成学习之Boosting —— Gradient Boosting原理
    查看>>
    Setting up a Multi-Cloud CI/CD Chain using Alibaba Tools
    查看>>
    Sql Server之旅——第二站 理解万恶的表扫描
    查看>>
    2014年小结——没有最好,只有更好
    查看>>
    win8 开发之旅(15) --足球游戏揭秘(1)
    查看>>
    AMD加载器实现笔记(一)
    查看>>
    Cocos2D iOS之旅:如何写一个敲地鼠游戏(五):设置背景
    查看>>
    [python学习] 简单爬取维基百科程序语言消息盒
    查看>>
    窥探Swift之基本数据类型
    查看>>
    【J2EE十三个规范】业务层之EJB
    查看>>