zoukankan      html  css  js  c++  java
  • JavaScript list转tree

     js list转tree

    //------------------------------------List Convert to Tree ----------------------------------------------------//
    /**
     * 将list装换成tree  封装到JqueryUtils 
     * @param {Object } id    节点主ID
     * @param {Object } pId   父节点ID 
     * @param {Object } list  list数据
     * @return {Node} - 自定义Node树节点 
     * @author Jason - jasonandy@hotmail.com
     */
    function listToTree(id,pId,list){
        
        convertToNodeList(list);
        /**
         * @param {} list        list
         * @param {} pId        父节点
         * @return {Boolean}    是否存在父节点  
         * @description now.id = other.pid   other 的父节点为 noew
         */
        function exists(list, pId){
            for(var i=0; i<list.length; i++){
                if (list[i][id] == pId){
                    return true;
                }
            }
            return false;
        }
        
        /**
         * 树节点数据  最终数据结构
         * @type  Node treeNode 
         */
        var nodes = [];
       
     
        /**
         * 将所有的节点数据装入List 
         * @type Number
         */
        for(var i=0; i<list.length; i++){
             var row = list[i];
             if (!exists(list, row[pId])){//now.pid != new.id
                  nodes.push(row);//new ids
             }
        }
        
        /**
         * 父节点
         * @type Node 
         */
        var pNodes = [];
       
        /**
         * 将所有的子节点数据装入 pNodes 
         * @type pNodes 
         */
        for(var i=0; i<nodes.length; i++){
            pNodes.push(nodes[i]);
        }
        
        
        /**
         * 循环处理所有节点数据并进行封装 
         */
        while(pNodes.length){
            
            /**
             *  把数组的第一个元素从其中删除,并返回第一个元素的值
             *  the parent node
             */
            var node = pNodes.shift();
            
            /**
             * 
             * get the children nodes
             */
            for(var i=0; i<list.length; i++){
                var row = list[i];
                /**
                 * 取出list所有数据 比较处理
                 * 
                 * 这里可以定制化 Node 节点数据结构
                 * {  
                 *   "isActive": true, 
                 *   "isRoot": true, 
                 *   "title": "标题1",
                 *   "items": [
                 *       {
                 *           "text": "内容1"
                 *       }, 
                 *       {
                 *           "text": "内容2"
                 *       }
                 *   ]
                 * }
                 */
                if (row[pId] == node[id]){
                    /**
                     * 如果有items 直接放入
                     */
                    if (node.items){
                           node.items.push(row);
                      } else {
                           node.items = [row];
                      }
                      pNodes.push(row);
                 }
            }
       }
       return nodes;
    }
    
    /**
     * Node 节点数据封装
     * @param {} list  转为map结构数据
     * @return {}      list 需要转换的list
     */
    function convertToNodeList(list){
        $.each(list,function(i,e){
            e['title'] = e.NAME;
            e['isRoot'] = (e.PARENT_ID == '-1');
            e['isActive'] = (e.ENABLE_STATUS == '01');
        })    
        return list;
    }
    //------------------------------------List Convert to Tree End----------------------------------------------------//
      
    -------------------------------------- 勿忘初心 方得始终 --------------------------------------
  • 相关阅读:
    第十二章学习笔记
    UVa OJ 107 The Cat in the Hat (戴帽子的猫)
    UVa OJ 123 Searching Quickly (快速查找)
    UVa OJ 119 Greedy Gift Givers (贪婪的送礼者)
    UVa OJ 113 Power of Cryptography (密文的乘方)
    UVa OJ 112 Tree Summing (树的求和)
    UVa OJ 641 Do the Untwist (解密工作)
    UVa OJ 105 The Skyline Problem (地平线问题)
    UVa OJ 100 The 3n + 1 problem (3n + 1问题)
    UVa OJ 121 Pipe Fitters (装管子)
  • 原文地址:https://www.cnblogs.com/jasonandy/p/9156025.html
Copyright © 2011-2022 走看看