项目中遇到只需要选择年月,找到一个三级联动的例子改的,保存一下
WXML
{{multiArray[0][multiIndex[0]]}}{{multiArray[1][multiIndex[1]]}}
JS
data: {
objectMultiShow:[],
objectMultiArray:[],
multiArray: [],
multiIndex: [],
multiIndex:[1,4],//默认显示的内容
},
onLoad: function (options) {
let data = {
objectMultiShow: this.data.objectMultiShow,
objectMultiArray: this.data.objectMultiArray,
multiArray: this.data.multiArray,
multiIndex: this.data.multiIndex, }
data.objectMultiArray = [
[{ id: 0, name: '2018年' }, { id: 1, name: '2019年' }],
[
{ id: 0, name: '1月', parentId: 0 }, { id: 1, name: '2月', parentId: 0 }, { id: 2, name: '3月', parentId: 0 }, { id: 3, name: '4月', parentId: 0 }, { id: 4, name: '5月', parentId: 0 }, { id: 5, name: '6月', parentId: 0 }, { id: 6, name: '7月', parentId: 0 }, { id: 7, name: '8月', parentId: 0 }, { id: 8, name: '9月', parentId: 0 }, { id: 9, name: '10月', parentId: 0 }, { id: 10, name: '11月', parentId: 0 }, { id: 11, name: '12月', parentId: 0 },
{ id: 0, name: '1月', parentId: 1 }, { id: 1, name: '2月', parentId: 1 }, { id: 2, name: '3月', parentId: 1 }, { id: 3, name: '4月', parentId: 1 }, { id: 4, name: '5月', parentId: 1 }
],
]
data.objectMultiShow = data.objectMultiArray.map((item, index) => {
if (index > 0) {
item = item.filter(i => i.parentId === data.objectMultiArray[index - 1][0].id)
}
return item
})
data.multiArray = data.objectMultiShow.map(item => {
item = item.map(i => i.name)
return item
})
console.log(data.multiIndex)
// 数据更新
this.setData(data)
},
bindMultiPickerChange: function (e) {
console.log('picker发送选择改变,携带值为', e.detail.value)
this.setData({
multiIndex: e.detail.value
})
},
bindMultiPickerColumnChange: function (e) {
console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
// 初始化数据
var data = {
objectMultiShow: this.data.objectMultiShow,
multiArray: this.data.multiArray,
multiIndex: this.data.multiIndex
};
// 改变第i列数据之后,后几列选择第0个选项(重置)
data.multiIndex[e.detail.column] = e.detail.value;
for (let i = e.detail.column; i < data.multiIndex.length - 1; i++) {
data.multiIndex[i + 1] = 0
}
let arry = this.data.objectMultiArray
for (let i = e.detail.column; i < data.multiIndex.length - 1; i++) {
data.objectMultiShow[i + 1] = arry[i + 1].filter(item => item.parentId === data.objectMultiShow[i][data.multiIndex[i]].id)
data.multiArray[i + 1] = data.objectMultiShow[i + 1].map(item => item.name)
}
// 数据更新
this.setData(data);
},