ElmentUI el-cascader数据懒加载实现及数据源切换时动态更新及选中态不消失及回选

1.el-cascader数据懒加载实现

<el-cascader
  ref="cascader"
  v-model="selctedCasArea"
  @change="handleSelectArea" //选中数据处理用
  @expand-change="expandChange" //数据异步加载时选中态消失处理
  :props="areaCasProps"
  placeholder="地区选择"
  clearable>
</el-cascader>

data() {
  return {
    areaCasProps: {
    multiple: true,
    checkStrictly: true,
    lazy: true,
    lazyLoad: _this.lazyLoad
  },
  selctedCasArea: []
}

methods: {
  lazyLoad (node, resolve) {
    const { level } = node
    if (level === 0) {
      this.getProvince(node, resolve)
    } else {
      this.provinces(node, resolve)
    }
  }
}

2.数据源切换时动态更新

methods: {
  switch (para) {
    if (para === this.curFlag) return
    this.curFlag = para
    if (this.curFlag === 2) {
      this.$refs.cascader.panel.lazyLoad()
    } else {
      this.$refs.cascader.panel.lazyLoad()
    }
  }
}

3.el-cascader数据懒加载下选中数据不消失

expandChange (val) {
  let curSelctedCasArea = this.selctedCasArea
  curSelctedCasArea.push(val)
  this.selctedCasArea = curSelctedCasArea
}

3.el-cascader数据懒加载下数据回选

getProvince (node, resolve) {
  HomeApi.getProvince()
    .then((res) => {
      if (res.code === 200) {
        let arr = res.data.map((item) => {
          return {
            ...item,
            label: item.name,
            value: item.code,
            leaf: false
          }
        })
        resolve(arr)
      }
    })
    .finally(() => {
      this.selctedCasArea = [['110000']]
    })
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容