Vant Cascader级联选择 实现省市区三级

前面有一篇文章是介绍Vue + MintUI实现的省市区三级联动,今天介绍使用Vant框架中的Cascader来实现省市区三级联动。建议先搂一遍,官网介绍~~

 

有赞官网Cascader组件介绍Vant 2 - Mobile UI Components built on Vue

本次实现的省市区三级联动,数据是通过后端接口获取,对应数据格式分享如下

省:

result: [
    {name: "山西省", code: "140000", childResources: null}
    {name: "内蒙古自治区", code: "150000", childResources: null}
    {name: "北京市", code: "110000", childResources: null}
    {name: "天津市", code: "120000", childResources: null}
    {name: "河北省", code: "130000", childResources: null}
]

市:

result:[
    {name: "晋城市", code: "140500", childResources: null}
    {name: "朔州市", code: "140600", childResources: null}
    {name: "晋中市", code: "140700", childResources: null}
    {name: "运城市", code: "140800", childResources: null}
    {name: "忻州市", code: "140900", childResources: null}
    {name: "临汾市", code: "141000", childResources: null}
    {name: "吕梁市", code: "141100", childResources: null}
    {name: "太原市", code: "140100", childResources: null}
    {name: "大同市", code: "140200", childResources: null}
    {name: "阳泉市", code: "140300", childResources: null}
]

 区、县:

result:[
    {name: "城区", code: "140502", childResources: null}
    {name: "沁水县", code: "140521", childResources: null}
    {name: "阳城县", code: "140522", childResources: null}
    {name: "陵川县", code: "140524", childResources: null}
    {name: "泽州县", code: "140525", childResources: null}
    {name: "高平市", code: "140581", childResources: null}
]

1、在index.js中引入Vant,并注册声明

import Vant from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

2、页面使用(template)

<van-field v-model="accidentArea.value" label="地区" readonly placeholder="省市区三级选择"  @click="showAreaPop()">
        			<div slot="extra" style="width: 18px; height: 18px; padding-top: 3px;"><img width="18px" :src="icons.location" /></div>
        		</van-field>



<!--出险地区-->
<van-popup v-model="accidentArea.visible" round position="bottom" >
	<van-cascader
		class="cascader-options"
		v-model="accidentArea.cascaderValue"
		title="请选择出险地区"
		:options="accidentArea.options"
		@close="hideAreaPop"
		@change="onChangeAcidentArea"
		@finish="onFinishaAcidentArea"
		:field-names="{ text: 'name', value: 'code', children: 'childResources' }"
	/>
</van-popup>

3、对应JS方法

export default {
	data() {
		return {
			accidentArea : {
				value : '',			//显示的值
				cascaderValue : '',	//显示的值
				visible : false,	//是否显示
				options: []
			},
        }
    }
}


//显示出险地区
showAreaPop(){
	this.accidentArea.visible = true;
	if(this.accidentArea.options.length == 0){
		//省
		queryProvince({level:'',parentCode:""}).then(res =>{
			if(res.code == '0'){
				res.result.forEach(item => item.childResources = [])
				this.accidentArea.options = res.result;
			}
		})
	}
},
//关闭弹层
closeAreaPop(){
	this.accidentArea.visible = false;
},
// 选择完成,触发 finish事件
onFinishaAcidentArea({ selectedOptions }) {
	this.accidentArea.visible = false;
	this.accidentArea.value = selectedOptions.map((option) => option.name).join('/');
	this.params.accidentArea = selectedOptions.map((option) => option.code).join('_');
},
//选中项变化时触发 	出险地区
onChangeAcidentArea({ value, selectedOptions, tabIndex }){
	let level = tabIndex === 0 ? '01' : '02';
	queryAreas({level:level,parentCode:value}).then(res =>{
		if(res.code == '0'){
			//市
			if(tabIndex === 0){
				res.result.forEach(item => item.childResources = [])
				let index = this.accidentArea.options.findIndex(item =>item.code == value);
				this.accidentArea.options[index].childResources = res.result
			}else if(tabIndex === 1){
				let firstIndex = this.accidentArea.options.findIndex(item =>item.code == selectedOptions[0].code);		//省级 index
				let cities =  this.accidentArea.options[firstIndex].childResources	//所有城市
				let index = cities.findIndex(item =>item.code == value);	//市级 index
				cities[index].childResources = res.result
			}
		}
	})
}

逻辑感觉没啥可说得,field-names这个属性需要注意下,官网对于这个字段的解释通过 field-names 属性可以自定义 options 里的字段名称

意思就是如果后端返回的字段不符合你的预期字段,你可以通过这个属性重新定义下你需要展示的字段。

组件原先的名称是text,后台返回的字段是其他的。那就可以像官网一样,做一个映射的关系。将接口返回字段映射到text。

本次分享完毕~

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
vant级联选择器(Cascader)组件提供了异步加载数据的功能,可以用于无限层级的地区选择。以下是一个示例代码: ``` <template> <van-cascader :options="options" :load-data="loadData" v-model="value" :placeholder="'请选择地区'" :columns="3" /> </template> <script> export default { data() { return { value: [], options: [ { text: '省份', value: 'province', children: [], }, { text: '城市', value: 'city', children: [], }, { text: '区县', value: 'district', children: [], }, ], }; }, methods: { async loadData(item) { const data = await this.getAreaData(item.value); item.children = data; }, async getAreaData(value) { // 根据 value 获取对应的数据 // 这里可以通过异步请求获取数据 // 返回一个 Promise }, }, }; </script> ``` 以上代码中,`options` 数组是级联选择器的选项列表,其中每个选项都有 `text`、`value` 和 `children` 三个属性。`text` 和 `value` 表示选项的显示文本和值,`children` 是一个空数组,用于存放子选项。 `loadData` 方法是级联选择器异步加载数据的回调函数,它接收一个 `item` 参数,表示当前选中的选项。在该方法中,可以根据当前选项的值通过异步请求获取子选项数据,并将数据赋值给 `item.children` 属性。这样,在用户选择当前选项时,就会自动触发加载子选项的操作。 在实际使用中,需要根据具体的业务需求调整代码,例如修改选项显示文本、修改异步请求的参数等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值