element ui cascader级联选择器 动态加载以及回显

在用实际开发时使用element ui级联选择器遇到通过点击一级的下拉选项去动态获取二级下拉选项.以此记录:

使用:

<el-cascader :props="props" v-if="showSelect" placeholder="请选择" v-model="doctorValue" style="width:90%;" />

定义变量以及设置:

复制代码
data() {
      return {
        doctorValue:[]
        props: {
          //checkStrictly: true,// 父子节点是否互相关联,其余配置见官网
          lazy: true,
          lazyLoad: this.lazyLoad
        }
      };
    },
复制代码

点击动态加载:

复制代码
lazyLoad(node, resolve) {
        setTimeout(() => {
          this.getHospital(node, resolve)
        }, 50)
      },
      getHospital(node, resolve) {
        // 第一级
     //
this.hospitalData 是提前接口获取的一级下拉框
        if (node.level == 0) {
          const nodess =this.hospitalData.map((i, index) => ({
            value: i.id,
            label: i.hospitalName,
          }))
          resolve(nodess);
        }
        // 第二级
        if (node.level == 1) {
          let data = {
            hospitalId: node.data.value,
            sortType: "default"
          };
          getDoctorsByHospital(data).then(res => {
            // console.log(res)
            const nodes = res.data.map((item, index) => ({
              value: item.id,
              label: item.doctorName,
              leaf: node.level >= 1
         // 节点数,我的只有二级,设置之后点击二级不再出现加载圈
})); resolve(nodes); }) } },
复制代码

第二次进来以后,并不能正确回显,查询资料后,在会显示时,重新渲染级联选择器的方法最简单

this.doctorValue = [res.data.hospitalId, res.data.doctorId]
this.showSelect = false
setTimeout(() => {
  this.showSelect = true
}, 50)

 

posted @   月下云生  阅读(3523)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示