代码之家  ›  专栏  ›  技术社区  ›  Violet

react native-flatlist单选择

  •  0
  • Violet  · 技术社区  · 6 年前

    我一直在寻找如何在 平面图 但是我找不到任何,在我的代码中,我试图对我的 平面图 .

    例子: 我选择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>
            )
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   SmoggeR_js    6 年前

    这个 TextInputs 目标是同一个州 SplOrder 所以,如果你把它换成 TextInput 其他的将显示相同的内容。我看到的解决方案是为您所创建的每个项设置一个状态。 你应该这样做:

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

    我们在注释中讨论的第二个问题应该通过将索引参数传递给incrementcount函数来解决。

    嗨,现在在方法_increment count()中,您必须传递索引,并使用索引递增每个计数,就像在值中一样。所以你可以

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

    然后更改_incrementcount方法,添加一个参数,如下所示:

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