【问题标题】:Multiple checkbox in react native反应原生的多个复选框
【发布时间】:2019-01-30 07:46:11
【问题描述】:

如何在 react native <CheckBox></CheckBox> 中制作多个复选框?

目前我的复选框是这样的:

if(this.state.specialise_list != null){
  return this.state.specialise_list.map((data, i)=>(
    <View key={i} style={{ flexDirection: 'row' }}>
      <CheckBox
        containerStyle={{ borderColor: 'transparent' }}
        value={this.state.specialise}
        onValueChange={(itemValue, itemKey) => this.setState({ specialise: itemValue })} />
      <Text style={{marginTop: 5}}> { data.name }</Text>
    </View>
  ))
}

我上面的代码将检查所有的复选框。我正在尝试以selected_specialise: [ '1', '2', '5', '24' ]; 的形式将选中的复选框插入到数组中,如果未选中特定的复选框,数据将从数组中删除。

【问题讨论】:

    标签: arrays react-native checkbox


    【解决方案1】:

    您只需检查 selected_specialise 数组是否包含索引。

    if(this.state.specialise_list != null){
      return this.state.specialise_list.map((data, i)=>(
        <View key={i} style={{ flexDirection: 'row' }}>
          <CheckBox
            containerStyle={{ borderColor: 'transparent' }}
            value={this.state.selected_specialise.includes(i)}
            onValueChange={(itemValue, itemKey) => this.setState({ specialise: itemValue })} />
          <Text style={{marginTop: 5}}> { data.name }</Text>
        </View>
      ))
    }
    

    如果您的意思是 1 作为第一个,那么 ..includes(i + 1)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多