Warm tip: This article is reproduced from serverfault.com, please click

multipleselection-如何在React Native中显示从列表中选择的多个项目

(multipleselection - how to show multiple selected item from list in react native)

发布于 2020-11-28 07:25:04

列表中没有项目被选中,但是我想选择所有三个标记的项目,如何从列表中标记选定的项目=?

const List = [
    {
        options: [{ opt: 'a', oid: '1' }, { opt: 'b', oid: '2' }, { opt: 'c', oid: '3' }, { opt: 'd', oid: '4' },
        ],
        marked: [1, 2, 3],
    }
]

const [index, setIndex] = useState(0);
const qstn = List[index];

{qstn.options.map((x, id) =>
                            <Option
                                value={x.opt}
                                selected={qstn.marked === id} // here i am selecting items from marked
                                key={id}
                            />
                        )}

export function Option({ value, selected }) {
    return (
        <TouchableWithoutFeedback>
            <View style={styles.option}>
                <View style={{ flex: 8, backgroundColor:selected? '#b8de6f' : '#fff' }}>
                    <Text style={{ fontSize: 15, fontFamily: 'OpenSans-Regular', }}>{value}</Text>
                </View>
            </View>
        </TouchableWithoutFeedback>
    )
}
Questioner
Shivam
Viewed
0
Leri Gogsadze 2020-11-28 15:54:23

selected={qstn.marked === id}我认为你在这里犯了一个错误,因为id是一个数字,而qstn.marked是一个数组。如果要检测该ID是否在你的数组中,请将此行更改为 qstn.marked.indexOf(id) !== -1