现象
循环picker选择器,改变一个下拉框选项,导致全部下拉框选项改变;
问题
怎样操作才能实现只改变当前操作的下拉框的值?
思路
在js中设置一个数组变量,存储每个picker选择器默认的值;然后根据bingchange获取相应的索引、e.detail.value,根据这些更新数组,重新赋值,然后根据
数组的索引、值更新相应的picker索引以及值。
wxml
12 223 21销售品申请 45 146 137 128 { {objectArray[item.option].good_name}} 9 10 1115 19 2016 17 1823 24 继续增加商品 25
js
1 data: { 2 salesList: [] //定义一个空数组 3 }, 4 //销售品删除一行 5 reduceSales: function (e) { 6 var that = this; 7 var index = e.currentTarget.dataset.index; 8 var list = that.data.salesList; 9 if (list.length === 1) {10 return;11 }12 list.splice(index, 1);13 this.setData({14 salesList: list15 })16 },17 //点击添加销售品18 addSales: function (e) {19 var that = this;20 var list = that.data.salesList;21 var r = /^\+?[1-9][0-9]*$/; //正整数 22 var tip = '数量请输入正整数~~';23 for (var i = 0; i < list.length; ++i) {24 if (list[i].nums <= 0 || !r.test(list[i].nums)) {25 wx.showToast({26 title: tip,27 image: '../Image/error.png',28 duration: 200029 });30 return;31 }32 }33 var options = this.data.objectArray34 this.data.salesList.push({ id: options[0].id, nums: '', option: 0 }) //添加新行的时候nums初始为035 this.setData({36 salesList: this.data.salesList37 })38 39 },40 //销售品申请类41 bindPickerChange: function (e) {42 var arr = this.data.objectArray //数组;43 var value = e.detail.value; //数组子集44 var list = this.data.salesList; //数组传递某一项的容器45 var index = e.currentTarget.dataset.index; //索引46 47 list[index] = arr[value] //被选中数组的某一项赋给容器的相对应的某一项容器48 49 list[index].option = value //数组子集赋给某一项相对应的容器索引50 this.setData({51 salesList: list52 })53 },54 //申请数量55 numChangeS: function (e) {56 var _that = this;57 var index = e.currentTarget.dataset.index;58 var list = this.data.salesList59 list[index].nums = e.detail.value60 this.setData({61 salesList: list62 })63 },64 65 // 页面初始化 options为页面跳转所带来的参数66 onLoad: function (options) {67 var that = this68 wx.request({69 url: 'https://....',70 method: 'GET',71 header: { 'content-type': 'application/json' },72 data: {},73 success: function (res) { // success74 var options = res.data.goods;75 that.data.salesList.push({ id: options[0].id, nums: '', option: 0 })76 that.data.materielList.push({ id: options[0].id, nums: '', option: 0 })77 that.data.activesList.push({ id: options[0].id, option: 0 })78 that.setData({79 objectArray: options,80 salesList: that.data.salesList,81 })82 },83 })84 },
参考网址:https://www.cnblogs.com/JinQing/p/6673665.html