使用AntD在React JavaScript中进行筛选操作

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

filtering using antD in React JavaScript

问题

I have a table which requires filtering. I am writing as a component class and not a function. When I try to filter the column, it gives me an error saying

Uncaught TypeError: Cannot read properties of undefined (reading 'includes') - using this for filtering on a text..

Idea values are "Choc Flavor" and "Mini Packs" so am trying to create basic filter but when I select the filter, it filters all values and shows me nothing.

render() {

    const onChange = (pagination, filters, sorter, extra) => {
      console.log('params', pagination, filters, sorter, extra);
    };

    const columns = [
      {
        title: 'Stack Rank',
        key: 'stackRank',
        dataIndex: 'rank',
        sorter: (a, b) => a.rank - b.rank,
      },
      {
        title: 'Idea',
        dataIndex: 'title',
        key: 'idea',
        sorter: (a, b) => a.title.length - b.title.length,
        filters: [
          {
            text: 'Mini',
            value: 'Mini',
          },
          {
            text: 'Choc Flavor',
            value: 'Choc Flavor',
          },
        ],
        onFilter: (value, record) => record.title?.includes(value) === 0,
        render: (_, record) => (
          <Space size="middle">
            <Link to={'/idea-detail/' + this.product_id + '/' + record.ideaId}>{record.title}</Link>
          </Space>
        ),
      },
      {
        title: 'Votes',
        dataIndex: 'voteCount',
        key: 'voteCount',
        sorter: (a, b) => a.voteCount - b.voteCount,
        onFilter: (value, record) => record.voteCount.includes(value),
        sortDirections: ['descend', 'ascend'],
      },    
    ];

    return (
      <Table columns={columns} dataSource={this.state.idea_list} onChange={onChange}   pagination={{
      }} />
    );
}

使用AntD在React JavaScript中进行筛选操作
1: https://i.stack.imgur.com/oy7gD.png

英文:

I have a table which requires filtering. I am writing as a component class and not a function. When I try to filter the column, it gives me an error saying

Uncaught TypeError: Cannot read properties of undefined (reading 'includes') - using this for filtering on a text..

Idea values are "Choc Flavor" and "Mini Packs" so am trying to create basic filter but when I select the filter, it filters all values and shows me nothing.

render() {
const onChange = (pagination, filters, sorter, extra) =&gt; {
console.log(&#39;params&#39;, pagination, filters, sorter, extra);
};
const columns = [
{
title: &#39;Stack Rank&#39;,
key: &#39;stackRank&#39;,
dataIndex: &#39;rank&#39;,
sorter: (a, b) =&gt; a.rank - b.rank,
},
{
title: &#39;Idea&#39;,
dataIndex: &#39;title&#39;,
key: &#39;idea&#39;,
sorter: (a, b) =&gt; a.title.length - b.title.length,
{
text: &#39;Mini&#39;,
value: &#39;Mini&#39;,
},
{
text: &#39;Choc Flavor&#39;,
value: &#39;Choc Flavor&#39;,
},
],
onFilter: (value, record) =&gt; record.title?.includes(value) === 0,            render: (_, record) =&gt; (
&lt;Space size=&quot;middle&quot;&gt;
&lt;Link to={&#39;/idea-detail/&#39;+ this.product_id+&#39;/&#39;+record.ideaId} &gt;{record.title}&lt;/Link&gt;
&lt;/Space&gt;
),
},
{
title: &#39;Votes&#39;,
dataIndex: &#39;voteCount&#39;,
key: &#39;voteCount&#39;,
sorter: (a, b) =&gt; a.voteCount - b.voteCount,
onFilter: (value, record) =&gt; record.voteCount.includes(value),
sortDirections: [&#39;descend&#39;, &#39;ascend&#39;],
},    
];
return (
&lt;Table columns={columns} dataSource={this.state.idea_list} onChange={onChange}   pagination={{
}} /&gt;

使用AntD在React JavaScript中进行筛选操作

答案1

得分: 1

这可能是因为数组变量 this.state.idea_list 中的一个或多个项目不包含 'idea' 字段或 'idea' 字段未定义。

解决方案是将 onFilter 函数更改为以下形式:

onFilter: (value, record) => record.idea?.includes(value) === 0
英文:

This is probably due to the fact that one or all of the item in the array variable this.state.idea_list do not contain the 'idea' field or it is undefined.

A solution would be to change onFilter function like this:

onFilter: (value, record) =&gt; record.idea?.includes(value) === 0

huangapple
  • 本文由 发表于 2023年2月18日 12:09:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491128.html
匿名

发表评论

匿名网友

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

确定