zhangshichun / tree-lodash Goto Github PK
View Code? Open in Web Editor NEW轻量的“树操作”函数库 (Lightweight "tree operations" library)
Home Page: https://zhangshichun.github.io/tree-lodash/
轻量的“树操作”函数库 (Lightweight "tree operations" library)
Home Page: https://zhangshichun.github.io/tree-lodash/
如题 childrenKey: (node: any) => string
有时候需要给的数据是list,不是一个tree,可以考虑增加一个toTree的功能
在项目中安装tree-lodash后,发现缺少types.d.ts文件。导致
check(data, (t) => t.key === 'a') // true / false
第一级不符合,第二级有符合的,但是过滤不出来
我需要下面这3个功能:
export class Tree {
/**从当前元素开始(包含),向上冒泡,直到指定的祖先元素(不包含),每个元素都执行一次回调,如果回调返回true,则终止冒泡。*/
static bubbleCall(node: any, callback: (node: any) => boolean, parent: any): any {
if (node === parent || callback(node))
return node;
return Tree.bubbleCall(node.parentElement, callback, parent);
};
/**从当前元素开始(包含),向上冒泡,直到指定的祖先元素(不包含),找到第一个符合要求的元素,执行回调,并返回回调结果。*/
static onClosest<TNode extends {parentElement:TNode}>(currentNode: TNode, predict: (node: TNode) => boolean, callback: (node: TNode) => TNode, parent: TNode): TNode | undefined {
if (predict(currentNode))
return callback(currentNode);
if (currentNode !== parent && currentNode.parentElement)
return Tree.onClosest(currentNode.parentElement, predict, callback, parent);
};
//static toArray(){
//
//}
/** array 组装成树 */
static array2tree(array,idStr='id',parentStr='parent',rootStr=1){
let group = {};
array.forEach(node=>{
let parent = group[node[parentStr]] = group[node[parentStr]] || [,[]];
parent[1].push(node);
let self = group[node[idStr]] = group[node[idStr]] || [,[]];
node.children = self[1];
self[0] = node;
})
return group[rootStr][0];
}
}
大佬能不能加到库中?
const formatTree = window.treeLodash.map(treeData, (node, meta) => {
return {
id: node.dicId,
name: node.name,
code: node.code,
pcode: node.pcode,
dicCode: node.dicCode,
depth: meta.depth,
children: node.list,
};
}, {
childrenKey: 'list'
});
使用类似结构调用map方法,返回的新的树节点对象会同时包含list和children两个key,list中的子节点返回正常,但是children中的子节点缺少children字段 (children: node.list 未生效)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.