这次有个功能 ant的tree 展开 点击子节点 新增节点之后 数据能够照常展开

有几种方法

我能想到的 因为ant 有个expanded 只要设置为true就能展开了,但是这边有个陷阱,就是仅仅设置子节点的expanded为true的时候 它父节点是不展开的,也需要它父节点额expanded为true

它还提供了一个api 就有个defaultExpandedKeys = ['0-0', '0-0-0', '0-0-1']; 这个defaultExpandedKeys 数组里面的值就算它父节点的key集合们

第一种方法 将data 每个都要添加pathKeys

var spect = [{
"key": 22456260137536,
"id": "root",
pathKeys: [],
"type": "org",
"title": "根组织",
"parentSid": 0,
"children": [{
"key": 22456302834240,
pathKeys: [],
"id": "child_1",
"type": "org",
"title": "部门",
"parentSid": 22456260137536,
"children": [{
"key": 120250157011520,
pathKeys: [],
"id": "147460064539200",
"type": "org",
"title": "test-001",
"parentSid": 22456302834240
}, {
"key": 120619034903104,
pathKeys: [],
"id": "147828945097280",
"type": "org",
"title": "test000",
"parentSid": 22456302834240
}, {
"key": 120619165348416,
pathKeys: [],
"id": "147829075550784",
"type": "org",
"title": "2222",
"parentSid": 22456302834240
}, {
"key": 120626434175552,
pathKeys: [],
"id": "147836344422976",
"type": "org",
"title": "333",
"parentSid": 22456302834240
}, {
"key": 121592991220288,
pathKeys: [],
"id": "148802957083200",
"type": "org",
"title": "qiqiqi23333",
"parentSid": 22456302834240
}]
}, {
"key": 121665603314240,
pathKeys: [],
"id": "148875569611328",
"type": "org",
"title": "333",
"parentSid": 22456260137536
}, {
"key": 121665737142848,
pathKeys: [],
"id": "148875703456320",
"type": "org",
"title": "233333",
"parentSid": 22456260137536,
"children": [{
"key": 121674823979584,
pathKeys: [],
"id": "148884790379072",
"type": "org",
"title": "122",
"parentSid": 121665737142848
}, {
"key": 121675403362880,
pathKeys: [],
"id": "148885369774656",
"type": "org",
"title": "333",
"parentSid": 121665737142848
}, {
"key": 121675935605312,
"id": "148885902012992",
"type": "org",
"title": "2222",
"parentSid": 121665737142848
}]
}, {
"key": 57249373893184,
"id": "aa",
"type": "role",
"title": "aaa",
"parentSid": 22456260137536
}]
}, {
"key": 38166237606464,
pathKeys: [],
"id": "test",
"type": "org",
"title": "test",
"parentSid": 0,
"children": [{
"key": 40615561060928,
pathKeys: [],
"id": "testchil",
"type": "org",
"title": "testchil",
"parentSid": 38166237606464
}]
}, {
"key": 40619971723840,
pathKeys: [],
"id": "test1",
"type": "org",
"title": "test11",
"parentSid": 0,
"children": [{
"key": 40621094822464,
pathKeys: [],
"id": "test1chil",
"type": "org",
"title": "test1chil",
"parentSid": 40619971723840
}]
}, {
"key": 114144172368448,
pathKeys: [],
"id": "sftest",
"type": "org",
"title": "sftest",
"parentSid": 0
}, {
"key": 114581250646592,
pathKeys: [],
"id": "sftest1",
"type": "org",
"title": "sftest1",
"parentSid": 0
}, {
"key": 114596497031744,
pathKeys: [],
"id": "sftest2",
"type": "org",
"title": "sftest2",
"parentSid": 0
}, {
"key": 116697090073152,
pathKeys: [],
"id": "sftest3",
"type": "org",
"title": "sftest3",
"parentSid": 0,
"children": [{
"key": 119797568778816,
pathKeys: [],
"id": "147006679814720",
"type": "org",
"title": "sftest4",
"parentSid": 116697090073152
}]
}, {
"key": 120238754472512,
pathKeys: [],
"id": "147448661918272",
"type": "org",
"title": "我测试的数据源",
"parentSid": 0
}, {
"key": 120240105521728,
pathKeys: [],
"id": "147450012979776",
"type": "org",
"title": "test-001",
"parentSid": 0
}];

 getPathKeys(data) {
    if (!Array.isArray(data)) {
        return data;
      }
      const run = (arr, pathKeys) => {
        if (!Array.isArray(arr)) {
          return;
        }
        return arr.map(item => {
          if (Array.isArray(item.children)) {
            item = {
              ...item,
              children: run(item.children, [...pathKeys, item.key]),
            };
          }
          return {
            ...item,
            pathKeys,
          };
        });
      };
      return run(data, []);
  }
这样返回的数据就是每个children的pathKeys 都会有自己和父节点的key的集合,只要将这个集合赋值defaultExpandedKeys,就可以正常显示了
 
方法二
function findChild(arr, id) {
    for(let i = 0; i < arr.length; i++) {
     const item = arr[i];
if (item.id === id) {
item.expanded= true;
return item;
}
if (item.children) {
const result = findChild(item.children, id);
  if (result) {
console.log(item);
item.expanded = true;
return result;
}
}
    }

}

findChild(spect, '147836344422976'); 很明显你点击展开的时候 将当前的key传进去 将数据也放进去 返回的就是当前的节点展开 都是true了

 
 
这俩方法都是我的枪手给我写的。。。。我。。。。

ant tree 展开key的集合的更多相关文章

  1. Jquery EasyUI Combotree和 EasyUI tree展开所有父节点和获取完整路径

    Jquery EasyUI Combotree展开所有父节点 Jquery EasyUI Combotree获取树完整路径 Jquery EasyUI tree展开所有父节点 Jquery EasyU ...

  2. vue实现两重列表集合,点击显示,点击隐藏的折叠效果,(默认显示集合最新一条数据,点击展开,显示集合所有数据)

    效果图: 默认显示最新一条数据: 点击显示所有数据: 代码: 说明:这里主要是 这块用来控制显示或者隐藏 根据当前点击的  这个方法里传递的index 对应  isShow 数组里的index  ,对 ...

  3. PHP 如何获取二维数组中某个key的集合(高性能查找)

    分享下PHP 获取二维数组中某个key的集合的方法. 具体是这样的,如下一个二维数组,是从库中读取出来的. 代码: $user = array( 0 => array( 'id' => 1 ...

  4. EasyUI的tree展开所有的节点或者根据特殊的条件控制展示指定的节点

    展示tree下的所有节点$(function(){ $('#t_funinfo_tree').tree({ checkbox: true, url:"<%=basePath %> ...

  5. 后台返回平铺数据,如何转换成树形json并渲染树形结构,ant tree 异步加载

    如何后台返回对象数组(平铺式) 1.根据字段标识(板块)获取根节点 ### initTreeData(dataOrg){ var resultArr=dataOrg[0] var secArr=[]; ...

  6. Flex tree展开节点问题!

    问题: 使用 for each(var item:XML in menuTree.dataProvider) {     menuTree.expandChildrenOf(item,true);   ...

  7. PHP 获取二维数组中某个key的集合

    本文为代码分享,也是在工作中看到一些“大牛”的代码,做做分享. 具体是这样的,如下一个二维数组,是从库中读取出来的. 代码清单: $user = array( 0 => array( 'id'  ...

  8. ext tree展开时的一些技巧

    加入子节点的时候.我们须要展开父节点.并选中刚加入好的节点. 这时候会有一个问题. 我用的ext-js-4.2起码有一种问题. 节点内部会混乱.要么多加一个. 要么层级会发生故障. 随后我发现一个窍门 ...

  9. PHP根据array_map、array_reduce和array_column获取二维数组中某个key的集合

    http://camnpr.com/php-python/1715.html 巧用array_map()和array_reduce()替代foreach循环

随机推荐

  1. Android activity 亮度调整

    注意点 screenBrightness 取值范围0-1 不是0-255一定要注意 scanForActivity(context) 是根据上下文获取所在的activity如果直接在activity ...

  2. 聊聊HTTPS和SSL/TLS协议 【基础入门】

    要说清楚 HTTPS 协议的实现原理,至少需要如下几个背景知识.1. 大致了解几个基本术语(HTTPS.SSL.TLS)的含义2. 大致了解 HTTP 和 TCP 的关系(尤其是“短连接”VS“长连接 ...

  3. Struts2加载自定义库注意事项

    新建Struts2项目,添加Struts2的jar包时,往往通过导入自定义库的方式,导入自定义库时,有个地方必须要设置,否则项目无法正常执行,如图所示: 必须要按照上述方式对自定义库进行加载!

  4. php的header方法

    http://www.cnblogs.com/fengzheng126/archive/2012/04/21/2461475.html

  5. Point Estimate|unbiased estimator|Confidence-Interval Estimate

    8.1 Estimating a Population Mean Point Estimate estimate  a single number, or point. 因为:the mean of ...

  6. [LC] 242. Valid Anagram

    Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...

  7. linux上python3的安装

    我这里使用的时centos7-mini,centos系统本身默认安装有python2.x,版本x根据不同版本系统有所不同,可通过 python --V 或 python --version 查看系统自 ...

  8. Mybatis/ibatis基础知识

    Tip:mapper.xml中sql语句不允许出现分号! 1.#和$符号的区别 #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是 ...

  9. Java并发编程入门与高并发面试(三):线程安全性-原子性-CAS(CAS的ABA问题)

    摘要:本文介绍线程的安全性,原子性,java.lang.Number包下的类与CAS操作,synchronized锁,和原子性操作各方法间的对比. 线程安全性 线程安全? 线程安全性? 原子性 Ato ...

  10. Dockerfile创建zabbix监控体系

    使用for循环将zabbix的镜像导入到容器中 for n in `ls *.tar.gz`;do docker load -i $n ;done 使用docker运行zabbix-server do ...