vue el-cascader 动态加载(三级分类或省市区)

本文介绍了如何在Vue项目中使用el-cascader组件实现动态加载三级分类,例如省市区选择。通过在template中设置配置,data中定义初始数据,以及在script的methods中编写加载逻辑,实现按需加载数据,提高应用性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.tempalte中:

    <el-dialog
        width="600px"
        title="商品类目编辑"
        :visible.sync="dialogVisible">
        <el-cascader
          v-model="partyOrganId"
          :props="{
            value: 'id',
            label: 'name',
            children: 'cities'
          }"
          :options="cascaderData"
          :show-all-levels="false"
          @change="handleChange"
          @active-item-change="handleItemChange"
          filterable
        ></el-cascader>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="categoryEdit">确 定</el-button>
        </span>
      </el-dialog>

2.data中:

  partyOrganId: [],
  cascaderData: [],

3.在编辑入口
  this.getNodes()

4. script中(methods):

    handleChange (value) {
      this.partyOrganId = value
    },
    handleItemChange (val) {
      console.log(val, 'ggggggg')
      this.getNodes(val)
    },
    getNodes(val) {
      let idArea
      let sizeArea
      if (!val) {
        idArea = null
        sizeArea = 0
      } else if (val.length === 1) {
        idArea = val[0]
        sizeArea = val.length
      } else if (val.length === 2) {
        idArea = val[1]
        sizeArea = val.length
      }
      getproductcategory({
        id: idArea,
        shop_id: this.ssid
      }).then(res => {
        if (res.status === 200) {
          this.ones = res.data
          if (sizeArea === 0) {
            this.cascaderData = this.ones.map((value, i) => {
              return {
                id: value.id,
                name: value.name,
                cities: []
              }
            })
          } else if (sizeArea === 1) {
            this.cascaderData.map((value, i) => {
              if (value.id === val[0]) {     // 遍历数据源=选中
                if (!value.cities.length) {  //value.cities不存在时
                  value.cities = this.ones.map((value, i) => {
                    return {
                      id: value.id,
                      name: value.name,
                      cities: []
                    }
                  })
                }
              }
            })
          } else if(sizeArea === 2) {
            this.cascaderData.map((value, i) => {
              if (value.id === val[0]) {
                value.cities.map((value, i) => {
                  if (value.id === val[1]) {
                    if (!value.cities.length) {
                      value.cities = this.ones.map((value, i) => {
                        return {
                          id: value.id,
                          name: value.name
                        }
                      })
                    }
                  }
                })
              }
            })
          }
        }
      })
    },

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值