select.vue 2.95 KB
<template>
  <el-popover trigger="click" popper-class="city_select_popover">
    <div class="city_list">
      <div class="cell_box">
        <el-scrollbar class="el_scrollbar">
          <div class="cell" :class="showClassName(item)" v-for="(item,index) in provinceList" :key="`province_${index}`" @click="showChildren(item,'city')">
            <span>{{item.fullname}}</span>
            <i class="el-icon-arrow-right"></i>
          </div>
        </el-scrollbar>
      </div>
      <div class="cell_box" v-show="cityList && cityList.length>0">
        <el-scrollbar class="el_scrollbar">
          <div class="cell" :class="showClassName(item)" v-for="(item,index) in cityList" :key="`city_${index}`" @click="showChildren(item,'area')">
            <span>{{item.fullname}}</span>
            <i class="el-icon-arrow-right"></i>
          </div>
        </el-scrollbar>
      </div>
      <div class="cell_box" v-show="areaList && areaList.length>0">
        <el-scrollbar class="el_scrollbar">
          <div class="cell" :class="showClassName(item)" v-for="(item,index) in areaList" :key="`area_${index}`" @click="showChildren(item)">{{item.fullname}}</div>
        </el-scrollbar>
      </div>
    </div>
    <el-input slot="reference" v-model="selectText"></el-input>
  </el-popover>
</template>
<script>
import cityJson from "@/assets/json/city.json";
export default {
  name: "CitySelect",
  props: {
    value: "",
  },
  data() {
    return {
      provinceList: cityJson,
      cityList: [],
      areaList: [],
      selectText: "",
      areaCode: [],
      areaName: [],
    };
  },
  mounted() {
    console.log(cityJson);
  },
  methods: {
    // 获取省市区
    showChildren(row, id) {
      let typeList = {
        city: 0,
        area: 1,
      };
      if (id) {
        this.areaList = [];
      }
      let idx = id ? typeList[id] : 2;
      this.$set(this.areaCode, idx, row.code);
      this.$set(this.areaName, idx, row.fullName);
      this[id + "List"] = row.children || [];
    },

    // 是否选中
    showClassName(row) {
      let list = this.areaCode;
      let idx = list.findIndex((k) => k == row.code);
      if (idx >= 0) {
        return "active";
      } else {
        return "";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.city_list {
  display: flex;
  .cell_box {
    width: 184px;
    height: 300px;
    position: relative;
    .cell {
      padding: 8px 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      cursor: pointer;
    }
    .active {
      color: #409eff;
      background: #f5f7fa;
    }
  }
  .cell_box:after {
    position: absolute;
    display: block;
    content: "";
    width: 1px;
    background: #f2f2f2;
    top: 0;
    bottom: 0;
    right: 0;
  }
  .cell_box:last-child:after {
    display: none;
  }
  .el_scrollbar {
    /deep/ .el-scrollbar__wrap {
      overflow-x: hidden;
    }
  }
}
</style>
<style lang="scss">
.city_select_popover {
  padding: 12px 0 !important;
}
</style>