省市区三级联动--移动端

随着信息时代的发展,我们已经可以足不出户就建议购买到我们想要的东西,这个想必不需要我多说,大家都知道也切身体会到,在使用这些APP的中不可避免的就是我们的地址,小编在做需求时看到了这一项,所以到插件库找了一下,发现下载是需要money滴,奈何小编是个穷X,没钱购买,所以只能自己动手了。话不多说,代码为重。
html:




    
    
    
    
    


     
      
     
     
     
    


mobileSelect.js

var nameEl = document.getElementById('sel_city');
var first = []; /* 省,直辖市 */
var second = []; /* 市 */
var third = []; /* 镇 */

var selectedIndex = [0, 0, 0]; /* 默认选中的地区 */

var checked = [0, 0, 0]; /* 已选选项 */

function creatList(obj, list){
  obj.forEach(function(item, index, arr){
  var temp = new Object();
  temp.text = item.name;
  temp.value = index;
  list.push(temp);
  })
}

creatList(city, first);

if (city[selectedIndex[0]].hasOwnProperty('sub')) {
  creatList(city[selectedIndex[0]].sub, second);
} else {
  second = [{text: '', value: 0}];
}

if (city[selectedIndex[0]].sub[selectedIndex[1]].hasOwnProperty('sub')) {
  creatList(city[selectedIndex[0]].sub[selectedIndex[1]].sub, third);
} else {
  third = [{text: '', value: 0}];
}

var picker = new Picker({
    data: [first, second, third],
  selectedIndex: selectedIndex,
    title: '地址选择'
});

picker.on('picker.select', function (selectedVal, selectedIndex) {
  var text1 = first[selectedIndex[0]].text;
  var text2 = second[selectedIndex[1]].text;
  var text3 = third[selectedIndex[2]] ? third[selectedIndex[2]].text : '';

    nameEl.innerText = text1 + ' ' + text2 + ' ' + text3;
});

picker.on('picker.change', function (index, selectedIndex) {
  if (index === 0){
    firstChange();
  } else if (index === 1) {
    secondChange();
  }

  function firstChange() {
    second = [];
    third = [];
    checked[0] = selectedIndex;
    var firstCity = city[selectedIndex];
    if (firstCity.hasOwnProperty('sub')) {
      creatList(firstCity.sub, second);

      var secondCity = city[selectedIndex].sub[0]
      if (secondCity.hasOwnProperty('sub')) {
        creatList(secondCity.sub, third);
      } else {
        third = [{text: '', value: 0}];
        checked[2] = 0;
      }
    } else {
      second = [{text: '', value: 0}];
      third = [{text: '', value: 0}];
      checked[1] = 0;
      checked[2] = 0;
    }

    picker.refillColumn(1, second);
    picker.refillColumn(2, third);
    picker.scrollColumn(1, 0)
    picker.scrollColumn(2, 0)
  }

  function secondChange() {
    third = [];
    checked[1] = selectedIndex;
    var first_index = checked[0];
    if (city[first_index].sub[selectedIndex].hasOwnProperty('sub')) {
      var secondCity = city[first_index].sub[selectedIndex];
      creatList(secondCity.sub, third);
      picker.refillColumn(2, third);
      picker.scrollColumn(2, 0)
    } else {
      third = [{text: '', value: 0}];
      checked[2] = 0;
      picker.refillColumn(2, third);
      picker.scrollColumn(2, 0)
    }
  }

});

picker.on('picker.valuechange', function (selectedVal, selectedIndex) {
  console.log(selectedVal);
  console.log(selectedIndex);
});

nameEl.addEventListener('click', function () {
    picker.show();
});

注:
1、picker.js请自行上网查询下载,若需要的同学也可以自己写样式,但小编不推荐,有现成的,为啥不用,还要多此一举呢
2、city.js数据太大,小编就不贴出了,只贴出数据结构,请谅解

var city = [
  {
    "name": "东北地区",
    "sub": [
      {
        "name": "黑龙江",
        "sub":[
        	{"name":"1111"},
        	{"name":"2222"}
        ],
        "type": 0
      },
      {
        "name": "吉林",
         "sub":[
         	{"name":"333"}
         ],
        "type": 0
      },
      {
        "name": "辽宁",
        "sub":[],
        "type": 0
      }
    ],
    "type": 1
  }
  ]

效果图:
省市区三级联动--移动端_第1张图片

你可能感兴趣的:(常用插件)