适合移动端的省市区县选取器

作者:月光光 2017年06月03日 09:27helloweba.com 标签:移动端 

移动端的省市区县的多级联动如何选取呢?今天给大家介绍一款非常流畅的非常实用的移动端省市区县多级选取器组件,它不需要第三方插件,使用原生纯Javascript实现。

HTML

我们只需在页面中放置一个可用来选取的元素,如:

<div id="sel_city">点击选取省市区县</div>

然后,导入两个js,一个是picker.min.js组件,一个是全国省市区县数据city.js。

<script src="js/picker.min.js"></script>
<script src="js/city.js"></script>

我们使用的组件叫picker,项目网址:https://github.com/ustbhuangyi/picker,你也可以git此项目,然后使用npm构建,然后导入picker。

Javascript

picker支持单列和多列选取,我们现在使用的是三列选取,定义好省市区县3个数组,然后调用picker,并侦听picker事件,完成选取效果。

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();
});

选项

title (String)

筛选器标题,默认为空。

data (Array)

筛选器的数据,是一个二维数组,第一维表示多少列数据,第二维表示每列的数据,单个数据是一个 object,由 text 和 value 两个字段组成,text 表示显示在筛选器的文本,value 表示数据的值。

selectedIndex (Array)

筛选器初始化默认选择的数据索引,是一个二维数组,第一维表示列的序号,第二维表示每列的行号,从 0 开始。

事件

picker.change

当一列滚动停止的时候,会派发 picker.change 事件,同时会传递列序号 index 及滚动停止的位置 selectedIndex。

picker.select

当用户点击确定的时候,会派发 picker.select 事件,同时会传递每列选择的值数组 selectedVal 和每列选择的序号数组 selectedIndex。

picker.cancel

当用户点击取消的时候,会派发picker.cancel事件。

picker.valuechange

当用户点击确定的时候,如果本次选择的数据和上一次不一致,会派发 picker.valuechange 事件,同时会传递每列选择的值数组 selectedVal 和每列选择的序号数组 selectedIndex。

方法

show (next)

显示筛选器,next 为筛选器显示后执行的回调函数。

hide ()

隐藏筛选器,一般来说,筛选器内部已经实现了隐藏逻辑,不必主动调用。

1条评论