1.0.2 • Published 2 years ago
list-and-tree-utils v1.0.2
list-and-tree-utils
具有父子关系的列表数据和树形数据的处理工具。
本工具库没有进行完整细致的测试,请谨慎使用!!!
安装
npm i list-and-tree-utils
引入
import listAndTreeUtils from "list-and-tree-utils"
开始使用
测试数据:
const list = [
{
id: 1,
parentId: 0,
name: "一级 1",
},
{
id: 2,
parentId: 1,
name: "二级 1-1",
},
{
id: 3,
parentId: 2,
name: "三级 1-1-1",
},
{
id: 4,
parentId: 0,
name: "一级 2",
},
{
id: 5,
parentId: 4,
name: "二级 2-1",
},
{
id: 6,
parentId: 5,
name: "三级 2-1-1",
},
{
id: 7,
parentId: 4,
name: "二级 2-2",
},
{
id: 8,
parentId: 7,
name: "三级 2-2-1",
},
{
id: 9,
parentId: 0,
name: "一级 3",
},
{
id: 10,
parentId: 9,
name: "二级 3-1",
},
{
id: 11,
parentId: 10,
name: "三级 3-1-1",
},
{
id: 12,
parentId: 9,
name: "二级 3-2",
},
{
id: 13,
parentId: 12,
name: "三级 3-2-1",
},
{
id: 14,
parentId: 12,
name: "三级 3-2-2",
},
]
测试数据:
const tree = [
{
id: 1,
name: "一级 1",
children: [
{
id: 2,
name: "二级 1-1",
children: [
{
id: 3,
name: "三级 1-1-1"
},
],
},
],
},
{
id: 4,
name: "一级 2",
children: [
{
id: 5,
name: "二级 2-1",
children: [
{
id: 6,
name: "三级 2-1-1"
},
],
},
{
id: 7,
name: "二级 2-2",
children: [
{
id: 8,
name: "三级 2-2-1"
},
],
},
],
},
{
id: 9,
name: "一级 3",
children: [
{
id: 10,
name: "二级 3-1",
children: [
{
id: 11,
name: "三级 3-1-1"
},
],
},
{
id: 12,
name: "二级 3-2",
children: [
{
id: 13,
name: "三级 3-2-1"
},
{
id: 14,
name: "三级 3-2-2"
},
],
},
],
},
]
/**
* @description 组装树结构
*
* @param {Array} list 具有父子关系的列表数据
* @param {Object} options 配置项
*
* @param {String} options.idField id字段名,默认为{id}
* @param {String} options.parentIdField 父id字段名,默认为{parentId}
* @param {String} options.topNodeParentId 顶级节点的父id,默认为{0}
* @param {String} options.childrenField 子节点字段名,默认为{children}
* @param {Object} options.emptyChildrenValue 空子节点的值,默认为{undefined}
* @param {Function} options.eachNode 遍历每个节点的回调函数,参数为(node, children)
* @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
*/
listAndTreeUtils.list.toTree(list, options);
使用示例:
const eachNode = (node, children) => {
node['label'] = node.name;
node['isLeaf'] = children.length === 0;
}
const result = listAndTreeUtils.list.toTree(list, {
emptyChildrenValue: [],
eachNode,
})
console.log(result)
/**
* @description 拆解树结构
*
* @param {Array} tree 树结构数据
* @param {Object} options 配置项
*
* @param {String} options.idField id字段名,默认为{id}
* @param {String} options.parentIdField 父id字段名,默认为{parentId}
* @param {String} options.topNodeParentId 顶级节点的父id,默认为{0}
* @param {String} options.childrenField 子节点字段名,默认为{children}
* @param {Boolean} options.remainChildren 是否保留子节点,默认为{false}
* @param {Function} options.eachNode 遍历每个节点的回调函数,参数为(node, children)
* @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
*/
listAndTreeUtils.tree.toList(tree, options);
使用示例:
const eachNode = (node, children) => {
node['label'] = node.name;
node['isLeaf'] = children.length === 0;
}
const result = listAndTreeUtils.tree.toList(tree, {
emptyChildrenValue: [],
eachNode,
})
console.log(result)
/**
* @description 在列表中过滤目标节点,返回所有目标节点以及该节点的所有父节点
*
* @param {Function} filter 过滤函数
* @param {Array} list 具有父子关系的列表数据
* @param {Object} options 配置项
*
* @param {String} options.idField id字段名,默认为{id}
* @param {String} options.parentIdField 父id字段名,默认为{parentId}
* @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
*/
listAndTreeUtils.list.filter(filter, list, options);
使用示例:
const filter = item => item.name.indexOf("1-") > -1
const result = listAndTreeUtils.list.filter(filter, list)
console.log(result)
/**
* @description 获取目标节点的所有父节点
*
* @param {Number} targetNodeId 目标节点id
* @param {Array} list 具有父子关系的列表数据
* @param {Object} options 配置项
*
* @param {String} options.idField id字段名,默认为{id}
* @param {String} options.parentIdField 父id字段名,默认为{parentId}
* @param {Boolean} options.returnTargetNode 目标节点是否一起返回,默认为{true}
* @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
*/
listAndTreeUtils.list.getParents(targetNodeId, list, options)
使用示例:
const result = listAndTreeUtils.list.getParents(3, list)
console.log(result)
/**
* @description 获取目标节点的所有子节点
*
* @param {Number} targetNodeId 目标节点id
* @param {Array} list 具有父子关系的列表数据
* @param {Object} options 配置项
*
* @param {String} options.idField id字段名,默认为{id}
* @param {String} options.parentIdField 父id字段名,默认为{parentId}
* @param {Boolean} options.returnTargetNode 目标节点是否一起返回,默认为{true}
* @param {Boolean} options.useDeepCopy 是否对数据使用深拷贝,默认为{true}
*/
listAndTreeUtils.list.getChildren(targetNodeId, list, options)
使用示例:
const result = listAndTreeUtils.list.getChildren(1, list)
console.log(result)