优化选择地区自动绑定model以及地址自动识别示例编写
This commit is contained in:
+253
-74
@@ -1,114 +1,293 @@
|
||||
<template>
|
||||
<div id="app">
|
||||
<select
|
||||
v-model="province"
|
||||
<div>
|
||||
<el-select
|
||||
v-model="defaultAddr.province_code"
|
||||
placeholder="请选择省/市"
|
||||
@change="checkProvince"
|
||||
>
|
||||
<option
|
||||
v-for="(province, p_i) in provinceJson"
|
||||
:key="p_i"
|
||||
:value="province.name"
|
||||
<el-option
|
||||
v-for="item in provinceJson"
|
||||
:key="item.code"
|
||||
:label="item.name"
|
||||
:value="item.code"
|
||||
>
|
||||
{{ province.name }}
|
||||
</option>
|
||||
</select>
|
||||
</el-option>
|
||||
</el-select>
|
||||
<el-select
|
||||
v-model="defaultAddr.city_code"
|
||||
placeholder="请选择市/区"
|
||||
@change="checkCity"
|
||||
>
|
||||
<el-option
|
||||
v-for="city in cityJson"
|
||||
:key="city.code"
|
||||
:label="city.name"
|
||||
:value="city.code"
|
||||
>
|
||||
</el-option>
|
||||
</el-select>
|
||||
|
||||
<select v-model="city" placeholder="请选择市/区" @change="checkCity">
|
||||
<option v-for="(city, c_i) in cityJson" :key="c_i" :value="city.name">
|
||||
{{ city.name }}
|
||||
</option>
|
||||
</select>
|
||||
<select v-model="area" placeholder="请选择区/县">
|
||||
<option v-for="(area, a_i) in areaJson" :key="a_i" :value="area.name">
|
||||
{{ area.name }}
|
||||
</option>
|
||||
</select>
|
||||
<el-select
|
||||
v-model="defaultAddr.area_code"
|
||||
placeholder="请选择区/县"
|
||||
@change="checkArea"
|
||||
>
|
||||
<el-option
|
||||
v-for="area in areaJson"
|
||||
:key="area.code"
|
||||
:label="area.name"
|
||||
:value="area.code"
|
||||
>
|
||||
</el-option>
|
||||
</el-select>
|
||||
|
||||
<!-- <div class="distpicker-address-wrapper">
|
||||
<select
|
||||
v-model="province"
|
||||
placeholder="请选择省/市"
|
||||
@change="checkProvince"
|
||||
>
|
||||
<option
|
||||
v-for="(province, p_i) in provinceJson"
|
||||
:key="p_i"
|
||||
:value="province.name"
|
||||
>
|
||||
{{ province.name }}
|
||||
</option>
|
||||
</select>
|
||||
|
||||
<select v-model="city" placeholder="请选择市/区" @change="checkCity">
|
||||
<option v-for="(city, c_i) in cityJson" :key="c_i" :value="city.name">
|
||||
{{ city.name }}
|
||||
</option>
|
||||
</select>
|
||||
<select v-model="area" placeholder="请选择区/县">
|
||||
<option v-for="(area, a_i) in areaJson" :key="a_i" :value="area.name">
|
||||
{{ area.name }}
|
||||
</option>
|
||||
</select>
|
||||
</div> -->
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import AddressJson from "./lib/address-json";
|
||||
// import { Select as ElSelect,Option as ElOption } from 'element-ui';
|
||||
|
||||
import AddressJson from "./lib/address-json";
|
||||
console.log(AddressJson);
|
||||
export default {
|
||||
name: "v-distpicker",
|
||||
// components:{ElSelect,ElOption},
|
||||
props: {
|
||||
// province: { type: [String, Number], default: "" },
|
||||
// city: { type: [String, Number], default: "" },
|
||||
// area: { type: [String, Number], default: "" },
|
||||
// type: { type: String, default: "" },
|
||||
// hideArea: { type: Boolean, default: false },
|
||||
// onlyProvince: { type: Boolean, default: false },
|
||||
// staticPlaceholder: { type: Boolean, default: false },
|
||||
// placeholders: {
|
||||
// type: Object,
|
||||
// default() {
|
||||
// return {
|
||||
// province: "省",
|
||||
// city: "市",
|
||||
// area: "区",
|
||||
// };
|
||||
// },
|
||||
// },
|
||||
// districts: {
|
||||
// type: [Array, Object],
|
||||
// default() {
|
||||
// return DISTRICTS;
|
||||
// },
|
||||
// },
|
||||
// disabled: { type: Boolean, default: false },
|
||||
// provinceDisabled: { type: Boolean, default: false },
|
||||
// cityDisabled: { type: Boolean, default: false },
|
||||
// areaDisabled: { type: Boolean, default: false },
|
||||
// addressHeader: { type: String, default: "address-header" },
|
||||
// addressContainer: { type: String, default: "address-container" },
|
||||
// wrapper: { type: String, default: "distpicker-address-wrapper" },
|
||||
value: {
|
||||
type: Object,
|
||||
default: () => {
|
||||
return {
|
||||
province_code: "",
|
||||
province: "",
|
||||
city_code: "",
|
||||
city: "",
|
||||
area_code: "",
|
||||
area: "",
|
||||
};
|
||||
},
|
||||
},
|
||||
|
||||
disabled: { type: Boolean, default: false },
|
||||
provinceDisabled: { type: Boolean, default: false },
|
||||
cityDisabled: { type: Boolean, default: false },
|
||||
areaDisabled: { type: Boolean, default: false },
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
provinceJson: AddressJson,
|
||||
cityJson: [],
|
||||
areaJson: [],
|
||||
codes: [],
|
||||
province: "",
|
||||
city: "",
|
||||
area: "",
|
||||
// tab: 1,
|
||||
// showCityTab: false,
|
||||
// showAreaTab: false,
|
||||
// provinces: [],
|
||||
// cities: [],
|
||||
// areas: [],
|
||||
// currentProvince:
|
||||
// this.determineType(this.province) || this.placeholders.province,
|
||||
// currentCity: this.determineType(this.city) || this.placeholders.city,
|
||||
// currentArea: this.determineType(this.area) || this.placeholders.area,
|
||||
|
||||
defaultAddr: {
|
||||
province_code: "",
|
||||
province: "",
|
||||
city_code: "",
|
||||
city: "",
|
||||
area_code: "",
|
||||
area: "",
|
||||
},
|
||||
};
|
||||
},
|
||||
created() {},
|
||||
created() {
|
||||
this.defaultAddr = JSON.parse(JSON.stringify(this.value));
|
||||
this.created = true;
|
||||
this.checkProvince();
|
||||
this.checkCity();
|
||||
this.checkArea();
|
||||
this.created = false;
|
||||
},
|
||||
// 不知道是哪里姿势不对用了此方法就一直无法获取父主键传过来的v-model
|
||||
// model: {
|
||||
// // 使用model, 这儿2个属性,prop属性说,我要将msg作为该组件被使用时(此处为aa组件被父组件调用)v-model能取到的值,event说,我emit ‘cc’ 的时候,参数的值就是父组件v-model收到的值。
|
||||
// prop:
|
||||
// "defaultAddress",
|
||||
|
||||
// // {
|
||||
// // defaultAddress: {
|
||||
// // type: Object,
|
||||
// // default: () => {
|
||||
// // return {
|
||||
// // province_code: "",
|
||||
// // province: "",
|
||||
// // city_code: "",
|
||||
// // city: "",
|
||||
// // area_code: "",
|
||||
// // area: "",
|
||||
// // };
|
||||
// // },
|
||||
// // },
|
||||
// // },
|
||||
// event: "currentAddress",
|
||||
// },
|
||||
watch: {},
|
||||
methods: {
|
||||
// 选择省
|
||||
checkProvince(e, a) {
|
||||
console.log(e, a);
|
||||
console.log(this.province);
|
||||
checkProvince() {
|
||||
this.provinceJson.forEach((element) => {
|
||||
if (element.name == this.province) {
|
||||
if (
|
||||
(!this.defaultAddr.name &&
|
||||
element.code == this.defaultAddr.province_code) ||
|
||||
(!this.defaultAddr.code && element.name == this.defaultAddr.province)
|
||||
) {
|
||||
this.cityJson = element.children;
|
||||
this.areaJson = [];
|
||||
|
||||
this.defaultAddr.province_code = element.code;
|
||||
this.defaultAddr.province = element.name;
|
||||
|
||||
this.$emit("input", this.defaultAddr);
|
||||
if (!this.created) {
|
||||
this.defaultAddr.city_code = "";
|
||||
this.defaultAddr.area_code = "";
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
// 选择市
|
||||
checkCity(e, a) {
|
||||
console.log(e, a);
|
||||
console.log(this.city);
|
||||
checkCity() {
|
||||
this.cityJson.forEach((element) => {
|
||||
if (element.name == this.city) {
|
||||
if (
|
||||
(!this.defaultAddr.name &&
|
||||
element.code == this.defaultAddr.city_code) ||
|
||||
(!this.defaultAddr.code && element.name == this.defaultAddr.city)
|
||||
) {
|
||||
this.areaJson = element.children;
|
||||
if (!this.created) {
|
||||
this.defaultAddr.area_code = "";
|
||||
}
|
||||
this.defaultAddr.city_code = element.code;
|
||||
this.defaultAddr.city = element.name;
|
||||
|
||||
this.$emit("input", this.defaultAddr);
|
||||
}
|
||||
});
|
||||
},
|
||||
// 选择区县
|
||||
checkArea() {
|
||||
console.log(this.area_code);
|
||||
this.areaJson.forEach((element) => {
|
||||
if (
|
||||
(!this.defaultAddr.name &&
|
||||
element.code == this.defaultAddr.area_code) ||
|
||||
(!this.defaultAddr.code && element.name == this.defaultAddr.area)
|
||||
) {
|
||||
this.defaultAddr.area_code = element.code;
|
||||
this.defaultAddr.area = element.name;
|
||||
this.$emit("input", this.defaultAddr);
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
<style lang="scss" scoped>
|
||||
@import url(https://unpkg.com/element-ui@2.15.6/lib/theme-chalk/select.css);
|
||||
@import url(https://unpkg.com/element-ui@2.15.6/lib/theme-chalk/option.css);
|
||||
@import url(https://unpkg.com/element-ui@2.15.6/lib/theme-chalk/icon.css);
|
||||
|
||||
.distpicker-address-wrapper {
|
||||
color: #9caebf;
|
||||
select {
|
||||
padding: 0.5rem 0.75rem;
|
||||
height: 40px;
|
||||
font-size: 1rem;
|
||||
line-height: 1.25;
|
||||
color: #464a4c;
|
||||
background-color: #fff;
|
||||
background-image: none;
|
||||
-webkit-background-clip: padding-box;
|
||||
background-clip: padding-box;
|
||||
border: 1px solid rgba(0, 0, 0, 0.15);
|
||||
border-radius: 0.25rem;
|
||||
-webkit-transition: border-color ease-in-out 0.15s,
|
||||
-webkit-box-shadow ease-in-out 0.15s;
|
||||
transition: border-color ease-in-out 0.15s,
|
||||
-webkit-box-shadow ease-in-out 0.15s;
|
||||
-o-transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s;
|
||||
transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s;
|
||||
transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s,
|
||||
-webkit-box-shadow ease-in-out 0.15s;
|
||||
|
||||
option {
|
||||
font-weight: normal;
|
||||
display: block;
|
||||
white-space: pre;
|
||||
min-height: 1.2em;
|
||||
padding: 0px 2px 1px;
|
||||
}
|
||||
}
|
||||
ul {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
|
||||
li {
|
||||
list-style: none;
|
||||
}
|
||||
}
|
||||
.address-header {
|
||||
background-color: #fff;
|
||||
|
||||
ul {
|
||||
display: flex;
|
||||
justify-content: space-around;
|
||||
align-items: stretch;
|
||||
|
||||
li {
|
||||
display: inline-block;
|
||||
padding: 10px 10px 7px;
|
||||
|
||||
&.active {
|
||||
border-bottom: #52697f solid 3px;
|
||||
color: #52697f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.address-container {
|
||||
background-color: #fff;
|
||||
|
||||
ul {
|
||||
height: 100%;
|
||||
overflow: auto;
|
||||
|
||||
li {
|
||||
padding: 8px 10px;
|
||||
border-top: 1px solid #f6f6f6;
|
||||
|
||||
&.active {
|
||||
color: #52697f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.disabled-color {
|
||||
background: #f8f8f8;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<template>
|
||||
<div>
|
||||
<div class="box-later-2">{{'父组件在子组件使用v-model,子组件使用value接收父组件的传参'}}</div>
|
||||
<div class="box-later-2">{{value}}</div>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
export default {
|
||||
props: {
|
||||
// 🚀 子组件使用value接收父组件的传参
|
||||
value: {
|
||||
type: Object,
|
||||
|
||||
}
|
||||
// value:
|
||||
// {
|
||||
// type: Object,
|
||||
// // default: () => {
|
||||
// // return {
|
||||
// // province_code: "",
|
||||
// // province: "",
|
||||
// // city_code: "",
|
||||
// // city: "",
|
||||
// // area_code: "",
|
||||
// // area: "",
|
||||
// // };
|
||||
// // },
|
||||
// },
|
||||
},
|
||||
created() {
|
||||
this.$emit("input",{ 11:'22'});
|
||||
}
|
||||
// model: {
|
||||
// // 使用model, 这儿2个属性,prop属性说,我要将msg作为该组件被使用时(此处为aa组件被父组件调用)v-model能取到的值,event说,我emit ‘cc’ 的时候,参数的值就是父组件v-model收到的值。
|
||||
// prop:
|
||||
// "defaultAddress",
|
||||
|
||||
// // {
|
||||
// // defaultAddress: {
|
||||
// // type: Object,
|
||||
// // default: () => {
|
||||
// // return {
|
||||
// // province_code: "",
|
||||
// // province: "",
|
||||
// // city_code: "",
|
||||
// // city: "",
|
||||
// // area_code: "",
|
||||
// // area: "",
|
||||
// // };
|
||||
// // },
|
||||
// // },
|
||||
// // },
|
||||
// event: "currentAddress",
|
||||
// },
|
||||
}
|
||||
</script>
|
||||
+11
-1
@@ -1,5 +1,15 @@
|
||||
// import 'element-ui/lib/theme-chalk/select.css'
|
||||
// import 'element-ui/lib/theme-chalk/option.css'
|
||||
import Vue from 'vue'
|
||||
import { Select, Option } from 'element-ui'
|
||||
|
||||
Vue.use(Select)
|
||||
Vue.use(Option)
|
||||
|
||||
|
||||
import VDistpicker from './Distpicker'
|
||||
import AddressParse from './lib/address-parse'
|
||||
import AddressJson from './lib/address-json'
|
||||
// import { Select } from 'element-ui'
|
||||
|
||||
export default {VDistpicker,AddressParse,AddressJson}
|
||||
export default { VDistpicker, AddressParse, AddressJson }
|
||||
@@ -251,6 +251,7 @@ const parseRegionWithRegexp = (fragment, hasParseResult) => {
|
||||
}
|
||||
|
||||
if (area.length === 0) {
|
||||
console.log(fragment)
|
||||
for (let i = 1; i < fragment.length; i++) {
|
||||
const str = fragment.substring(0, i + 1)
|
||||
const regexArea = new RegExp(`\{\"code\":\"[0-9]{1,9}\",\"name\":\"${str}[\u4E00-\u9FA5]*?\",\"cityCode\":\"${city[0] ? city[0].code : '[0-9]{1,6}'}\",\"provinceCode\":\"${province[0] ? `${province[0].code}` : '[0-9]{1,6}'}\"\}`, 'g')
|
||||
@@ -277,6 +278,7 @@ const parseRegionWithRegexp = (fragment, hasParseResult) => {
|
||||
if (city.length === 0) {
|
||||
const regexCity = new RegExp(`\{\"code\":\"${cityCode}\",\"name\":\"[\u4E00-\u9FA5]+?\",\"provinceCode\":\"${provinceCode}\"\}`, 'g')
|
||||
const matchCity = cityString.match(regexCity)
|
||||
console.log(matchCity)
|
||||
city.push(JSON.parse(matchCity[0]))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user