使用Material React Table来维护列过滤器状态。

huangapple go评论113阅读模式
英文:

Maintain Column Filter State using Materal React Table

问题

我正在使用Material React Table来显示记录列表。在初始化组件时,如何保持筛选状态?(例如,用户导航离开并返回等情况)

在我的列中,我有多选和文本字段变体的筛选。

const columns = useMemo(
  () => [
    {
      accessorKey: 'description',
      header: 'Description',
      filterVariant: 'text',
      size: 150,
    },
    {
      accessorKey: 'task_type',
      header: 'Type',
      size: 90,
      filterVariant: 'multi-select',
    }
  ],
  []
)

这是我的表格。

<MaterialReactTable
  columns={columns}
  data={rows}
  enableStickyHeader
  enablePinning
  initialState={{ density: 'compact' }}
  enableFacetedValues
  enableFilterMatchHighlighting
  muiTablePaperProps={{
    elevation: 0,
    sx: {
      borderRadius: '0'
    }
  }}
  muiTableContainerProps={{ sx: { maxHeight: '350px' } }}
/>
英文:

I am using Material React Table to display the list of records. How can I maintain the filter state while initializing the component? (In case the user navigates away and comes back etc.)

In my column, I have a multi-select and text field variant filter.

    const columns = useMemo(
() =&gt; [
  {
    accessorKey: &#39;description&#39;,
    header: &#39;Description&#39;,
    filterVariant: &#39;text&#39;,
    size: 150,
  },
  {
    accessorKey: &#39;task_type&#39;,
    header: &#39;Type&#39;,
    size: 90,
    filterVariant: &#39;multi-select&#39;,
  }
],
[]

)

This is my Table.

&lt;MaterialReactTable
      columns={columns}
      data={rows}
      enableStickyHeader
      enablePinning
      initialState={{ density: &#39;compact&#39; }}
      enableFacetedValues
      enableFilterMatchHighlighting
      muiTablePaperProps={{
        elevation: 0,
        sx: {
          borderRadius: &#39;0&#39;
        }
      }}
      muiTableContainerProps={{ sx: { maxHeight: &#39;350px&#39; } }}
      
    /&gt;

答案1

得分: 0

如果我的假设是正确的,那么您需要在用户选择一个过滤器并导航到另一页,然后返回页面时不更改过滤器。

您可以使用自定义函数来实现这一点。请参阅此文档Material Table Custom Filter Function

英文:

If my assumption is correct you need the filter to not been changed if the user select a filter and navigates to another page and reverts back to the page

you can use a custom function for this. See this documentation Material Table Custom Filter Function

huangapple
  • 本文由 发表于 2023年7月6日 16:19:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定