sailei1 / blog

1 stars 0 forks source link

el-cascader 赋值 #70

Closed sailei1 closed 5 years ago

sailei1 commented 5 years ago

//template <el-cascader style="width: 85%" v-model="select" :options="options" :props="{checkStrictly: true }" @change="selectDep" filterable clearable>

// method

 setCascaderSelect(key,treeData,keyName) {
                let arr = []; // 在递归时操作的数组
                let returnArr = []; // 存放结果的数组
                let depth = 0; // 定义全局层级

                function childrenEach(childrenData, depthN) {
                    for (var j = 0; j < childrenData.length; j++) {
                        depth = depthN; // 将执行的层级赋值 到 全局层级
                        arr[depthN] = (childrenData[j][keyName]);

                        if (childrenData[j][keyName] == key) {
                            returnArr = arr.slice(0, depthN + 1); //将目前匹配的数组,截断并保存到结果数组,
                            break
                        } else {
                            if (childrenData[j].children) {
                                depth++;
                                childrenEach(childrenData[j].children, depth);
                            }
                        }
                    }
                    return returnArr;
                }

                return childrenEach(treeData, depth);
            },
//调用
this.select=this.setCascaderSelect(item.deptId,this.options,'value');
this.select_staff=this.setCascaderSelect(item.userId,this.staffs,'userid');