|
@@ -84,17 +84,19 @@ export default {
|
|
|
optionData() {
|
|
|
//第一个节点为根节点
|
|
|
if (this.options.length > 0) {
|
|
|
- var rootId = this.options[0].id;
|
|
|
+ //var rootId = this.options[0].id;
|
|
|
|
|
|
let cloneData = JSON.parse(JSON.stringify(this.options)); // 对源数据深度克隆
|
|
|
- return cloneData.filter(father => {
|
|
|
- // 循环所有项,并添加children属性
|
|
|
- let branchArr = cloneData.filter(
|
|
|
- child => father.id == child.parentId
|
|
|
- ); // 返回每一项的子级数组
|
|
|
- branchArr.length > 0 ? (father.children = branchArr) : ""; //给父级添加一个children属性,并赋值
|
|
|
- return father.id == rootId || father.parentId == null; //返回第一层
|
|
|
- });
|
|
|
+
|
|
|
+ // return cloneData.filter(father => {
|
|
|
+ // // 循环所有项,并添加children属性
|
|
|
+ // let branchArr = cloneData.filter(
|
|
|
+ // child => father.id == child.parentId
|
|
|
+ // ); // 返回每一项的子级数组
|
|
|
+ // branchArr.length > 0 ? (father.children = branchArr) : ""; //给父级添加一个children属性,并赋值
|
|
|
+ // return father.id == rootId || father.parentId == null; //返回第一层
|
|
|
+ // });
|
|
|
+ return this.listToTree(cloneData);
|
|
|
} else {
|
|
|
return [];
|
|
|
}
|
|
@@ -112,6 +114,30 @@ export default {
|
|
|
this.initHandle();
|
|
|
},
|
|
|
methods: {
|
|
|
+ listToTree(list) { // 将普通列表转换为树结构的列表
|
|
|
+ if (!list || !list.length) {
|
|
|
+ return []
|
|
|
+ }
|
|
|
+
|
|
|
+ let treeListMap = {};
|
|
|
+
|
|
|
+ for (let item of list) {
|
|
|
+ treeListMap[item.id] = item
|
|
|
+ }
|
|
|
+
|
|
|
+ for (let i = 0; i < list.length; i++) {
|
|
|
+ if (list[i].parentId && treeListMap[list[i].parentId]) {
|
|
|
+ if (!treeListMap[list[i].parentId].children) {
|
|
|
+ treeListMap[list[i].parentId].children = []
|
|
|
+ }
|
|
|
+ treeListMap[list[i].parentId].children.push(list[i]);
|
|
|
+ list.splice(i, 1);
|
|
|
+ i--
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+ },
|
|
|
// 初始化值
|
|
|
initHandle() {
|
|
|
console.log("options.length=" + this.options.length);
|