一、效果图
二、完整代码
<template>
<view>
<picker mode="multiSelector" @change="bindMultiPickerChange" @columnchange="bindMultiPickerColumnChange"
:value="multiIndex" :range="multiArray">
<view class="picker">
{{multiArray[0][multiIndex[0]]}} > {{multiArray[1][multiIndex[1]]}}
</view>
</picker>
</view>
</template>
<script>
export default {
data() {
return {
multiArray: [],
multiIndex: [0, 0],
array: [{
id: 0,
name: '大类1',
children: [{
id: 10,
name: '小类1'
},{
id: 11,
name: '小类11'
}
]
},
{
id: 1,
name: '大类2',
children: [{
id: 20,
name: '小类2'
}]
}
],
oneId: 0,
twoId: 0,
}
},
onLoad() {
this.getType()
setTimeout(() => {
this.initData();
}, 1000)
},
methods: {
initData() {
console.log('111', this.array);
const arrOne = this.array.map(item => {
return item.name;
});
const arrTwo = this.array[0].children.map(item => {
return item.name;
});
this.multiArray[0] = arrOne;
this.multiArray[1] = arrTwo;
this.oneId = this.array[0].id;
this.twoId = this.array[0].children[0].id;
},
bindMultiPickerColumnChange(e) {
console.log(e.detail.column, "e.detail.column表示是第几列表示是第几列")
if (e.detail.column === 0) {
this.initSelect(e.detail.value);
this.multiIndex[0] = e.detail.value;
} else if (e.detail.column === 1) {
this.multiIndex[1] = e.detail.value;
console.log('detailvalue', e.detail.value);
this.twoId = this.array[this.multiIndex[0]].children[this.multiIndex[1]].id;
}
console.log(this.oneId, "打印第一列id");
console.log(this.twoId, "打印第二列id");
},
initSelect(index) {
this.oneId = this.array[index].id;
this.multiIndex[1] = 0;
this.$set(this.multiArray, 1, []);
if (this.array[index].children.length == 0) {
console.log("如果右边长度等于0,那么清掉右边ID");
this.twoId = "";
} else {
const arrTwo = this.array[index].children.map(item => {
return item.name;
});
this.$set(this.multiArray, 1, arrTwo);
this.twoId = this.array[index].children[0].id;
}
},
bindMultiPickerChange(e) {
console.log(this.oneId);
console.log(this.twoId);
},
getType() {
uni.request({
url: 'https:
success: (res) => {
console.log(res.data);
this.array = res.data.types.map((item, index) => {
return {
...item,
children: res.data.goods[index]
}
})
console.log('array', this.array);
}
})
},
}
}
</script>
<style>
.uni-picker-tips {
font-size: 12px;
color: #666;
margin-bottom: 15px;
padding: 0 15px;
}
</style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139