zoukankan      html  css  js  c++  java
  • 关于城市遍历

    vue项目记录点代码。。。。。

    1.通过获取后台数据对城市进行分组(根据首字母分组,后台返回数据中带有小写字母)

        postData() { /* 接口方法 */
                    let _self = this;
                
                    axios({
                        url: '接口地址',
                        data: {},
                        headers: {
                        
                        },
                        target: true,
                    }, (res) => {
                        let resData = JSON.parse(res.data)
                        _self.locationCity = resData.locationCity;
                        let cities = JSON.parse(resData.cities);
                        _self.citiesData = cities.data;
                        _self.cityArry = _self.selectCity(cities.data);
                    }, (err) => {
    
                    });
                },
    2._self.selectCity(cities.data)方法对数据分组

        selectCity(cities) {
                    let LetterArryData = [];
                    let Letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    let LetterArry = Letter.split('');
                    for(var j = 0; j < LetterArry.length; j++) {
                        let List = j;
                        List = {
                            name: [],
                            letter: LetterArry[j]
                        };
                        LetterArryData.push(List)
                    }
    
                    for(var i = 0; i < cities.length; i++) {
                        for(var j = 0; j < LetterArry.length; j++) {
                            if(cities[i].tag.toUpperCase() == LetterArryData[j].letter) {
                                LetterArryData[j].name.push(cities[i])
                            }
                        }
                    }
    
                    this.results = LetterArryData;
                    return LetterArryData;
                },

    3.本地匹配搜索城市

    getResult(val) { 
                    if(/^[x00-xff]/.test(val[0])) { /*首位是字母*/
                        if(!!val) {
                            this.cityArry = [];
                            this.results.map((data) => {
                                if(data.letter == val.toUpperCase()[0]) {
                                    this.cityArry.push(data)
                                }
                            })
                        } else {
                            this.cityArry = this.results;
                        }
                    } else { /*首位非字母*/
                        this.cityArry = [];
                        let List = {
                            name: [],
                            letter: ''
                        };
                        this.citiesData.map((data) => {
                            if(val.length == 1) {
                                if(data.city[0].indexOf(val) > -1) {
                                    List.name.push(data);
                                    List.letter = data.tag.toUpperCase()
                                }
                            } else {
                                if(data.city.indexOf(val) > -1) {
                                    List.name.push(data);
                                    List.letter = data.tag.toUpperCase()
                                }
                            }
    
                        })
                        this.cityArry.push(List)
                    }
                },
     
  • 相关阅读:
    day-7
    Redis数据库 : 基础
    MongoDB与python交互
    MongoDB数据库 : 管道,用户管理,副本集等
    MongoDB数据库 : 基础
    MySQL数据库 : 自关联,视图,事物,索引
    MySQL数据库 : 查询语句,连接查询及外键约束
    MySQL数据库 : 基本语句
    数据结构与算法 : 树与遍历
    python__标准库 : 测试代码运行时间(timeit)
  • 原文地址:https://www.cnblogs.com/zzp5980/p/8876094.html
Copyright © 2011-2022 走看看