【vue】-Element-UI之Cascader 级联选择器动态获取数据渲染

根据element-ui提供的属性来实现
在这里插入图片描述
在这里插入图片描述

html

 <el-form-item label="活动分类">
     <el-cascader clearable placeholder="请选择活动分类"  v-model="classify_value" :key="refreshClassify" :props="classifyProps"  @change="handleChange"  ></el-cascader>
  </el-form-item>

data:

data() {
    return { 
      classify_value: null, //分类id
      refreshClassify: 0,
      classifyArr: [], //选项数据源
      //配置项
      classifyProps: {
        label: "name",
        value: "id",
        lazy: true,
        // checkStrictly: true,
        lazyLoad(node, resolve) {
          console.log("nodenodenodenode====>", node);
          const { value, level } = node;
          console.log("88888value,level=====>", level, value);
          setTimeout(() => {
            if (node.level == 0) {
              config.selectParentClassifyPg().then((res) => {
                if (res.data.success) {
                  const arrs = res.data.data ? res.data.data : [];
                  arrs.map((i) => {
                    return (i.id = i.classifyId);
                  });
                  resolve(arrs);
                }
              });
            }
            if (node.level == 1) {
              config.selectByParentId({ parentId: value }).then((res) => {
                if (res.data.success) {
                  const arrs = res.data.data ? res.data.data : [];
                  // 通过调用resolve将子节点数据返回,通知组件数据加载完成
                  resolve(arrs);
                }
              });
            }
            if (node.level == 2) {
              config.selectByParentId({ parentId: value }).then((res) => {
                if (res.data.success) {
                  const arrs = res.data.data ? res.data.data : [];
                  resolve(arrs);
                }
              });
            } else if (node.level > 2) {
              resolve([]);
            }
          }, 100);
        },
      },
  
    
  },

methods:

  //活动分类 当选中节点变化时触发
    handleChange(val) {
      console.log(val, "this.classify_value===>",  this.classify_value );
      this.form.topClassifyId = this.classify_value[0]; 
    },

效果图:
在这里插入图片描述
选择后的效果:
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue.js 提供了非常方便的组件库,包括了级联选择器组件,可以快速实现级联选择器的功能。以下是一个简单的三级级联选择器实现: 1. 安装 `element-ui` 组件库: ```sh npm install element-ui --save ``` 2. 在 Vue 项目的入口文件中引入 `element-ui` 组件和样式: ```js import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 在你的 Vue 组件中使用 `el-cascader` 组件渲染级联选择器: ```html <template> <el-cascader :options="options" v-model="value" @change="handleChange" :props="props"> </el-cascader> </template> <script> export default { data() { return { value: [], options: [ { value: 'province1', label: '省份1', children: [ { value: 'city1', label: '城市1', children: [ { value: 'district1', label: '区域1' }, { value: 'district2', label: '区域2' } ] }, { value: 'city2', label: '城市2', children: [ { value: 'district3', label: '区域3' }, { value: 'district4', label: '区域4' } ] } ] }, { value: 'province2', label: '省份2', children: [ { value: 'city3', label: '城市3', children: [ { value: 'district5', label: '区域5' }, { value: 'district6', label: '区域6' } ] }, { value: 'city4', label: '城市4', children: [ { value: 'district7', label: '区域7' }, { value: 'district8', label: '区域8' } ] } ] } ], props: { value: 'value', label: 'label', children: 'children' } } }, methods: { handleChange(value) { console.log(value) } } } </script> ``` 以上就是一个简单的 Vue.js 级联选择器三级的实现,主要使用了 `el-cascader` 组件,通过传入 `options` 和 `props` 参数,实现了三级的级联选择器。需要注意的是,`options` 参数中的数据结构需要按照组件要求的格式进行定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值