【问题标题】:React Native - Flatlist Single SelectReact Native - Flatlist 单选
【发布时间】:2018-12-26 01:32:45
【问题描述】:

我一直在寻找如何在 Flatlist 中进行单选,但我找不到,在我的代码中,我试图在每个单元格上进行单选在我的Flatlist中。

示例:我选择了 1 号单元格,那么 1 号单元格将被选中。如果我需要选择 2 号,那么 1 号和 2 号都会被选中。

我的代码中发生的情况是,当我选择 1 号时,它会选择所有单元格。

export default class Dishes extends Component {
    constructor(props) {
        super (props)
        this.state = {
            data: [],
            id: [],
            price: [],
            count: 0,
            SplOrder: '',
            tbl: this.props.navigation.state.params.tbl,
            orderDet: this.props.navigation.state.params.orderDet,
            DineIn: this.props.navigation.state.params.DineIn,
            TakeOut: this.props.navigation.state.params.TakeOut,
        }
    }

/********************EDIT*************************
_incrementCount() {
    this.setState({ count: this.state.count + 1 });
}
_decreaseCount() {
    this.setState({ count: this.state.count - 1 });
}
changeTextHandler() {
    this.setState({ ['count'+index]: text });
};
*/

    _renderItem = ({ item, index }) => {
        return (
            <View>
                <View>
                    <View>
                        <Text>Name: { item.menu_desc }</Text>
                            <View}>
                                <Text>Price: ₱{ item.menu_price }</Text>
                                <Text>Status: { item.menu_status }</Text>
                            </View>
                        <TextInput
                            placeholder = "Insert Special Request"
                            onChangeText = { (text) => this.setState({ SplOrder: text }) }
                            value = { this.state.SplOrder }
                        />
                    </View>
                    <View>
                        <TouchableOpacity
                            onPress = {() => this._incrementCount()}>
                            <Text> + </Text>
                        </TouchableOpacity>
                        <TextInput
                            onChangeText={this.changeTextHandler}
                            value={this.state['count'+index].toString()}    // Not working
                            placeholder="0"/>
                        <TouchableOpacity
                            onPress = {() => this._decreaseCount()}>
                            <Text> - </Text>
                        </TouchableOpacity>
                    </View>
                </View>
            </View>
        )
    }

    render() {
        return (
            <View>
                <View>
                    <Text>Table No: { this.state.tbl }</Text>
                    <Text>Order No: { this.state.orderDet }</Text>
                    <Text>{ this.state.DineIn }{ this.state.TakeOut }</Text>
                </View>

                <FlatList
                data = {this.state.data}
                keyExtractor={(item, index) => index.toString()}
                extraData={this.state}
                renderItem = {this._renderItem}
                />
                <View>
                    <TouchableOpacity
                    onPress = {() => this.submit()}>
                        <Text>Send Order</Text>
                    </TouchableOpacity>
                </View>
            </View>
        )
    }
}

【问题讨论】:

    标签: javascript android reactjs react-native mobile


    【解决方案1】:

    TextInputs 的目标是相同的状态 SplOrder,因此,如果您在任何 TextInput 中更改它,其他状态将显示相同。我看到的解决方案是为您创建的每个项目设置一个状态。 你应该这样做:

                        <TextInput
                            placeholder = "Insert Special Request"
                            onChangeText = { (text) => this.setState({ ['SplOrder'+index]: text }) }
                            value = { this.state['SplOrder'+index] }
                        />
    

    我们在 cmets 中讨论的第二个问题应该通过将 index 参数传递给 incrementCount 函数来解决。

    您好,现在在 _incrementCount() 方法中,您将必须传递索引并使用索引增加每个计数,就像您在值中所拥有的一样。所以你可以这样做

    <TouchableOpacity
        onPress = {() => this._incrementCount(index)}>
        <Text> + </Text>
    </TouchableOpacity>
    

    然后更改您的 _incrementCount 方法,添加一个参数并这样做:

    _incrementCount(index) {
        this.setState({ ['count'+index]: this.state['count'+index] + 1 });
    }
    

    【讨论】:

    • 这适用于一个文本输入,但不适用于我的其他文本输入先生。我会更新我的代码。
    • 告诉我你得到了什么!无论如何,我猜它应该只是按照我给你的建议玩。
    • 先生,我已经更新了我的代码,您将它们放在 cmets 中。是的,我按照你之前所说的做了,但是你看,我的第二个文本输入与第一个有点不同。 (我指的是带有 + 和 - 的文本输入)
    • 我按照你说的做了,但没有奏效,先生,我也尝试将“value={this.state['count'+index].toString()}”放入我的值中,因为没有“ .toString() ” 我收到警告说预期的字符串。你觉得我的 changeTextHandler 怎么样?
    • TextInput 的 onChangeText 属性错误。它应该像 ... onChangeText = {(text) => this.更改文本处理程序(文本)} ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多