上周写了一个省市区三级联动的地址选择组件,今天测试发现了一个大问题,那就是我可以正常提交地址是没错,可是当我后端返回了数据,我要点击编辑的时候,它并不会自动就给我绑定上去。
http://t.csdn.cn/qVLHEhttp://t.csdn.cn/qVLHE- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
问题:地址选择器数据不回显。
解决:用一个props,每从外面接收一个address(province,city,area),让它三个子项对应好三级,一个个绑上去就好啦!~
最终的写法:
- <template>
- <el-form ref="form" :model="form" label-width="120px">
- <el-row>
- <el-select v-model="form.province" placeholder="请选择省" @change="changePro" >
- <el-option v-for="item in addressData" :key="item.code" :label="item.name" :value="item.name">
- el-option>
- el-select>
- <el-select v-model="form.city" placeholder="请选择市" @change="changeCity">
- <el-option v-for="item in cityData" :key="item.code" :label="item.name" :value="item.name">
- el-option>
- el-select>
- <el-select v-model="form.district" placeholder="请选择区" @change="changeArea">
- <el-option v-for="item in areaData" :key="item.code" :label="item.name" :value="item.name">
- el-option>
- el-select>
- <el-input placeholder="详细地址" v-model="form.detail" style="width:200px;" clearable>el-input>
- el-row>
-
- el-form>
- template>
-
- <script>
- import address from "@/utils/address/address.json"; //全国省市区街道数据
- export default {
- data() {
- return {
- // 省数据
- addressData: [],
- // 市数据
- cityData: [],
- // 区数据
- areaData: [],
- };
- },
- props: {
- // 街道数据
- form: {
- province: "",
- // 市
- city: "",
- // 区
- district: "",
- //详细地址
- detail: "",
- }
- },
- created() {
- // 省份数据初始化
- this.addressData = address;
- // console.log("this.form",this.form)
-
- if (this.form.province != "") {
- let cityData = this.addressData.filter(item => item.name == this.form.province)
- if (cityData.length) {
- this.cityData = cityData[0].children
-
- let areaData = this.cityData.filter(item => item.name == this.form.city)
- if (areaData.length) {
- this.areaData = areaData[0].children
- }
- }
- }
- },
- computed: {
- //选择结果
- result() {
- if (!this.form.district) {
- return ''
- } else if (this.form.district && this.form.detail) {
- return '' + this.form.province + "," + this.form.city + "," + this.form.district + "," + this.form.detail
- } else {
- return '' + this.form.province + "," + this.form.city + "," + this.form.district
- }
- }
- },
- methods: {
- reset() {
- this.form = {
- // 省
- province: "",
- // 市
- city: "",
- // 区
- district: "",
- //详细地址
- detail: "",
- }
- },
- // 省份更改
- changePro(e) {
- // 从省中过滤出市的数据
- this.cityData = this.addressData.filter((item) => {
- return item.name == e;
- })[0].children;
- // 省发生改变的时候 清空输入框市区街道的内容
-
- this.form.district = "";
- this.form.city = this.cityData[0].name;
- // 省发生更改时 该表空区街道数据的内容
- this.areaData = this.cityData[0].children;
- this.form.district = this.areaData[0].name
- },
- // 市更改
- changeCity(e) {
- // 获取到区的数据
- this.areaData = this.cityData.filter(
- (item) => item.name == e
- )[0].children;
- // 清空数据后面对应数组的数据
- this.form.district = this.areaData[0].name;
- },
- // 区更改
- changeArea(e) {
- let temp = this.areaData.filter((item) => item.name == e);
- // 获取到区的code码
- this.form.regionalNumber = temp[0].code;
- // 获取到街道的数据
- this.jdData = this.areaData.filter((item) => item.name == e)[0].children;
- },
- },
- };
- script>
-
- <style lang="scss" rel="stylesheet/scss" scoped>
- .el-row {
- display: inline;
- }
-
- .el-row {
- display: inline-flex;
- flex: auto;
-
- .el-select {
- margin: 0 20px 0 0;
- width: 122px;
- }
- }
- style>
- <el-row>
- <el-col>
- <el-form-item label="联系地址" prop="address" v-model="ruleForm.address">
- <checkAddress v-model="ruleForm.address" :form="ruleForm.address" ref="address"/>
- el-form-item>
- el-col>
- el-row>
- <el-table-column prop="address" label="联系地址">
- <template slot-scope="scope">
- <span v-for="(item,index) in scope.row.address" :key="index" :label="item">
- <span>
- {{item}}
- span>
- span>
- template>
- el-table-column>
- address: {
- // 省
- province: "",
- // 市
- city: "",
- // 区
- district: "",
- //详细地址
- detail:""
- },//联系地址
- watch:{
- ruleForm:{
- // deep:true,
- handler(){
- this.ruleForm.address=this.$refs.address.form
- console.log(this.ruleForm.address)
- }
- }
- created(){
- if(this.rowData.contactType=="ADDRESS"){
- this.ruleForm.address.province=this.rowData.address.province
- this.ruleForm.address.city=this.rowData.address.city
- this.ruleForm.address.district=this.rowData.address.district
- this.ruleForm.address.detail=this.rowData.address.detail
- }
- }
就这样愉快地解决问题啦!从此点击编辑,相对应的地址数据会回显。
四、如果是多个填写项,需要时让他们一一对应
- <el-row>
- <el-col>
- <el-form-item label="联系地址" prop="address" v-model="item.address">
-
- <checkAddress ref="address" v-model="item.address" :key="index" :form="item.address"/>
- el-form-item>
- el-col>
- el-row>
- address: {
- // 省
- province: "",
- // 市
- city: "",
- // 区
- district: "",
- //详细地址
- detail:""
- },//联系地址
- if(item.contactType=="ADDRESS"){
- current.address.province=item.contactTypeFlex1
- current.address.city=item.contactTypeFlex2
- current.address.district=item.contactTypeFlex3
- current.address.detail=item.contactTypeFlex34
- }
- this.tempList.push(current)