zyc_tree v1.0.2
排序函数
function objSortBykey (objArr, key) {
let result = objArr.slice(0);
return result.sort((a, b) => akey - bkey);
}
初始化构造函数
export function TREEOBJ (pidKey, childrenKey, label, topPid) {
// 对象主体
this.pidKey = pidKey; // 所属父节点id key
this.topPid = topPid; // 顶级父节点id
this.childrenKey = childrenKey; // 子节点children
this.label = label; // 节点labe 的key值
// data 传进的树数据列表 pidKey 所属父级id对应的key值
this.create = function (data, pidKey = this.pidKey, pid) {
pid = pid || this.topPid; // 0为最高父级节点父id
let list = [];
data.map(item => {
if (itempidKey == pid) {
itemchildrenKey = this.create(data, pidKey, item.id);
list.push(item);
}
});
return list;
};
将树型结构数据处理成二位数组返回
this.getTreeDataList = function (data) {
const res = [];
function getData (data) {
data.forEach(v => {
res.push(v);
if (vchildrenKey) {
getData(vchildrenKey);
}
});
}
getData(data);
return res;
};
获取树节点的数量
this.getTreeNodeCount = function (data) {
return this.getTreeDataList(data).length || null;
};
根据对树节点的子节点根据key值进行排序
this.sortTreeNodeChildrenByKey = function (data, id, key) {
this.getTreeItem(data, id, item => {
if (item) {
itemchildrenKey = objSortBykey(itemchildrenKey, key);
}
});
return data;
};
id获取节点递归函数
this.getTreeItemByKey = function (data, key, val, callBack) {
data.map(item => {
if (itemkey == val) {
callBack && (typeof callBack == 'function') && callBack(item);
} else {
if (itemchildrenKey) {
this.getTreeItemByKey(itemchildrenKey, key, val, callBack);
}
}
});
};
根绝树节点id获取树节点的值
this.getTreeNodeById = function (data, id) {
let result = null; // 运行结果
this.getTreeItem(data, id, item => {
result = item;
});
return result;
};
设置修改树中的key对象值
this.setTreeNodeValue = function (data, id, key, value) {
this.getTreeItem(data, id, item => {
itemkey = value;
});
};
根据id获取对应树中的key值
this.getTreeNodeValueByKey = function (data, id, key) {
let value = null;
this.getTreeItem(data, id, item => {
value = itemkey;
});
return value;
};
根据id删除对应树中的key值
this.delTreeNodeKey = function (data, id, key) {
let value = null;
this.getTreeItem(data, id, item => {
value = itemkey;
if (key in item) {
delete itemkey;
}
});
return value;
};
往树里面追加子节点
this.appendChildrenNode = function (data, id, node = {}) {
this.getTreeItem(data, id, item => {
if (item) {
if (itemchildrenKey) {
itemchildrenKey.push(node);
} else {
itemchildrenKey = [];
itemchildrenKey.push(node);
}
}
});
return data;
};
根据id 清空子节点
this.clearChildrenNode = function (data, id) {
this.getTreeItem(data, id, item => {
if (item) {
itemchildrenKey = [];
}
});
};
通过子节点id删除子节点
this.delChildrenNodeById = function (data, id, childId) {
this.getTreeItem(data, id, item => {
if (item) {
(itemchildrenKey || []).forEach((v, idx) => {
if (v.id == childId) {
itemchildrenKey.splice(idx, 1);
}
});
}
});
};
获取父级节点
this.getParentNode = function (data, id) {
let currentNode = null;
let parentNode = null;
this.getTreeItem(data, id, item => {
if (item) {
currentNode = item;
}
});
if (currentNode) {
this.getTreeItem(data, currentNodethis.pidKey, item => {
if (item) {
parentNode = item;
}
});
}
return parentNode;
};
获取父级及祖先节点
this.getParentNodes = function (data, id) {
let parentNodes = this.getLevelNode(data, id);
return parentNodes;
};
获取子节点及子孙所有节点
this.getChildrenNodes = function (data, id) {
let res = [];
let childrens = [];
this.getTreeItem(data, id, item => {
if (item) {
childrens = itemchildrenKey;
}
});
function getChs (data) {
for (let i = 0, len = data.length; i < len; i++) {
res.push(datai);
if (datai && datai.length > 0) {
getChs(datai);
}
}
}
getChs(childrens);
return res;
};
获取子节点及子节点
this.getChildrenNode = function (data, id) {
let childrens = [];
this.getTreeItem(data, id, item => {
if (item) {
childrens = itemchildrenKey;
}
});
return childrens;
};
辅组查询层级递归遍历函数
this.getTreeLevelNode = function (data, id, topPid = this.topPid, result, pidKey = this.pidKey) {
let currentId = id;
let node = this.getTreeNodeById(data, currentId);
result.push(node);
if (node && nodepidKey && nodepidKey !== topPid) {
this.getTreeLevelNode(data, nodepidKey, topPid, result, pidKey);
}
};
根据id获取当前树节点的层级节点数据(倒序), topPid 最顶级父节点id
this.getLevelNode = function (data, id, topPid = this.topPid, pidKey = this.pidKey) {
let result = [];
this.getTreeLevelNode(data, id, topPid, result, pidKey);
return result || [];
};
获取当前节点层级
this.getNodeLevel = function (data, id) {
const res = this.getLevelNode(data, id);
return res.length || 0;
};
}
module.exports = { TREEOBJ };