【问题标题】:How to implement checkbox in FlatList React Native如何在 FlatList React Native 中实现复选框
【发布时间】:2020-06-24 18:07:13
【问题描述】:

我在从后端获取数据 API 时尝试实现一个复选框,但我遇到的问题是所有复选框都已选中,我无法取消选中它,以及如何通过选中的复选框作为下一个组件的参数。 我希望我能得到一些帮助。

这是我当前代码中的内容;

 class Gifts extends Component {
    constructor(props){
      super(props);
      this.state={
        users:'',
        checked: {},
        selected: 0
       
      }
    }

句柄复选框的API代码

  handleChange = (index) => {
    let { checked } = this.state;
    checked[index] = !checked[index];
    this.setState({ checked });
  }

  onPress(value) {
    this.setState({ selected: value });
}

  render() {
    let { navigation } = this.props
        return (

            <SafeAreaView style={{flex:1 }}>
            <View style={{ flex:1,justifyContent: 'center'}}>
              
               //...codes..//

                    <View style={styles.userlist}>
                    <FlatList
                            data={this.state.users}
                            keyExtractor={(item ,index) => index.toString()}
                            
                            renderItem={({ item }) => (
                                <FlatList
                                  data={item.friendList}
                                  renderItem={({ item }) => 
                                  <View style= {{flexDirection:'row', justifyContent:'space-between'}}>
                                  <Text style={{marginTop:20, marginLeft:10, fontSize: 20}}>{item.name}</Text>
                                  <CheckBox
                                      center
                                      checkedIcon='dot-circle-o'
                                      uncheckedIcon='circle-o'
                                      checked={this.state.checked}
                                      value={ this.state.checked[item.flag]}
                                      onPress={this.onPress}
                                    /> 
                  
                              </View>
                                
                                }
                                  keyExtractor={(item ,index) => index.toString()}
                                  ItemSeparatorComponent ={this.ItemSeparator}
                                />
                            )}
                    />
                    </View>

                  
                  <Button 
                  rounded
                  // disabled={true}  
                  //onPress={}
                  style={{width: 100, justifyContent: 'center',marginLeft:150}}
                  >
                    <Text>Send</Text>
                  </Button>

                </View>

        </SafeAreaView>
                                
              
    );
  }
}

【问题讨论】:

    标签: android ios react-native mobile-application mobile-development


    【解决方案1】:

    您的代码的问题是您对所有复选框都使用了一个 this.state.checked,这很难操作。

    一种更简单的方法是将标志 checked(及其值)集成到循环的 FlatList 数据中,在您的示例中,您将拥有类似:this.state.users[i].friendList[j].checked = true/false

    因此,对于每个元素,您都可以控制选中的值。 您必须将 onPress 函数更改为 setState 元素已选中/未选中

    编辑解决方案可能是:

    onPress 函数(您还必须为每个朋友使用checked=false 初始化您的用户对象):

    onPress(indexUser, indexFriend, value) {
        const { users } = this.state;
    
        for (let i = 0; i < users.length; i++) {
          if (i === indexUser) {
            for (let j = 0; j < users[i].friendList.length; j++) {
              if (j === indexFriend) {
                const bool = users[i].friendList[j].checked;
                users[i].friendList[j].checked = !bool;
                users[i].friendList[j].value = value;
              }
            }
          }
        }
        this.setState({ users });
      }
    

    复选框组件(你有嵌套的平面列表,也许你可以做一些重构):

    <CheckBox
        center
        checkedIcon='dot-circle-o'
        uncheckedIcon='circle-o'
        checked={item.checked}
        value={item.value}
        onPress={()=>{ this.onPress(indexUser, indexFriend, value) }}
    /> 
    

    【讨论】:

    • 也许你能告诉我怎么做?我在这里对这部分感到困惑。欣赏它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2018-04-18
    • 2019-06-17
    • 2018-12-26
    相关资源
    最近更新 更多