//如果children为空数组,则children为 undefined getTree() { this.$get('/distribution/pc/org/getTree/' + 0 + '/' + 0, {}).then((res) => { if (res.code == 1000) { this.options = this.getTreeData(res.data); //获取树结构值 res.data =[{ label: '一级 1', children: [{ label: '二级 1-1', children: [] }] }, { label: '一级 2', children: [{ label: '二级 2-1', children: [{ label: '三级 2-1-1' }] }, { label: '二级 2-2', children: [{ label: '三级 2-2-1' }] }] }, { label: '一级 3', children: [] }] this.getTreeData(res.data); //获取树结构值 } }) }, getTreeData(data) { // 循环遍历json数据 for (var i = 0; i < data.length; i++) { //if(data[i].level == 1){ //可根据数据返回值-添加需要参数-此条件禁止第一列选中 //data[i].disabled = true //} if (data[i].children.length < 1) { // children若为空数组,则将children设为undefined data[i].children = undefined; } else { // children若不为空数组,则继续 递归调用 本方法 this.getTreeData(data[i].children); } } return data; },
props: 返回参数不匹配的话,修改值比如children: 'child', <el-cascader :options="options" v-model="rankData.areaIdAll" :props="{value: 'id', label: 'name', children: 'child', multiple: true, checkStrictly: true }" collapse-tags style="width: 400px;" @change="change2(rankData.areaIdAll)"></el-cascader> //获取选择后的值 change2(idList) { console.log(idList,'236') if (idList.length == 0) { this.areaId = []; return false; } var newArr1 = []; // 去重后的 var newArr2 = []; // 重复的 var newArr3 = []; // 只出现一次的 if (idList.length > 1) { for (var i = 0; i < idList.length; i++) { for (var j = 0; j < idList[i].length; j++) { var v = idList[i][j]; // 判断是否存在数组中,不存在在往下走 if (!newArr1.includes(v)) { newArr1.push(v); } else { newArr2.push(v); } } } for (var i = 0; i < newArr1.length; i++) { var v = newArr1[i]; if (!newArr2.includes(v)) { newArr3.push(v); } } } else { for (var i = 0; i < idList.length; i++) { for (var j = 0; j < idList[i].length; j++) { var v = idList[i][j]; // 判断是否存在数组中,不存在在往下走 if (!newArr1.includes(v)) { newArr1.push(v); } else { newArr2.push(v); } } } for (var i = 0; i < newArr1.length; i++) { if (newArr3.indexOf(newArr1[newArr1.length - 1]) === -1) { newArr3.push(newArr1[newArr1.length - 1]) } } } this.areaId = newArr3; },
外图:禁止选择省市区