javascript - How does the small program picker get the value of objArray?
阿神
阿神 2017-06-26 10:54:51
0
2
1172

Small program picker component, you can see that there is a type of objArray in the demo, but it is not used. Now I have a set of objArray that I need to use, but it cannot be displayed normally. Please help me take a look:

Mini program demo link: picker

wxml:

<picker bindchange="bindPickerChange" value="{{index}}" range="{{objectArray}}">
    <view class="picker">
      当前选择:{{objectArray[index]}}
    </view>
</picker>

js:

Page({
  data: {
    objectArray: [
      {
        id: 0,
        name: '美国'
      },
      {
        id: 1,
        name: '中国'
      },
      {
        id: 2,
        name: '巴西'
      },
      {
        id: 3,
        name: '日本'
      }
    ]
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  }
})

In this case, the displayed drop-down option is [object Object], and the same is displayed on the page after selection. Now I want the drop-down list to display the value in name, and then know the selected id. I really don’t know how to achieve it. . . .

阿神
阿神

闭关修行中......

reply all(2)
我想大声告诉你

It should be possible to use this attribute. The modified code is as follows:

<picker bindchange="bindPickerChange" value="{{index}}" range-key="name" range="{{objectArray}}">
    <view class="picker">
      当前选择:{{objectArray[index].name}}
    </view>
</picker>

Update1:

Page({
  data: {
    objectArray: [
      {
        id: 0,
        name: '美国'
      },
      {
        id: 1,
        name: '中国'
      },
      {
        id: 2,
        name: '巴西'
      },
      {
        id: 3,
        name: '日本'
      }
    ]
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    var index = e.detail.value;
    var currentId = this.data.objectArray[index].id; // 这个id就是选中项的id
    this.setData({
      index: e.detail.value
    })
  }
})
ringa_lee

Add range-key='obj.item', for example

<picker bindchange="bindPickerChange" value="{{index}}" range-key="name" range="{{objectArray}}">
    <view class="picker">
      当前选择:{{objectArray[index].name}}
    </view>
</picker
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!