微信小程序下拉滾動選擇器picker綁定數據的兩種方式 本地數據綁定和wx.request(OBJECT) json數據綁定

1.本地數據綁定 (對象數組)
Page({
data:{
pic_array: [
{ id: 13, name: '1室1廳1衛' },
{ id: 14, name: '1室2廳1衛' },
{ id: 15, name: '2室1廳1衛' },
{ id: 16, name: '3室1廳2衛' },
{ id: 17, name: '4室1廳2衛' },
{ id: 18, name: '5室1廳3衛' },
{ id: 19, name: '6室1廳3衛' },
{ id: 20, name: '7室以上' },
],
hx_index: 0;
},
bindPickerChange_hx: function (e) {
console.log('picker發送選擇改變,攜帶值為', e.detail.value);
this.setData({
hx_index: e.detail.value,
})
console.log('自定義值:', this.data.hx_select);
},
})
<picker name="picker_hx" class="cybm_pic_1" value="{{pic_array[hx_index].id}}" data-selecthx="{{pic_array[hx_index].name}}" range="{{pic_array}}" range-key="{{'name'}}" bindchange="bindPickerChange_hx" >
<view class="picker" >
戶型: {{pic_array[hx_index].name}} //指定數組中指定下標的name鍵對應的值
view>
picker>
屬性名range 類型Array/Object Array 存放你的本地數據數組或者對象數組,需要加載的數據
屬性名range-key 類型String 當 range 是一個 二維Object Array 時,通過 range-key 來指定 Object 中 key 的值作為選擇器顯示內容
屬性名value 類型Array value 每一項的值表示選擇了 range 對應項中的第幾個(下標從 0 開始)
屬性名data- 類型自定義屬性后更的屬性名字可以自定義 當你需要設置其他值得時候可以使用 可選
2.網絡請求得到的json數據綁定下拉選擇器
首先得到后臺傳過來的json數據
data:{
}
onLoad: function () {
var that = this;
wx.request({
url: "https://www.************",
data: {
a: ""
},
header: {
"Content-Type": "applicatiSon/x-www-form-urlencoded"
},
method: "POST",
success: function (res) {
that.setData({
pic_array: res.data.data.exp_hx,
}) } }) }
//綁定的方式一樣,只是改動一下變量名既可以了,這是比較簡單的方式
<picker name="picker_hx" class="cybm_pic_1" value="{{pic_array[hx_index].id}}" data-selecthx="{{pic_array[hx_index].name}}" range="{{pic_array}}" range-key="{{'name'}}" bindchange="bindPickerChange_hx" >
<view class="picker" >
戶型: {{pic_array[hx_index].name}} //指定數組中指定下標的name鍵對應的值
view>
picker>
|