从React Native中的数组中绑定Picker.Item'label'

时间:2018-04-22 10:34:41

标签: javascript react-native picker

我是新手,想知道如何从数组中挑选'Picker.Item'的'label'。我有一个数组'Account []',它的每个元素都是'AccountSchema'的对象,如下所示 下拉列表代码

<Picker
style={{width:'70%'}}
selectedValue={this.state.PickerValue}
onValueChange={(itemValue,itemIndex) =>this.setState({PickerValue:itemValue})}>
                   <Picker.Item label="Select Name of A/C" value=""/>
                   <Picker.Item label="Salary A/C" value="salary" />
                   <Picker.Item label="Rent A/C" value="Rent"/>
</Picker>

数据库(架构)&amp;获取所有帐户的方法

export const AccountSchema = {
name: ACCOUNT_SCHEMA,
primaryKey: 'id',
properties: {
    id: 'int',    // primary key
    name: { type: 'string', indexed: true },
    type: 'string',
    balance: 'int',
    note:'string',
    creationDate: 'date',
}
};

export const queryAllAccounts = () => new Promise((resolve, reject) => {
    Realm.open(databaseOptions).then(realm => {
        let allAccounts = realm.objects(ACCOUNT_SCHEMA);
        resolve(allAccounts);
    }).catch((error) => {
         reject(error);
    });;
});

在下拉列表中,我想显示帐户名称,因此必须在'label'foreach中显示'account.name'。

1 个答案:

答案 0 :(得分:1)

您可以将数据映射到Picker.Item组件:

  <Picker
    style={{ width: '70%' }}
    selectedValue={this.state.PickerValue}
    onValueChange={(itemValue, itemIndex) => this.setState({ PickerValue: itemValue })}
  >
    {accounts.map(acct => <Picker.Item key={acct.id} label={acct.name} value="" />)}
  </Picker>;