|
| 1 | +<template> |
| 2 | + <div class="m-iselect"> |
| 3 | + <span class="name">按省份选择:</span> |
| 4 | + <el-select v-model="pvalue" placeholder="省份"> |
| 5 | + <el-option |
| 6 | + v-for="item in province" |
| 7 | + :key="item.value" |
| 8 | + :label="item.label" |
| 9 | + :value="item.value" |
| 10 | + /> |
| 11 | + </el-select> |
| 12 | + <el-select v-model="cvalue" :disabled="!city.length" placeholder="城市"> |
| 13 | + <el-option v-for="item in city" :key="item.value" :label="item.label" :value="item.value"/> |
| 14 | + </el-select> |
| 15 | + <el-autocomplete |
| 16 | + v-model="input" |
| 17 | + :fetch-suggestions="querySearchAsync" |
| 18 | + placeholder="请输入城市中文或拼音" |
| 19 | + @select="handleSelect" |
| 20 | + /> |
| 21 | + </div> |
| 22 | +</template> |
| 23 | + |
| 24 | +<script> |
| 25 | +import _ from "lodash"; |
| 26 | +const axios = require("../../server/interface/utils/axios"); |
| 27 | +import { createNamespacedHelpers } from "vuex"; |
| 28 | +const { mapState, mapActions } = createNamespacedHelpers("geo"); |
| 29 | +export default { |
| 30 | + data() { |
| 31 | + return { |
| 32 | + province: [], |
| 33 | + pvalue: "", |
| 34 | + city: [], |
| 35 | + cvalue: "", |
| 36 | + input: "", |
| 37 | + cities: [] |
| 38 | + }; |
| 39 | + }, |
| 40 | + watch: { |
| 41 | + pvalue: async function(newPvalue) { |
| 42 | + let self = this; |
| 43 | + let { |
| 44 | + status, |
| 45 | + data: { city } |
| 46 | + } = await axios.get(`/geo/province/${newPvalue}`); |
| 47 | + if (status === 200) { |
| 48 | + self.city = city.map(item => { |
| 49 | + return { |
| 50 | + value: item.id, |
| 51 | + label: item.name |
| 52 | + }; |
| 53 | + }); |
| 54 | + self.cvalue = ""; |
| 55 | + } |
| 56 | + } |
| 57 | + }, |
| 58 | + // 初始化获取所有的省份 |
| 59 | + mounted: async function() { |
| 60 | + let self = this; |
| 61 | + let { |
| 62 | + status, |
| 63 | + data: { province } |
| 64 | + } = await axios.get("/geo/province"); |
| 65 | + if (status === 200) { |
| 66 | + self.province = province.map(item => { |
| 67 | + return { |
| 68 | + value: item.id, |
| 69 | + label: item.name |
| 70 | + }; |
| 71 | + }); |
| 72 | + } |
| 73 | + }, |
| 74 | + methods: { |
| 75 | + ...mapActions(["setPosition"]), |
| 76 | + querySearchAsync: _.debounce(async function(query, cb) { |
| 77 | + let self = this; |
| 78 | + if (self.cities.length) { |
| 79 | + cb(self.cities.filter(item => item.value.indexOf(query) > -1)); |
| 80 | + } else { |
| 81 | + let { |
| 82 | + status, |
| 83 | + data: { city } |
| 84 | + } = await axios.get("/geo/city"); |
| 85 | + if (status === 200) { |
| 86 | + self.cities = city.map(item => { |
| 87 | + return { |
| 88 | + value: item.name |
| 89 | + }; |
| 90 | + }); |
| 91 | + cb(self.cities.filter(item => item.value.indexOf(query) > -1)); |
| 92 | + } else { |
| 93 | + cb([]); |
| 94 | + } |
| 95 | + } |
| 96 | + }, 200), |
| 97 | + handleSelect: function(item) { |
| 98 | + this.setPosition({ city: item.value }); |
| 99 | + // 选中进行页面跳转 |
| 100 | + window.location.href = "/"; |
| 101 | + } |
| 102 | + } |
| 103 | +}; |
| 104 | +</script> |
| 105 | + |
| 106 | +<style lang="scss"> |
| 107 | +@import "@/assets/css/changeCity/iselect.scss"; |
| 108 | +</style> |
0 commit comments