Commit 831952c4 by 胡悦

添加省市区组件

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