简体   繁体   English

一次重置所有过滤器

[英]Reset all filters at once

I have a sidebar on my site that has 8 options to filter cars by.我的网站上有一个侧边栏,其中有 8 个选项来过滤汽车。 Each of the parameters is made in the form of a drop-down list.每个参数都以下拉列表的形式制作。 Parameters are made in various styles - calendar, checkbox, taginput, on/off button.参数以各种样式制作 - 日历、复选框、标签输入、开/关按钮。

When the user wants to reset all filters, it is inconvenient for him to return everything to its original position.当用户想要重置所有过滤器时,他不方便将所有东西都恢复到原来的位置。 I would like to add a button that will bring the sidebar to its original position.我想添加一个按钮,将侧边栏带到其原始位置。

I added a very simplified code.我添加了一个非常简化的代码。 Here I create a sidebar, and I prescribe each component (filter) in it.在这里,我创建了一个侧边栏,并在其中规定了每个组件(过滤器)。

    export default function FiltersSideBar({className}) {
    return (
        <CardContent className={className}>
            <Table>
                <TableBody>

                    <TableRow>
                        <TableCell>
                            <Filter1/>
                        </TableCell>
                    </TableRow>

                    <TableRow>
                        <TableCell>
                            <Filter2/>
                        </TableCell>
                    </TableRow>

                    <TableRow>
                        <TableCell>
                            <Filter3/>
                        </TableCell>
                    </TableRow>

                    <button>Reset All Filters</button>

                </TableBody>
            </Table>
        </CardContent>
    );
}

Is there any easy way to reset all filters and reset the sidebar?有什么简单的方法可以重置所有过滤器并重置侧边栏吗?

First of all, you don't need to set two files of the same component, FilterDateTime.js , they are the same component, and you can use it with different props values.首先,你不需要设置同一个组件的两个文件FilterDateTime.js ,它们是同一个组件,你可以使用不同的 props 值。 this is the main concept of components .这是components的主要概念。

About the filters, each filter should have a state control it as filterDateTimeOpen which control if the date input is opened or not, and time state which control the start and end date of the filter.关于过滤器,每个过滤器都应该有一个state控件,如filterDateTimeOpen控制日期输入是否打开, time状态控制过滤器的开始和结束日期。

The benefit of the state is it is only updated by setState function.状态的好处是它只由setState函数更新。 and then update the UI, Also state re-initialize to default state when the component unmount.然后更新 UI,当组件卸载时,状态也重新初始化为默认状态。

So, to reset the filters, you need to setState for each state that controls each filter.因此,要重置过滤器,您需要为控制每个过滤器的每个state设置setState

Please have a look at this.请看看这个。

https://codesandbox.io/s/serene-mayer-6h28sy https://codesandbox.io/s/serene-mayer-6h28sy

Assuming you don't want to use context or redux, you'd have to pull the state up to your FiltersSidebar (ie instead of each component handling it's own 'filter value', you keep track of all of that in the parent FiltersSidebar).假设您不想使用上下文或 redux,您必须将状态拉到您的 FiltersSidebar(即,不是每个组件处理它自己的“过滤器值”,而是在父 FiltersSidebar 中跟踪所有这些) .

So something like:所以像:

export default function FiltersSideBar({className}) {

    const [filter1Value, setFilter1Value] = useState();
    const [filter2Value, setFilter2Value] = useState();
    const [filter3Value, setFilter3Value] = useState();

    const onResetAll = () => {
      setFilter1Value(null);
      setFilter2Value(null);
      setFilter3Value(null);
    }

    return (
        <CardContent className={className}>
            <Table>
                <TableBody>

                    <TableRow>
                        <TableCell>
                            <Filter1 value={filter1Value}/>
                        </TableCell>
                    </TableRow>

                    <TableRow>
                        <TableCell>
                            <Filter2 value={filter2Value}/>
                        </TableCell>
                    </TableRow>

                    <TableRow>
                        <TableCell>
                            <Filter3 value={filter3Value}/>
                        </TableCell>
                    </TableRow>

                    <button onClick={onResetAll}>Reset All Filters</button>

                </TableBody>
            </Table>
        </CardContent>
    );
}

You can have a object with the default filter state, and make a functionality that reset the filters wherever you want, so, you can do the following:您可以拥有一个具有默认过滤器状态的对象,并创建一个在您想要的任何位置重置过滤器的功能,因此,您可以执行以下操作:

// Declare a object with your filters
const defaultFilterState = {
  filter1: [],
  filter2: [],
  filter3: [],
  filter4: [],
};

export default function FiltersSideBar({ className }) {
  // declare state
  const [filters, setFilters] = useState(defaultFilterState);

  // Reset all filters
  const resetFilters = () => setFilters(defaultFilterState);

  return (
    <CardContent className={className}>
      <Table>
        <TableBody>
          <TableRow>
            <TableCell>
              <Filter1 value={filters.filter1} />
            </TableCell>
          </TableRow>

          <TableRow>
            <TableCell>
              <Filter2 value={filters.filter2} />
            </TableCell>
          </TableRow>

          <TableRow>
            <TableCell>
              <Filter3 value={filters.filter3} />
            </TableCell>
          </TableRow>

          <TableRow>
            <TableCell>
              <Filter4 value={filters.filter4} />
            </TableCell>
          </TableRow>

          <button onClick={resetFilters}>Reset All Filters</button>
        </TableBody>
      </Table>
    </CardContent>
  );
}

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

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