element Cascader 级联选择器动态加载实例

Posted 包子源

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了element Cascader 级联选择器动态加载实例相关的知识,希望对你有一定的参考价值。

封装Cascader 级联选择器动态加载组件实现省市区三级联动

组件代码片段

<template>
  <el-cascader ref="myCascader" @change="handleChange" v-model="partyOrganId" placeholder="请选择" :props="areaprop"></el-cascader>
</template>

<script>
import  findProvince, findChildById  from "@/api/base";
export default 
  name: "AreaTree",
  props: 
    partyOrganId: 
      type: Array,
      default() 
        return []
      
    
  ,
  data() 
    return 
      areaprop: 
        lazy: true,
        lazyLoad(node, resolve) 
          setTimeout(() => 
            if (node.level == 0) 
              findProvince().then(response => 
                const cities = response.data.map((value, i) => (
                  value: value.id,
                  label: value.name,
                  leaf: node.level >= 2
                ));
                resolve(cities);
              );
            
            if (node.level == 1) 
              findChildById(node.value).then(response => 
                const areas = response.data.map((value, i) => (
                  value: value.id,
                  label: value.name,
                  leaf: node.level >= 2
                ));
                resolve(areas);
              );
             else if (node.level == 2) 
              findChildById(node.value).then(response => 
                const areas = response.data.map((value, i) => (
                  value: value.id,
                  label: value.name,
                  leaf: node.level >= 2
                ));
                resolve(areas);
              );
            
          , 100);
        
      
    ;
  ,
  mounted() 
    // console.log(this.partyOrganId)
  ,
  methods: 
    handleChange(value)
      this.$emit("changeAreaTree",value)
    
  
;
</script>

引用组件

<area-tree :partyOrganId="oid" @changeAreaTree="changeAreaTreeVal()"></area-tree>

后台接口:

findProvince返回数据格式


	"code":"0",
	"data":[
		
			"id":"110000",
			"levelType":1,
			"mergeName":"中国,北京",
			"name":"北京",
			"parentId":"100000"
		,
		
			"id":"120000",
			"levelType":1,
			"mergeName":"中国,天津",
			"name":"天津",
			"parentId":"100000"
		,
		
			"id":"130000",
			"levelType":1,
			"mergeName":"中国,河北省",
			"name":"河北省",
			"parentId":"100000"
		
	],
	"msg":"success",
	"serverTime":"2020-05-15 11:38:53"

findChildById/110000 返回数据格式:


	"code":"0",
	"data":[
		
			"id":"110100",
			"levelType":2,
			"mergeName":"中国,北京,北京市",
			"name":"北京市",
			"parentId":"110000"
		
	],
	"msg":"success",
	"serverTime":"2020-05-15 11:38:55"

效果图

以上是关于element Cascader 级联选择器动态加载实例的主要内容,如果未能解决你的问题,请参考以下文章

基于layui的cascader级联选择器

elementUI的级联选择器el-cascader

element-ui之级联选择器(Cascader) 全选 功能

修改antd级联选择器(cascader)

怎么获取cascader级联选择器的值

el-cascader级联选择懒加载数据回显解决办法