web前端之element级联选择器的那些事、获取label的值、getCheckedNodes、cascader、ref


动态加载时获取value和label的值

前言

1、使用ref的getCheckedNodes方法获取label的值,change事件只传递value值;
2、回显自动处理,无需手动干预,设置好接口即可。


html

html 复制代码
<el-cascader ref="refCascader" v-model="form.addressList" clearable :props="props" @change="changeCascader"></el-cascader>

JavaScript
data

javascript 复制代码
data() {
  return {
    form: {
      addressList: null
    },
    props: {
      // 设置为动态加载模式
      lazy: true,
      // 配置value字段
      value: 'id',
      // 配置label字段
      label: 'name',
      lazyLoad(node, resolve) {
        const { level, data: { id = '' } } = node;
        let mapList = (list) => list.map(item => {
          // 设置层级(表示到了第三级便不再显示展开图标,点击也不再有加载动画)
          item.leaf = level >= 2;

          return item;
        });

        getAddrTree({ parentId: id }).then(re => resolve(mapList(re.response)));
      }
    }
  }
}

methods

javascript 复制代码
changeCascader(event) {
  // 不明智的数据存储方式
  // let nodes = this.$refs.refCascader.getCheckedNodes()[0];
  // let [provinces, city, county] = nodes.pathLabels || [];
  // let [provincesId, cityId, countyId] = event || [];

  // this.form.provincesId = provincesId || '';
  // this.form.cityId = cityId || '';
  // this.form.countyId = countyId || '';
  // this.form.provinces = provinces || '';
  // this.form.city = city || '';
  // this.form.county = county || '';
  
  // 通用的数据存储方式
  let nodes = this.$refs.refCascader.getCheckedNodes()[0];
  this.form.ids = event.toString();
  this.form.label = nodes.pathLabels.toString();
}
相关推荐
alexander0681 小时前
CSS 类选择器组合
前端·css
寅时码6 小时前
React 之死·终章:一个 useRef,把闭包陷阱、依赖数组、漫天 rerender 全送走
前端·react.js·ai编程
不好听6136 小时前
HTML 事件监听机制:从 DOM Level 0 到 React 合成事件
前端·react.js·html
wordbaby6 小时前
为什么刷新页面就 404?聊聊 SPA 路由与 Nginx 的那点事
前端
daols886 小时前
vxe-table 渲染器教程一:实现金额输入控件
javascript·vue.js·vxe-table
不好听6137 小时前
React 核心概念:JSX、组件与数据驱动
前端·react.js
触底反弹7 小时前
🔥 React 零基础入门(中):500 行屎山代码到组件化的蜕变
前端·javascript·react.js
不好听6137 小时前
React vs Vue:两大前端框架技术选型深度对比
前端·vue.js·react.js
GuWenyue7 小时前
Cursor黑盒拆解!1套LangChain.js手写Mini编程Agent,自动生成React项目,效率提升60%
前端·数据库·人工智能
GuWenyue7 小时前
传统Agent工具两大痛点!300行代码落地MCP跨语言工具,彻底解耦LLM与工具
前端·人工智能·算法