1、原始的写法以及问题
<van-popup show="{{ showPopup && cellClick === 'Freq' }}" position="bottom" bind:close="onPopupClose">
<van-picker value-key="Spec" show-toolbar title="{{cellClick === 'Freq' ? showPcChangeTitle : ''}}" columns="{{ cellClick === 'Freq' ? pcColumns : hcColumns }}" default-index="{{hcDefaultIndex}}" bind:cancel="onPickerCancel" bind:confirm="onPickerConfirm" bind:change="onPickerChange" />
</van-popup>
之前用 popup 结合现实 picker 显示默认值
在 onload 的时候修改默认值
const index = this.data.hcColumns.findIndex(s => s.Id === item.PrescriptionConsumableInputDtos[0].ConsumableId)
this.setData({
showHcName: this.data.hcColumns.filter(s => s.Id === item.PrescriptionConsumableInputDtos[0].ConsumableId)[0].Name,
hcDefaultIndex: index
})
结果发现虽然hcDefaultIndex的值确实改变了 但是实际上页面上并没有显示为正确的选项
具体可以看 GitHub 上的一些评论单列picker,设置default-index,不起作用 · Issue #1636 · youzan/vant-weapp · GitHub
2、解决办法
在van-popup上加一个wx:if就可以了(单列和多列都是相同的修改办法)
<van-popup wx:if="{{ showPopup && cellClick === 'Freq' }}" show="{{ showPopup && cellClick === 'Freq' }}" position="bottom" bind:close="onPopupClose">
<van-picker id="freqPicker" show-toolbar title="{{showPcChangeTitle}}" columns="{{pcColumns}}" bind:cancel="onPickerCancel" bind:confirm="onPickerConfirm" bind:change="onPickerChange" />
</van-popup>