zoukankan      html  css  js  c++  java
  • ztree 数组和树结构互转算法

    //树转化为数组
    transformToArrayFormat: function (setting, nodes) { if (!nodes) return []; var childKey = setting.data.key.children, r = []; if (tools.isArray(nodes)) { for (var i=0, l=nodes.length; i<l; i++) { r.push(nodes[i]); if (nodes[i][childKey]) r = r.concat(data.transformToArrayFormat(setting, nodes[i][childKey])); } } else { r.push(nodes); if (nodes[childKey]) r = r.concat(data.transformToArrayFormat(setting, nodes[childKey])); } return r; },
    //数组转化为树 transformTozTreeFormat:
    function(setting, sNodes) { var i,l, key = setting.data.simpleData.idKey, parentKey = setting.data.simpleData.pIdKey, childKey = setting.data.key.children; if (!key || key=="" || !sNodes) return []; if (tools.isArray(sNodes)) { var r = []; var tmpMap = []; for (i=0, l=sNodes.length; i<l; i++) { tmpMap[sNodes[i][key]] = sNodes[i]; } for (i=0, l=sNodes.length; i<l; i++) { if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) { if (!tmpMap[sNodes[i][parentKey]][childKey]) tmpMap[sNodes[i][parentKey]][childKey] = []; tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]); } else { r.push(sNodes[i]); } } return r; }else { return [sNodes]; } }
  • 相关阅读:
    人月神话
    Rails 最佳实践
    萧伯纳名言名句大全
    听话,照做,执行,别发挥
    So Good They Can't Ignore You
    谈谈遵守公司作战纪律
    如何让自己有动力去长久地做一件事情
    新架构优化问题总结
    Markdown 入门
    关于代码版本管理的思考和建议
  • 原文地址:https://www.cnblogs.com/yscec/p/11826194.html
Copyright © 2011-2022 走看看