级联选择器(el-cascader)动态加载(lazyLoad)实现省市区三级选择、回显及参数整合

需求:

<template>
  <div class="app-container">
    <FirmInfo :data-form="dataForm" />
    <el-button
      @click="editCorpInfo"
    >编辑</el-button>
    <!-- 编辑-->
    <el-dialog
      append-to-body
    >
      <el-form
        ref="update"
        :model="corpInfo"
      >
        <com-form-item
          :config="corpInfoConfig"
          :form="corpInfo"
          :column="1"
          :no-set-label-width="true"
        />
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import FirmInfo from './firmInfo.vue'
import {
  query,
  editEtp
} from '@/api/user/blocks/Detail'
import { queryy } from '@/api/ate/lar'
export default {
  name: 'Detail',
  components: {
    FirmInfo
  },
  data() {
    const that = this
    return {
      corpInfo: {
        corpAddress: ''// 详细地址
      },
      provinceData: [], // 省份
      provinceList: [], // 省份
      provinceData1: [], // 全省数据
      cityData: [], // 全市数据
      regionData: [], // 全区数据
      lazyProps: {
        value: 'id',
        label: 'name',
        lazy: true,
        lazyLoad(node, resolve) {
          that.lazyLoadBron(node, resolve)
        }
      }
    }
  },
  computed: {
    corpInfoConfig() {
      const config = [
        {
          itemType: 'cascader',
          prop: 'CorpInfo',
          placeholder: this.corpInfo.CorpInfoName && this.corpInfo.CorpInfoName.length > 0 ? (this.corpInfo.CorpInfoName) : '请选择(选填)',
          label: '地址:',
          width: 'calc(100% - 95px)',
          props: this.lazyProps,
          change: this.corpAddressChange,
          list: this.provinceList
        },
        {
          itemType: 'input',
          prop: 'corpAddress',
          label: ' ',
          placeholder: '请输入地址',
          width: 'calc(100% - 95px)'
        }
      ]
      return config
    }
  },
  created() {
    this.requestC()
  },
  mounted() {
    // 获取省数据
    this.getProvinceList()
    // 获取市数据
    this.getCityList()
    // 获取区数据
    this.getRegionList()
  },
  methods: {
    // 企业地址输入框值改变
    corpAddressChange(e) {},
    // 获取市数据(整合参数)
    getCityList() {
      queryy(params).then(res => {
        if (res) {
          this.cityData = res.Data
        }
      })
    },
    // 获取区数据(整合参数)
    getRegionList() {
      queryy(params).then(res => {
        if (res) {
          this.regionData = res.Data
        }
      })
    },
    // 获取省数据
    getProvinceList() {
      queryy(params).then((res) => {
        if (res.Data && res.Data.length > 0) {
          this.provinceData1 = res.Data// (整合参数)
          this.provinceData = res.Data
          this.provinceList = res.Data.map((item) => {
            item.label = item.name
            item.value = item.id
            return item
          })
        }
      })
    },
    lazyLoadBron(node, resolve) {
      const that = this
      const { level, value, data } = node
      if (level === 1) {
        queryy({ parentId: value }).then(res => {
          if (res) {
            that.provinceData = res.Data
            resolve && resolve(res.Data)
          }
        })
      }
      if ([2, 3].includes(level)) {
        queryy({ parentId: data.id }).then(res => {
          if (res) {
            const nodes = res.Data.map(e => {
              return {
                id: e.id,
                name: e.name,
                // 地点只选到市区,level >= 2
                leaf: level >= 2
              }
            })
            resolve && resolve(nodes)
          }
        })
      }
    },
    // 获取信息(其他页面显示)
    requestC() {
      query({ corpId: this.$route.query.id }).then((res) => {
        const data = res.Data
        this.dataForm = { ...data }
        if (res.Data.corpAddress) {
          this.dataForm.corpAddress = res.Data.province + '/' + res.Data.city +
 '/' + res.Data.region +
 '/' + res.Data.corpAddress
        } else {
          this.dataForm.corpAddress = res.Data.province + '/' + res.Data.city +
 '/' + res.Data.region
        }
        // 编辑信息所需数据(回显)
        this.corpInfo = { ...data }
        this.corpInfo.corpId = data.id
        const { email } = data
        this.accountInfo.email = email
        const arr = []
        arr.push(data.provinceCode)
        arr.push(data.cityCode)
        arr.push(data.regionCode)
        this.corpInfo.CorpInfo = arr
        this.corpInfo.CorpInfoName = data.province + '/' + data.city + '/' + data.region
      })
    },
    // 确定编辑信息按钮(整合参数)
    confirmUpdateEnterprise() {
      this.$refs.update.validate((valid) => {
        if (valid) {
          const params = {
            corpInfo: { ...this.corpInfo,
              provinceCode: this.corpInfo.CorpInfo[0],
              province: this.provinceData1.find(e => e.id === this.corpInfo.CorpInfo[0])?.name,
              cityCode: this.corpInfo.CorpInfo[1],
              city: this.cityData.find(e => e.id === this.corpInfo.CorpInfo[1])?.name,
              regionCode: this.corpInfo.CorpInfo[2],
              region: this.regionData.find(e => e.id === this.corpInfo.CorpInfo[2])?.name },
            accountInfo: this.accountInfo
          }
          editEtp(params).then((res) => {
            if (res.Data) {
              this.editCorpInfoDialogVisible = false
              this.requestC()
              this.$message({
                message: '操作成功',
                type: 'success'
              })
            }
            if (res.Error.Message) {
              this.$message({
                message: res.Error.Message,
                type: 'error'
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-cascader{
  input::-webkit-input-placeholder { /* WebKit browsers */
 color: #606266!important;
}
}
</style>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 Element UI 的级联选择器 el-cascader,如果需要回显已经选中的值,可以通过设置 v-model 和 :options 属性来实现。 假设有一个三级联动的级联选择器,选项数据如下: ``` options: [{ value: 'zhinan', label: '指南', children: [{ value: 'shejiyuanze', label: '设计原则', children: [{ value: 'yizhi', label: '一致' }, { value: 'fankui', label: '反馈' }] }, { value: 'daohang', label: '导航', children: [{ value: 'cexiangdaohang', label: '侧向导航' }, { value: 'dingbudaohang', label: '顶部导航' }] }] }] ``` 如果需要回显已经选中的值,可以将当前选中的值通过 v-model 绑定到 data 中的一个变量上,例如: ``` <el-cascader v-model="selectedOptions" :options="options" ></el-cascader> ``` 其中,selectedOptions 是一个数组,用于保存当前选中的值。 接下来,需要在 mounted 钩子函数中设置已选中的值,例如: ``` mounted() { this.selectedOptions = ['zhinan', 'shejiyuanze', 'yizhi'] } ``` 这样,在页面加载完成后,级联选择器就会自动回显已经选中的值。 需要注意的是,如果级联选择器的选项数据是异步加载的,需要在加载完成后再设置已选中的值,例如: ``` mounted() { this.loadOptions().then(() => { this.selectedOptions = ['zhinan', 'shejiyuanze', 'yizhi'] }) }, methods: { loadOptions() { // 异步加载选项数据 } } ``` 这样就可以实现 Element UI 的级联选择器 el-cascader 的回显功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值