// treeData树的扁平化数据,selectArray,选中节点的id
let SelectHandel = function (treeData, selectArray) {
this.treeData = JSON.parse(JSON.stringify(treeData));
this.selectArray = JSON.parse(JSON.stringify(selectArray));
this.rootNode = []
this.updateChecked()
}
SelectHandel.prototype.updateChecked = function (tree, id) {
if (this.treeData) {
for (let i = 0; i < this.treeData.length; i++) {
const treeItem = this.treeData[i];
if (this.selectArray.some(resItem => resItem == treeItem.id)) {
treeItem['checked'] = true;
}
}
this.filterCheck()
}
}
SelectHandel.prototype.filterCheck = function () {
for (let i = 0; i < this.treeData.length; i++) {
const element = this.treeData[i];
if (!element.checked) {
this.pushRootNode(element.pid)
}
}
this.handelRootNode()
}
SelectHandel.prototype.pushRootNode = function (pid) {
for (let i = 0; i < this.treeData.length; i++) {
const element = this.treeData[i];
if (element.id == pid) {
if (!this.rootNode.some(item => item.id == element.id)) {
this.rootNode.push(element)
}
if (element.pid !== 0) {
this.pushRootNode(element.pid)
}
}
}
}
SelectHandel.prototype.handelRootNode = function () {
this.rootNode.forEach(eachItem => {
for (let i = 0; i < this.treeData.length; i++) {
const element = this.treeData[i];
if (eachItem.id == element.id) {
element['checked'] = false;
}
}
});
}
export default SelectHandel;