Three level linkage of small program city

Three level linkage is the most common in the project. For example, users fill in the receiving address and the prize receiving address after winning the prize, all need to be written by three-level linkage. Then, my situation is that the back end doesn't give three-level linkage data. So we need to pick up the data of a three-level linkage city on the Internet. See code for details:

<input class="input" placeholder="{{province}}{{city}}{{county}}" placeholder-style="color:#454545;font-size:22rpx;" focus="{{focus}}" onFocus="open" />
var tcity = require("/utils/city.js");
data: {
      // Three level linkage
      provinces: [],//Provincial Data
      citys: [],//City data
      countys: [],//County data
      province: "",//Name of the page rendering Province
      city: "",//Name of the page rendering City
      county: '',//Name of paged County
      value: [0, 0, 0],
      values: [0, 0, 0],
      condition: false//Three level linkage of control switch
},
onLoad(options) {
      var that = this;
      /*Three level linkage data start*/
      tcity.init(that);
      var cityData = that.data.cityData;
      const provinces = [];
      const citys = [];
      const countys = [];
      for(let i=0;i<cityData.length;i++){
        provinces.push(cityData[i].name);
      }
      for (let i = 0 ; i < cityData[0].sub.length; i++) {
        citys.push(cityData[0].sub[i].name)
      }
      for (let i = 0 ; i < cityData[0].sub[0].sub.length; i++) {
        countys.push(cityData[0].sub[0].sub[i].name)
      }
      that.setData({
        'provinces': provinces,
        'citys':citys,
        'countys':countys,
        'province':cityData[0].name,
        'city':cityData[0].sub[0].name,
        'county':cityData[0].sub[0].sub[0].name
      })
      /*Three level linkage data end*/
 },
 // Handle three-level linkage
    bindChange: function(e) {
        //console.log(e);
        var val = e.detail.value
        var t = this.data.values;
        var cityData = this.data.cityData;
        
        if(val[0] != t[0]){
          const citys = [];
          const countys = [];

          for (let i = 0 ; i < cityData[val[0]].sub.length; i++) {
            citys.push(cityData[val[0]].sub[i].name)
          }
          for (let i = 0 ; i < cityData[val[0]].sub[0].sub.length; i++) {
            countys.push(cityData[val[0]].sub[0].sub[i].name)
          }

          this.setData({
            province: this.data.provinces[val[0]],
            city: cityData[val[0]].sub[0].name,
            citys:citys,
            county: cityData[val[0]].sub[0].sub[0].name,
            countys:countys,
            values: val,
            value:[val[0],0,0]
          })
          
          return;
        }
        if(val[1] != t[1]){
          const countys = [];

          for (let i = 0 ; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
            countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
          }
          
          this.setData({
            city: this.data.citys[val[1]],
            county: cityData[val[0]].sub[val[1]].sub[0].name,
            countys:countys,
            values: val,
            value:[val[0],val[1],0]
          })
          return;
        }
        if(val[2] != t[2]){

          this.setData({
            county: this.data.countys[val[2]],
            values: val
          })
          return;
        }
    },
    open:function(){
      this.setData({
        condition:!this.data.condition
      })
    }

Then this is a json file, which contains the three-level linkage city data picked from the Internet

var cityData = [
	//There are a lot of data on the Internet. Pick it up by yourself	
];
function init(that){
  that.setData( { 
      'cityData': cityData
  });
}

module.exports={
    init:init
}
Published 2 original articles, praised 0, visited 9
Private letter follow

Tags: JSON

Posted on Sun, 12 Jan 2020 09:21:06 -0500 by pl_towers