How to implement single-select, multiple-select and reverse-select in forms in ReactJS

黄舟
Release: 2018-05-30 09:45:12
Original
1979 people have browsed it

This article mainly introduces examples of single-selection, multi-selection and reverse selection in ReactJS implementation forms. It is of great practical value. Friends in need can refer to it.

This article introduces the single-selection and multi-selection implementation of forms in ReactJS. I would like to share with you examples of reverse selection and hope it will be helpful to you.
The requirement is to implement single selection, reverse selection, multi-selection, and all clear operations on the list

...... this.state = { //初始化空数组,表示已经选择的 selectedStores:[], } ...... handleClick(e){ const newSelection = e.target.value;//拿到点击的具体一项 let newSelectionArray;//新建一个空数组 //判断点击项是否为选择状态,是的话清除选中状态 if(this.state.selectedStores.indexOf(newSelection) > -1) { newSelectionArray = this.state.selectedStores.filter((s:any) => s !== newSelection) } else { //不是的话就加入新选择数组 newSelectionArray = [...this.state.selectedStores, newSelection]; } this.setState({ // 新选择数组统一改为选中状态 selectedStores: newSelectionArray }); }
Copy after login

The Array.prototype.indexOf() method returns the first index where a given element can be found in the array , if it does not exist, returns -1.

Syntax:

arr.indexOf(searchElement) arr.indexOf(searchElement[, fromIndex = 0])
Copy after login

The Array.prototype.filter() method creates a new array containing all elements of the test implemented by the provided function.

Grammar:

var new_array = arr.filter(callback[, thisArg])
Copy after login

The above is the detailed content of How to implement single-select, multiple-select and reverse-select in forms in ReactJS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!