Home  >  Q&A  >  body text

javascript - How does the small program picker get the value of objArray?

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. . . .

阿神阿神2674 days ago1178

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:56:51

    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
        })
      }
    })

    reply
    0
  • ringa_lee

    ringa_lee2017-06-26 10:56:51

    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

    reply
    0
  • Cancelreply