picker 1级 2级 菜单

微信小程序

19-2-1 16:58:36

1级          使用的方法是后台 传送2个数组  1个id数组 一个名字数组  最终值为bmid

<picker bindchange="bm" value="{{bmid}}" range="{{bmlist}}">
{{bmlist[bm_index]}}
</picker>


bmid: 0, //最终选择id值
bmlist: [], //人员姓名列表
bmidlist: [], //人员id列表
bm_index: 0, //指针

bm: function (e) {
var id = e.detail.value;
var self = this;
console.log('值为', id + "-" + this.data.bmlist[id])
this.setData({
bm_index: id,
bmid: this.data.bmidlist[id]
})
}







2级联动  最终获取值为fjid

fjid:
0,         //最终值
multdata: [],    //后台数据数组
multiIndex: [0, 0], //2个选择中
multiArray: [],      //前台显示数组



<picker mode="multiSelector" value="{{multiIndex}}" range="{{multiArray}}" bindchange="fj" bindcolumnchange="fj2" >
 {{multiArray[0][multiIndex[0]]}} > {{multiArray[1][multiIndex[1]]}}

</picker>


fj: function (e) {
var id1 = e.detail.value[0]
var id2 = e.detail.value[1]
var that = this;
console.log('1picker发送选择改变,携带值为', id1)
console.log('2picker发送选择改变,携带值为', id2)
this.setData({
multiIndex: [id1, id2],
fjid: that.data.multdata[e.detail.value[0]][e.detail.value[1]].id
})

console.log('获取值' + that.data.multdata[e.detail.value[0]][e.detail.value[1]].name + '获取值' + "--" + that.data.multdata[e.detail.value[0]][e.detail.value[1]].id);

},

fj2: function (e) {
var that = this;
var id = e.detail.value;
//console.log(that.data.multdata[id]);
var list
switch (e.detail.column) {
case 0:
list = []
for (var i = 0; i < that.data.multdata[id].length; i++) {
list.push(that.data.multdata[id][i].name)
}
that.setData({
"multiArray[1]": list,
"multiIndex[0]": e.detail.value,
"multiIndex[1]": 0
})

}

// console.log('2picker发送选择改变,携带值为', id );

},
获取网络数据后 需要处理一步
liandong: function (tdata, data) { //1级数据  2级数据

var that = this;
var dl = []; //1级菜单
var dl2 = []; //2级菜单

for (var i = 0; i < tdata.length; i++) {
dl.push(tdata[i].name);
}

for (var i = 0; i < data[0].length; i++) {
dl2.push(data[0][i].name);
}

that.setData({
multiArray: [dl, dl2],
multdata: data
})
}

我的数据格式比较奇怪
1级在一个数组   
[{"id":"1","pid":0,"name":"111"},{"id":"2","pid":0,"name":"2222"},{"id":"3","pid":0,"name":"333"}]
2级数组
[[{"id":"8","name":"101","pid":"1"},{"id":"9","name":"103","pid":"1"},{"id":"11","name":"124","pid":"1"}], [{"id":"30","name":"201","pid":"2"},{"id":"31","name":"202","pid":"2"},{"id":"32","name":"203","pid":"2"}], [{"id":"110","name":"362","pid":"3"},{"id":"111","name":"363","pid":"3"},{"id":"112","name":"364","pid":"3"}]]