Commit 831952c4 by 胡悦

添加省市区组件

1 parent 816b4831
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
<span v-show="errorBags.has('businessDescription')" class="error">{{ errorBags.first('businessDescription') }}</span> <span v-show="errorBags.has('businessDescription')" class="error">{{ errorBags.first('businessDescription') }}</span>
</gl-form-item> </gl-form-item>
<gl-form-item label="地址"> <gl-form-item label="地址">
<city-area></city-area> <city-area @cityArea="getCityArea"></city-area>
</gl-form-item> </gl-form-item>
</gl-form> </gl-form>
<div slot="footer" class="dialog-footer"> <div slot="footer" class="dialog-footer">
...@@ -145,6 +145,9 @@ export default { ...@@ -145,6 +145,9 @@ export default {
}, },
handleAddData() { handleAddData() {
this.addDialogVisible = !this.addDialogVisible this.addDialogVisible = !this.addDialogVisible
},
getCityArea(params) {
console.log(params)
} }
} }
} }
......
<template> <template>
<!-- <div> --> <!-- <div> -->
<gl-row style="margin-left: 0" :gutter="10"> <gl-row
<gl-col :span="8"> style="margin-left: 0"
<gl-select filterable clearable v-model="province" @focus="selectProvince" placeholder="请选择省"> :gutter="10"
<gl-option >
v-for="(item, k) in provinceList" <gl-col :span="8">
:key="k" <gl-select
:label="item.provinceName" filterable
:value="item.provinceId"> clearable
</gl-option> v-model="province"
</gl-select> @change="selectProvince"
</gl-col> placeholder="请选择省"
<gl-col :span="8"> >
<gl-select filterable clearable v-model="province" placeholder="请选择市"> <gl-option
<gl-option v-for="(item, k) in provinceList"
v-for="item in cityList" :key="k"
:key="item.cityId" :label="item.provinceName"
:label="item.cityName" :value="item.provinceId"
:value="item.cityId"> >
</gl-option> </gl-option>
</gl-select> </gl-select>
</gl-col> </gl-col>
<gl-col :span="8"> <gl-col :span="8">
<gl-select filterable clearable v-model="province" placeholder="请选择区"> <gl-select
<gl-option filterable
v-for="item in areaList" clearable
:key="item.value" v-model="city"
:label="item.label" @change="selectCity"
:value="item.value"> placeholder="请选择市"
</gl-option> >
</gl-select> <gl-option
</gl-col> v-for="item in cityList"
</gl-row> :key="item.cityId"
:label="item.cityName"
:value="item.cityId"
>
</gl-option>
</gl-select>
</gl-col>
<gl-col :span="8">
<gl-select
filterable
clearable
v-model="area"
@change="selectArea"
placeholder="请选择区"
>
<gl-option
v-for="item in areaList"
:key="item.areaId"
:label="item.areaName"
:value="item.areaId"
>
</gl-option>
</gl-select>
</gl-col>
</gl-row>
<!-- </div> --> <!-- </div> -->
</template> </template>
...@@ -46,9 +70,14 @@ export default { ...@@ -46,9 +70,14 @@ export default {
areaList: [], areaList: [],
province: '', // 省值 province: '', // 省值
city: '', // 市值 city: '', // 市值
area: '' // 区值 area: '', // 区值
mutIndex: [], // 存储下标
mutArray: [] //
} }
}, },
props: {
// value: Array
},
mounted() { mounted() {
const URL = ' http://192.168.3.171:7300/mock/5be17454f31545347559d499/config' const URL = ' http://192.168.3.171:7300/mock/5be17454f31545347559d499/config'
const getConfig = (url = URL + '/area_config') => { const getConfig = (url = URL + '/area_config') => {
...@@ -56,35 +85,78 @@ export default { ...@@ -56,35 +85,78 @@ export default {
const xhr = new XMLHttpRequest() const xhr = new XMLHttpRequest()
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) { if (xhr.readyState === 4 && xhr.status === 200) {
resolve({'config': JSON.parse(xhr.responseText) }) resolve({ 'config': JSON.parse(xhr.responseText) })
} }
} }
xhr.open('GET', url) xhr.open('GET', url)
xhr.send() xhr.send()
}) })
} }
// 初始化
getConfig().then(data => { getConfig().then(data => {
console.log(data.config) console.log(data.config)
this.AllData = JSON.parse(JSON.stringify(data.config.provinceInfoList)) this.AllData = JSON.parse(JSON.stringify(data.config.provinceInfoList))
this.AllData.forEach(element => { this.AllData.forEach(element => {
this.provinceList.push({provinceId: element.provinceId, provinceName: element.provinceName}) this.provinceList.push({ provinceId: element.provinceId, provinceName: element.provinceName })
}) })
// this.getCity() this.getCity(0)
this.getArea(0, 0)
}) })
}, },
methods: { methods: {
selectProvince() { // 选择省
console.log('我选择了省') selectProvince(val) {
this.mutArray = [val, , ]
this.AllData.forEach((element, index) => {
if (element.provinceId === val) {
this.mutIndex[0] = index
}
})
this.getCity(this.mutIndex[0])
this.getArea(this.mutIndex[0], 0)
this.city = ''
this.area = ''
this.$emit('cityArea', this.mutArray)
},
// 选择市
selectCity(val) {
this.mutArray[1] = val
this.mutArray[2] = ''
const i = this.mutIndex[0]
this.AllData[i].cityInfoList.forEach((element, index) => {
if (element.cityId === val) {
this.mutIndex[1] = index
}
})
this.getArea(this.mutIndex[0], this.mutIndex[1])
this.area = ''
val ? this.$emit('cityArea', this.mutArray) : false
},
// 选择区
selectArea(val) {
this.mutArray[2] = val
val ? this.$emit('cityArea', this.mutArray) : false
}, },
// 获取市 // 获取市
getCity(index) { getCity(index) {
this.AllData[0].forEach(element => { const arr = []
this.cityList.push({cityId: element.cityId, cityName: element.cityName}) this.AllData[index].cityInfoList.forEach(element => {
arr.push({ cityId: element.cityId, cityName: element.cityName })
}) })
this.cityList = arr
},
// 获取区
getArea(i, j) {
const arr = []
this.AllData[i].cityInfoList[j].areaInfoList.forEach(element => {
arr.push({ areaId: element.areaId, areaName: element.areaName })
})
this.areaList = arr
} }
}, },
} }
</script> </script>
<style> <style>
</style> </style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!