vue實現省市區三級聯動


首先呈現效果圖

 

 

 

1.我的是通過element-ui實現的;可自由選擇UI

2.話不多說,直接上代碼,結構非常簡單

 1 <el-form ref="form" :model="form" label-width="100px">
 2       <el-form-item  label="">
 3            一:vue實現城市聯動選擇
 4       </el-form-item>
 5       <el-form-item  label="">
 6            一:可清除輸入框記錄
 7       </el-form-item>
 8       <el-form-item  label="所在地區:" class="selform">
 9           <el-cascader
10             :options="CityInfo"
11             v-model="form.selectedOptions"
12             :change-on-select="true"
13             :clearable="true"
14             :filterable="true"
15             @change="handleChange">
16            </el-cascader>
17           <span>所選地區:{{form.city | myAddressCity}}{{form.erae | myAddressErae}}{{form.minerae | myAddressMinerae}}</span>
18       </el-form-item>
19  </el-form>

 

3.js部分(重點部分是實現字段的重重過濾)

 1 el: "#app",
 2     data: {
 3         CityInfo: CityInfo,//地區數據
 4         form: {
 5             city: '',
 6             erae: '',
 7             minerae: '',
 8             selectedOptions: [],//地區篩選數組
 9         },
10     },
11     methods: {
12         handleChange(value) {
13             this.form.city = this.form.selectedOptions[0];
14             this.form.erae = this.form.selectedOptions[1]
15             this.form.minerae = this.form.selectedOptions[2]
16         },
17     },
18     /*字段過濾*/
19     filters: {
20         myAddressCity: function (value) {
21             for (y in this.CityInfo) {
22                 if (this.CityInfo[y].value == value) {
23                     return value = this.CityInfo[y].label
24                 }
25             }
26         },
27         myAddressErae: function (value) {
28             for (y in this.CityInfo) {
29                 for (z in this.CityInfo[y].children) {
30                     if (this.CityInfo[y].children[z].value == value && value != undefined) {
31                         return value = this.CityInfo[y].children[z].label;
32                     }
33                 }
34             }
35         },
36         myAddressMinerae: function (value) {
37             for (y in this.CityInfo) {
38                 for (z in this.CityInfo[y].children) {
39                     for (i in this.CityInfo[y].children[z].children) {
40                         if (this.CityInfo[y].children[z].children[i].value == value && value != undefined) {
41                             return value = this.CityInfo[y].children[z].children[i].label
42                         }
43                     }
44                 }
45             }
46         },
47     },

 

4.城市數據代碼格式

{
		value: 1, label: '北京', children: [
			{
				value: 1, label: '北京市', children: [
					{ value: 1, label: '東城區' },
					{ value: 2, label: '西城區' },
					{ value: 3, label: '崇文區' },
					{ value: 4, label: '宣武區' },
					{ value: 5, label: '朝陽區' },
					{ value: 6, label: '豐台區' },
					{ value: 7, label: '石景山區' },
					{ value: 8, label: '海淀區' },
					{ value: 9, label: '門頭溝區' },
					{ value: 10, label: '房山區' },
					{ value: 11, label: '通州區' },
					{ value: 12, label: '順義區' },
					{ value: 13, label: '昌平區' },
					{ value: 14, label: '大興區' },
					{ value: 15, label: '懷柔區' },
					{ value: 16, label: '平谷區' },
					{ value: 17, label: '密雲縣' },
					{ value: 18, label: '延慶縣' }
				]
			}
		]
	},

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM