element的Cascader 级联选择器插件

将联级选择器做成插件传值,注意用v-model来放一级二级的id数组更方便

子组件:

<template>
  <div class="cascader-component-container">
    <el-cascader ref="cascaderMallCatergory" :options="options" :show-all-levels="false" :props="props"  @change="menu" v-model="mallCatergoryCode"></el-cascader>
  </div>
</template>

<script>
import trade from '@/utils/trade'
  export default {
    data() {
      return {
        options:[],
        // 父组件传过来的一级二级菜单id
        mallCatergoryCode: [],
        props: {
          label: 'text',
          value:'code'
        }
      };
    },
    props:{
      data: {
        type: Array,
        default: []
      },
      value:{
        type: String,
        default: ''
      }
    },
    created() {
      this.options = this.data
      // 父组件传过来的一级二级菜单id是用,分割的字符串,现在转为数组
      this.mallCatergoryCode = this.value.split(',')
    },
    methods:{
      menu(e){
        console.log(e)
        if (this.mallCatergoryCode.length != 0) {       
        let arr = this.$refs['cascaderMallCatergory'].getCheckedNodes()[0].pathLabels
        // arr为选中的一级菜单与二级菜单
        // console.log( arr)   
        this.$emit('changeIndustry', {
          id: e,
          title: arr
        })
      }
      }
    }
  };
</script>

父组件:

// 将一级二级两个id用value传过去
<Cascader :data="options" :value="String(form.industryId + ',' +form.industryInfoId)" @changeIndustry="changeIndustry"></Cascader>

。。。。。。

methods: {
      changeIndustry(data){
        // 选中的一二级菜单id
        this.form.industryId = data.id[0]
        this.form.industryInfoId = data.id[1]
        console.log(data)
        this.selectValue1 = data.title[0]
        this.selectValue2 = data.title[1]
      },
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值