案例演示
步骤
封装的组件,之后引用组件。
父:
<pickerAddress @change="change">
<input type="text" placeholder="请选择所在地区" :value="value" />
<image :src="$common.image('/static/personal/addressNext.png')" mode="aspectFill" class="next"></image>
</pickerAddress>
import pickerAddress from "@/components/citySelect/citySelect.vue"
export default {
components: {
pickerAddress
},
data() {
return {
// 城市选择
value: ""
};
},
methods:{
// 城市选择
change(data) {
this.value = data.data.join('')
console.log(this.value);
}
}
}
组件:
citySelect文件:
<template>
<picker @change="bindPickerChange" @columnchange="columnchange" :range="array" range-key="name" :value="value"
mode="multiSelector" style="display:flex;justify-content: space-between;">
<slot></slot>
</picker>
</template>
<script>
import AllAddress from '@/components/citySelect/data.js'
let selectVal = ['', '', '']
export default {
data() {
return {
value: [0, 0, 0],
array: [],
index: 0
}
},
created() {
this.initSelect()
},
methods: {
// 初始化地址选项
initSelect() {
this.updateSourceDate() // 更新源数据
.updateAddressDate() // 更新结果数据
.$forceUpdate() // 触发双向绑定
},
// 地址控件改变控件
columnchange(d) {
this.updateSelectIndex(d.detail.column, d.detail.value) // 更新选择索引
.updateSourceDate() // 更新源数据
.updateAddressDate() // 更新结果数据
.$forceUpdate() // 触发双向绑定
},
/**
* 更新源数据
* */
updateSourceDate() {
this.array = []
this.array[0] = AllAddress.map(obj => {
return {
name: obj.name
}
})
this.array[1] = AllAddress[this.value[0]].cityList.map(obj => {
return {
name: obj.name
}
})
this.array[2] = AllAddress[this.value[0]].cityList[this.value[1]].areaList.map(obj => {
return {
name: obj.name
}
})
return this
},
/**
* 更新索引
* */
updateSelectIndex(column, value) {
let arr = JSON.parse(JSON.stringify(this.value))
arr[column] = value
if (column === 0) {
arr[1] = 0
arr[2] = 0
}
if (column === 1) {
arr[2] = 0
}
this.value = arr
return this
},
/**
* 更新结果数据
* */
updateAddressDate() {
selectVal[0] = this.array[0][this.value[0]].name
selectVal[1] = this.array[1][this.value[1]].name
selectVal[2] = this.array[2][this.value[2]].name
return this
},
/**
* 点击确定
* */
bindPickerChange(e) {
this.$emit('change', {
index: this.value,
data: selectVal,
array: this.array
})
return this
}
}
}
</script>
<style>
</style>
data.js
放不下了 放资源里吧