<!-- Score selection pop-up --> <van-popup v-model="showScore" position="bottom" :lazy-render='false'> <van-picker show-toolbar Title="Score selection" :columns="scoreColumns" @cancel="showScore = false" /> </van-popup>
data() { return { showScore: false, scoreColumns: [ { Values: [],//The integer column needs to be set dynamically defaultIndex: 0 }, { values: ['.'], defaultIndex: 0 }, { values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], defaultIndex: 0 }, { values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], defaultIndex: 0 } ] } }, methods: { showScorePopup() { //The code is for display only, and some codes have been omitted. The following variable maxScore is the maximum value obtained dynamically, which may be 10 or 30 this.scoreColumns[0].values = []; // Clear first and then assign values for(let i = maxScore; i >= 0; i --) { //Add all scores to the first column of picker after traversing, for example: [30, 29, 28,..., 2, 1, 0] this.scoreColumns[0].values.push(i); } this.showScore = true; // Display score selection pop-up window } }
Modifying the columns parameter of the picker will not update the view
Use ref to obtain the picker component
<van-picker ref="scoreSelect" />
console.log(this.$refs.scoreSelect);
Solve that the picker component cannot be obtained by using ref in the popup
<van-popup :lazy-render="false" />
//The instance method scoreSelect receives two parameters (columnIndex, values). The first parameter is the index of the column to be modified by the selector, and the second parameter is the value to be modified showScorePopup() { //The code is for display only, and some codes have been omitted. The following variable maxScore is the maximum value obtained dynamically, which may be 10 or 30 // this.scoreColumns[0].values = []; let numArr = []; for(let i = maxScore; i >= 0; i --) { // this.scoreColumns[0].values.push(i); numArr.push(i); } // this.$ refs.scoreSelect.setColumnValues(0, this.scoreColumns[0].values); this.$ refs.scoreSelect.setColumnValues(0, numArr); this.showScore = true; // Display score selection pop-up window }