如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
实现
我们要实现的目的是 form的数据是后台加载的,那么我们就要从onload处获取数据
代码
小程序源码:
index.js
Page({ data: { form:[] }, onLoad:function(option){ console.log(1) var form = [ { 'name':'名字', 'type':'input', 'field':'name', 'val':'' }, { 'name':'类型', 'type':'dx', 'field':'dx', 'val': [0, 0, 0], 'multiArray': [['无脊柱动物', '脊柱动物'], ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'], ['猪肉绦虫', '吸血虫']], }, ] this.setData({ form:form }) }, bindMultiPickerChange: function (e) { console.log('picker发送选择改变,携带值为', e.detail.value) var index = 1 let temp = 'form[' + index +'].val' // 获取list[index].date this.setData({ [temp]: e.detail.value }) console.log(this.data.form) }, bindMultiPickerColumnChange: function (e) { console.log('修改的列为', e.detail.column, ',值为', e.detail.value); // 知道修改的列以后,就可以通过修改multiIndex中对应元素的值,然后再修改multiArray } })
index.wxml
<view> <!-- 循环 --> <block wx:for="{{form}}" wx:for-index="index1" wx:for-item="item1"> <view>{{item1.name}}--{{item1.type}}--{{item1.field}}--{{item1.val}}</view> <!-- //input --> <view wx:if="{{item1.type=='input'}}"> {{item1.name}}:<input name="{{item1.field}}" value="{{item1.val}}" placeholder="请输入{{item1.name}}"/> </view> <!-- 多筛选 --> <view class="picker-box" wx:if="{{item1.type=='dx'}}"> <view class='skill-picker'> <picker mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange" value="{{item1.val}}" range="{{item1.multiArray}}" data-iid="{{index1}}"> <view class="skill-title"> {{item1.multiArray[0][item1.val[0]]}}, {{item1.multiArray[1][item1.val[1]]}}, {{item1.multiArray[2][item1.val[2]]}} </view> </picker> </view> </view> </block> </view>
index.wxss
.picker-box { margin-top: 0rpx; width: 100%; position: fixed; top: 0; } .skill-picker { height: 80rpx; background-color: rgba(0, 121, 132, 0.8); box-shadow: 0rpx 5rpx 16rpx rgba(0, 0, 0, 0.05); } .skill-title { line-height: 80rpx; text-align: center; vertical-align: middle; color: white; font-size: 36rpx; font-weight: bold; } .drop-down { position: fixed; top: 0; right: 0; margin-top: 18rpx; margin-right: 48rpx; display: inline-block; width: 48rpx; height: 48rpx; vertical-align: middle; }
ok