el-cascader 数据回显

最近做一个后台管理系统菜单权限控制,选择菜单用到了element-ui 的组件el-cascader,只保存了最后一级菜单的id,编辑回显的时候就显示出了问题,回显一直是错的,在网上搜索了很久没找到合适的,自己写了一个,记录一下。

<el-form-item label="上级菜单" prop="pid">
          <el-cascader ref="selectMenu" :options="selectTableData"
          :props="{ value: 'id', label: 'menu_name', children: 'children', checkStrictly: true, 	emitPath:false }" 
          clearable @change="onSelectPMenu" v-model="selectMenu"></el-cascader>
</el-form-item>
export default {
	data() {
	    return {
	    	selectMenu: [0],
	    	selectTableData: [],
	    	formData: {
				pid: 0,
			}
		}
	},
	methods: {
		// 回显菜单
	    getPidMenuList(pid) {
	      let cid_list = [];
	      this.selectTableData.forEach((item, index) => {
	        if (item.id == pid) {
	          cid_list = [item.id];
	          return false;
	        } else {
	          if (item.children) {
	            let newCid_list = [item.id];
	            let list = nodefun(item.children, pid, newCid_list);
	            if (list) {
	              cid_list = list;
	            }
	          }
	        }
	      });
	      // 递归函数
	      function nodefun(newVal, newId, newCid_list) {
	        let flag = false;
	        newVal.forEach((j) => {
	          if (j.id == newId) {
	            newCid_list.push(j.id);
	            flag = true;
	          } else {
	            if (j.children) {
	              let cid_list = JSON.parse(JSON.stringify(newCid_list));
	              cid_list.push(j.id);
	              let list = nodefun(j.children, newId, cid_list);
	              if (list) {
	                newCid_list = list;
	                flag = true;
	              }
	            }
	          }
	        });
	        if (flag) {
	          return newCid_list;
	        }
	      }
	      return cid_list;
	    },
	    onSelectPMenu(e) {
	      console.log(e)
	      this.formData.pid = e[0]
	      this.$refs.selectMenu.dropDownVisible = false
	    },
	    onEdit(row) {
			this.selectMenu = this.getPidMenuList(row.pid)
		}
	}
		```

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值