简体   繁体   English

如何在反应材料表中创建自定义过滤器框?

[英]How to create a custom filter box in a react material-table?

So I have this material-table in a react project I'm working on, the default filtering option just puts a row above your data where you can type whatever you want.所以我在我正在处理的一个反应项目中有这个材料表,默认过滤选项只是在你的数据上方放置一行,你可以在其中输入任何你想要的东西。 What I need is to make a button above the table that I could click on, then click on the checkbox to choose what I want to filter out.我需要的是在表格上方制作一个可以单击的按钮,然后单击复选框以选择要过滤掉的内容。 I was wondering if there is a way you could do that by modifying what material-table gives you or should I just link the checkbox options to a function that would get the data again and filter them out with a.filter?我想知道是否有一种方法可以通过修改材料表给您的内容来做到这一点,或者我应该将复选框选项链接到 function 以再次获取数据并使用 a.filter 过滤掉它们? it just seems like a long way around though, doing that for every checkbox, but I haven't really found the solution to my problem anywhere.不过,这似乎还有很长的路要走,对每个复选框都这样做,但我还没有真正找到解决我问题的任何地方。 I've only seen people doing that with react-table.我只见过有人用 react-table 来做这件事。 I would be thankful for any suggestions.如果有任何建议,我将不胜感激。 Here's a poor gimp drawing on how i want my filter box to look like这是一个关于我希望我的过滤器框看起来如何的可怜的 gimp 绘图

Reading you I understand that you want to crete custom filter.阅读您我了解您想要创建自定义过滤器。 So you could define your button and filter based on rowData.因此,您可以根据 rowData 定义按钮和过滤器。 I've found an example .我找到了一个例子 I hope it helps:我希望它有帮助:

<MaterialTable
    columns={[
      {
        title: "Adı",
        field: "name"
      },
      { title: "Soyadı", field: "surname" },
      { title: "Doğum Yılı", field: "birthYear", type: "numeric" },
      {
        title: "Doğum Yeri",
        field: "birthCity",
        lookup: { 34: "İstanbul", 63: "Şanlıurfa" }
      }
    ]}
    data={[
      { name: "Mehmet", surname: "Baran", birthYear: 1987, birthCity: 63 }
    ]}
    options={{
      filtering: true
    }}
    title="Demo Title"
    icons={tableIcons}
    components={{
      FilterRow: props => <FilterRow {...props} /> <---- your modified filter row component
    }}
  />

Using the example you can override all the filters and I think that is what you want to do.使用该示例,您可以覆盖所有过滤器,我认为这就是您想要做的。 You can define your filters in column def.您可以在列定义中定义您的过滤器。 After that make you custom filter component and get props data to get what you want.之后让您自定义过滤器组件并获取道具数据以获得您想要的。

Inside the same post you have more detailed examples about how to manage changes on filters:在同一篇文章中,您有更多关于如何管理过滤器更改的详细示例:

  const CustomDatePicker = (props) => {
  const [date, setDate] = useState(null);

  return (
    <DatePicker
      label="Select Date"
      inputVariant="outlined"
      variant="inline"
      format="dd/MM/yyyy"
      value={date}
      ampm
      autoOk
      allowKeyboardControl
      style={{ minWidth: 175 }}
      onChange={(event) => {
        setDate(event);
        props.onFilterChanged(props.columnDef.tableData.id, event);
      }}
      InputProps={{
        endAdornment: (
          <InputAdornment position="end">
            <IconButton>
              <EventIcon />
            </IconButton>
          </InputAdornment>
        ),
      }}
    />
  );
};

And if you want to override only in one column:如果您只想在一列中覆盖:

{
    title: "Created Date",
    field: "order_created_date",
    searchable: false,
    grouping: false,
    sorting: true,
    type: "datetime",
    filterComponent: (props) => <CustomDatePicker {...props} />,
  }

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

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