vant 拼音排序 城市列表

template>

  <div>

    <van-index-bar class="indexBar" :sticky="false" highlight-color="#AE853A">

      <van-index-anchor v-for="(item,index) in this.cityData" :key="index" :index=item.id>

       <span class="indexWord"  @click="chooseCity(item)">{{item.first}}</span><br>

         <router-link to="/bibing"><span class="indexWord"  @click="chooseCity(item)">{{item.vlaue}}</span><br></router-link>

       

      </van-index-anchor>

    </van-index-bar>

  </div>

</template>

<script>

import { Toast, Col, Row } from "vant";

import { platform } from "@/api/getData";

 

export default {

  data() {

    return {

      indexList: [],cityData:[],copycityData:[],

      list: {},

      FirstPin: ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"],

    };

  },

  methods: {

    getdate() {

      let arr = [],

        newarr = [],

 

        newArrs = [];

      let data = { types: [1, 8] };

      platform(data).then((res) => {  //axios 封装的方法 从后台拿到的数据

        arr = res.data.data;

        for (let i = 0; i < arr.length; i++) {

          newarr.push({

            vlaue: arr[i].name,

            id: arr[i].id,

          });

        }

          this.indexList = newarr;

          // console.log( this.indexList)

          let firstName = {};

        

          var pinyin = require("pinyin");

         

          console.log("wordName",this.indexList)

          for (let i = 0; i < this.indexList.length; i++) {

            //获取每条数据里的名称

            let wordName = this.indexList[i].vlaue;

            // console.log("wordName",wordName)

            //获取所有名称的首字母,并且大写

            let fristName = pinyin(wordName, {

              style: pinyin.STYLE_NORMAL,

              heteronym: true,

            })[0][0]

              .substring(0, 1)

              .toUpperCase();

            // console.log("fristName",fristName)

            // 进行判断,给原始json数据添加新的键值对

            if (this.FirstPin.includes(fristName) == true) {

              this.indexList[i].first = fristName;

            } else {

              this.indexList[i].first = "#";

            }

            // 选种状态

            this.indexList[i].isSelect = false;

            //放入新的数组中

            newArrs.push(this.indexList[i]);

            // console.log("newArrs",newArrs)

          }

           let wordJson = [];

        for (let i = 0; i < this.FirstPin.length; i++) {

          newArrs.forEach((el) => {

            if (this.FirstPin[i] === el.first) {

              wordJson.push(el);

            }

          });

        }

        this.cityData  = wordJson;

        // if (this.cityData.length > 0) {

        //   this.cityData[0].isSelect = true;  // 默认选种第一条数据

        // }

 

 

        });

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值