简体   繁体   中英

How do I add a filter to the array from a select onChange

I am trying to get the array to be updated to filter a column from it. The idea is to use multiple additional select/TextFields once I get one working. What I am struggling with is where and how to add it, so that a value can be obtained to update the array below.

The Modal/Filter Display

function DisplayFilter(data){

    const [open, setOpen] = React.useState(false);
    const handleOpen = () => setOpen(true);
    const handleClose = () => setOpen(false);

    const [source, getSource] = React.useState('');

    const handleChange = (event) => {getSource(event.target.value)}

    return (

    <div>

        <Stack spacing={2} direction="row">
            <Button onClick={handleOpen} type="submit" variant="contained" size="small" style={{ height: "40px", backgroundColor: 'purple' }} className="mleft1">
                Filter 
                <FilterListIcon style={{ fill: "white" }} />
            </Button>
        </Stack>

        <Modal
            open={open}
            onClose={handleClose}
            aria-labelledby="modal-modal-title"
            aria-describedby="modal-modal-description"
            >
            <Box sx={modalFilterStyle}>
                <div>
                    <Box sx={{ minWidth: 120, margin: 2 }}>
                      <FormControl fullWidth>
                        <InputLabel id="source-label">Source</InputLabel>
                        <Select
                          labelId="source-label"
                          id="source-label-select"
                          value={source}
                          label="Source"
                          onChange={handleChange}
                        >
                            <MenuItem key={makeUniqueKey()} value="">
                              Alle
                            </MenuItem> 
                    
                              {getUnique(data.data,'source')?.map(option => {
                                  return (
                                    <MenuItem key={makeUniqueKey()} value={option}>
                                      {option}
                                    </MenuItem>
                                  );
                              })}                           
                        </Select>
                      </FormControl>
                    </Box>
                </div>
            </Box>
        </Modal>
    </div>);
}

Search TextField

function SearchBar ({setSearchQuery}){

    return (

        <form>
            <IconButton type="submit" aria-label="search">
              <SearchIcon style={{ fill: "purple" }} />
            </IconButton>
            <TextField
              id="search-bar"
              onChange={(e) => {
                setSearchQuery(e.target.value);
              }}
              variant="outlined"
              placeholder="Suche..."
              size="small"
            />
        </form>
    );
}

Here I tried to create something to return the array, so that I can pass the value of a select to query

const modalfilterData = (query, data, headeritem) => {
  if (!query) {
  return data;
  } else {
  return data = data.filter((item) => {
    return Object.keys(item).some(key => item[headeritem].toString().toLowerCase().search(query.toLowerCase()) !== -1);
  });
  }
};

The rest of the code where everything comes together. This is where I would use modalfilterData so that I can update the tableData with the value from the select. What and how can I proceed, so that I can obtain the select value and possibly scale this up to add multiple similar select elements ?

...
export default function CollapsibleTable() {

  ...
  const rows = tableData;
  ...

  //.    
  //apply filter from select value using `modalfilterData`
  //.

  return (
            <SearchBar searchQuery={searchQuery} setSearchQuery={setSearchQuery} />

            <DisplayFilter data={tableData} />
  );
}

Sample Data : tableData

[
    {
        "id": 2097190,
        "date": 1652965848416,
        "client": 49372,
        "level": 2,
        "source": 3,
        "status": 0
    },
    {
        "id": 2097185,
        "date": 1652965848761,
        "client": 49372,
        "level": 2,
        "source": 3,
        "status": 0
    } 
]

Implementation of getUnique

function getUnique(array,headeritem){
    const unique = [...new Set(array.map(item => item[headeritem]))]
    return Array.from(unique);
}

要从CollapsibleTable组件中的DisplayFilter组件中获取Source select 的值,您必须像在SearchBar组件中那样提升状态

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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