【问题标题】:React MaterialTable clear all filters action - column and global filterReact MaterialTable 清除所有过滤器操作 - 列和全局过滤器
【发布时间】:2021-01-20 15:26:39
【问题描述】:

我对反应完全陌生。 这可能微不足道,但我不知道如何实现清除所有表格过滤器的操作。

在我的表格中,我使用日期过滤器、下拉列表、文本和全局过滤器寻找一键清除所有过滤器

https://codesandbox.io/s/eager-thunder-ejlg5?file=/src/index.js

  <MaterialTable
      title="Free Action Preview"
      columns={[
        { title: "Name", field: "name" },
        { title: "Surname", field: "surname" },
        { title: "Birth Year", field: "birthYear", type: "numeric" },
        {
          title: "Birth Place",
          field: "birthCity",
          lookup: { 34: "İstanbul", 63: "Şanlıurfa" }
        }
      ]}
      data={[
        { name: "Mehmet", surname: "Baran", birthYear: 1987, birthCity: 63 },
        {
          name: "Zerya Betül",
          surname: "Baran",
          birthYear: 2017,
          birthCity: 34
        }
      ]}
      actions={[
        {
          icon: () => <FilterNoneIcon />,
          tooltip: "clear all filters",
          isFreeAction: true,
          onClick: (event) => alert("clear all filters logic")
        }
      ]}
      options={{
        filtering: true,
        sorting: true
      }}
    />

【问题讨论】:

  • 请阅读 stackoverflow.com/help/minimal-reproducible-example 提供一些重现问题的最小代码,以便观众更好地理解。
  • @Prasad Phule 添加了一个示例项目,我确实知道 StackOverflow 的角色非常好!.. 这是一个非常强大的前瞻性问题

标签: reactjs material-ui material-table


【解决方案1】:

在撰写本文时,他们似乎没有明确的过滤器功能 - 至少根据这个问题:https://github.com/mbrn/material-table/issues/1132 因为他们将其标记为wontfix - 这意味着他们不打算处理它。但是,在同一问题上,有 1 位用户建议使用 ref 并手动访问表以过滤数据(尽管该用户后来建议不要这样做) - 所以您也可以尝试。

您可以这样做的另一种方法是重新安装组件。由于组件被重新挂载,它将以其初始状态开始,包括未过滤的数据

function App() {
  const [muiTableKey, setMuiTableKey] = React.useState(0);

  return (
    <MaterialTable
      key={muiTableKey}
      actions={[
        {
          icon: () => <FilterNoneIcon />,
          tooltip: "clear all filters",
          isFreeAction: true,
          onClick: (event) => {
            setMuiTableKey(muiTableKey + 1); // set new key causing remount
          }
        }
      ]}

【讨论】:

  • 谢谢,我真的想疯了,我想念 Angular :)
  • 谢谢,解决材料表限制的好方法。
猜你喜欢
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2019-08-09
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
相关资源
最近更新 更多