简体   繁体   English

如何使用复选框列表中的选定值创建数组?

[英]How to create an array with the selected values from a checkbox list?

I have a list that renders some products that are divided into some categories.我有一个列表,其中显示了一些分为某些类别的产品。

It is possible to select these categories through a checkbox.可以通过复选框选择这些类别。 Would you like to know how to create an array with the list of selected values in the checkbox?您想知道如何使用复选框中的选定值列表创建数组吗?

Here is my code I put into codesandbox :这是我放入codeandbox 的代码:

I'm getting the value of the last selected filter, I'm not being able to create an array with all the selected values:我正在获取最后一个选定过滤器的值,但无法创建包含所有选定值的数组:

在此处输入图片说明

 import Divider from "@material-ui/core/Divider"; import List from "@material-ui/core/List"; import ListItem from "@material-ui/core/ListItem"; import ListItemText from "@material-ui/core/ListItemText"; import Checkbox from "@material-ui/core/Checkbox"; import React, { useState } from "react"; import data from "../data"; import { useStyles } from "./styles"; const DrawerComponent = () => { const classes = useStyles(); const [activeFilter, setActiveFilter] = useState([]); const handleChange = (text) => (event) => { setActiveFilter((prev) => ({ ...prev, value: event.target.checked, text })); }; console.log("activeFilter: ", activeFilter); const allCategories = data .reduce((p, c) => [...p, ...c.categories], []) .filter((elem, index, self) => index === self.indexOf(elem)); return ( <div className={classes.root}> <div className={classes.toolbar} /> <Divider /> <List className={classes.list}> {allCategories.sort().map((text, index) => ( <ListItem className={classes.itemList} button key={text}> <Checkbox onChange={handleChange(text)} /> <ListItemText primary={text} /> </ListItem> ))} </List> </div> ); }; export default DrawerComponent;

Thank you in advance.先感谢您。

Et voilà.等等。 I've added a callback in your onChange event so as to avoid writing it in the handleChange function.我在您的 onChange 事件中添加了一个回调,以避免在 handleChange 函数中写入它。 Also, I've made sure the array is properly filtered if you toggle the checkbox status (with your version, a country would be added in double if you clicked twice on it).此外,如果您切换复选框状态,我已确保正确过滤数组(对于您的版本,如果您单击两次,国家/地区将被添加双倍)。

 const handleChange = (text) => { if(activeFilter.some(country=> country.text === text)){ const updatedList = activeFilter.filter(country=> country.text!==text) return setActiveFilter(updatedList) } return setActiveFilter((prev) => ([...prev, {text}])) } // <Checkbox onChange={()=> handleChange(text)} />

You can also add or filter array based on checkbox events:您还可以根据复选框事件添加或过滤数组:

const handleChange = (event, text) => {
  if (event.target.checked) {
    setActiveFilter((prev) => prev.concat({text}));
  } else {
    const filteredData = activeFilter.filter(
      (filterValue) => filterValue.text !== text
    );
    setActiveFilter(filteredData);
  }
};

... ...

<Checkbox onChange={(event) => handleChange(event, text)} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM