index.vue 3.73 KB
<template>
  <div class="nationality_container">
    <NafmHeader :title="$t('title.nationality')" navClass="nafmii-head-bg3">
      <template #bottom>
        <SearchInput v-model="keyword"  :placeholder="$t('person.country')" @search="handleSearch" />
      </template>
    </NafmHeader>
    <div class="search_content" ref="searchContent" v-show="keyword">
      <ul>
        <li class="search_item" v-for="(item,index) of list" :key="index" @click="handleCountryClick(item.cityName)">{{item.cityName}}</li>
        <li class="search_item" v-show="hasNoData">没有搜索到匹配内容</li>
      </ul>
    </div>
    <div class="city-area" ref="cityarea">
      <van-index-bar :sticky="false" @select="handleSelect">
        <div class="city-area-list" v-for="(item,key) in countryList" :key="key">
          <van-index-anchor :index="key">{{key}}</van-index-anchor>
          <van-cell v-for="(innerItem,Index) in item " :key="Index" :title="innerItem.cityName" @click="handleCountryClick(innerItem.cityName)"></van-cell>
        </div>
      </van-index-bar>
    </div>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
import SearchInput from '@/components/SearchInput.vue'
import data from './city.json'
export default {
  name: 'Nationality',
  components: {
    SearchInput
  },
  data() {
    return {
      keyword: '',
      list: [],
      countryList: {},
    }
  },
  computed: {
    hasNoData() {
      return !this.list.length
    }
  },
  watch: {
    keyword() {
      // console.log('keyword', this.keyword)
      if (!this.keyword) {
        this.list = []
        return false
      }
      const result = []
      for (const i in this.countryList) {
        this.countryList[i].forEach(value => {
          if (value.cityName.indexOf(this.keyword) > -1 || value.cityPinyin.indexOf(this.keyword) > -1) {
            result.push(value)
          }
        })
      }
      this.list = result
    },
    countryList() {
      setTimeout(() => {
        this.$refs.cityarea.style.height = document.documentElement.clientHeight - this.$refs.cityarea.getBoundingClientRect().top - 10 + "px";
        this.InsertElementBuildIos();
      }, 1000)
    }
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.searchContent, {
      click: true
    })
    this.countryList = data.list[0]
  },
  methods: {
    //修改a-z列表在ios中fixed未脱离文档流bug
    InsertElementBuildIos() {
      let li = document.querySelector(".nationality_container");
      let el = document.querySelector(".van-index-bar__sidebar");
      li.insertBefore(el, li.firstChild);
    },
    jumpPage() {

    },
    // 字母选择
    handleSelect(value) {
      let cityDom = document.querySelector(".city-area");
      cityDom.scrollTop += 5;
    },
    //
    handleCountryClick(value) {
      console.log('value', value)
      // 接口请求 返回上页
      this.$router.back()
    },
    handleSearch(v){
      console.log('handleSeach',v)
    }
  }
}
</script>
<style lang="less" scoped>
@import "~@/assets/css/search.less";
.nationality_container {
  height: 100vh;
}
.search_content {
  overflow: hidden;
  position: absolute;
  top: 94px;
  left: 0;
  right: 0;
  bottom: 0;
  background: #f7f7f7;
  z-index: 9;
  .search_item {
    line-height: 35px;
    padding-left: 35px;
    border-bottom: 1px solid #efefef;
    background: #fff;
    color: #666;
  }
}
.city-area {
  overflow: hidden;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
}
/deep/ .van-index-bar__sidebar {
  top: 55%;
  right: 10px;
}
/deep/ .van-index-bar__index {
  font-size: 10px;
  width: 15px;
  height: 15px;
  text-align: center;
  margin-bottom: 2px;
  padding: 0;
}
/deep/ .van-index-bar__index--active {
  color: #fff;
  background: #5594e5;
  padding: 2px;
  border-radius: 50%;
}
</style>