admin管理员组文章数量:1426644
Using codes from the example: /ponents/table/#ponents-table-demo-custom-filter-panel
getColumnSearchProps = dataIndex => ({
filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
<div style={{ padding: 8 }}>
<Input
ref={node => {
this.searchInput = node;
}}
placeholder={`Search ${dataIndex}`}
value={selectedKeys[0]}
onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
onPressEnter={() => this.handleSearch(selectedKeys, confirm, dataIndex)}
style={{ width: 188, marginBottom: 8, display: 'block' }}
/>
<Space>
<Button
type="primary"
onClick={() => this.handleSearch(selectedKeys, confirm, dataIndex)}
icon={<SearchOutlined />}
size="small"
style={{ width: 90 }}
>
Search
</Button>
<Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>
Reset
</Button>
</Space>
</div>
),
filterIcon: filtered => <SearchOutlined style={{ color: filtered ? '#1890ff' : undefined }} />,
onFilter: (value, record) =>
record[dataIndex].toString().toLowerCase().includes(value.toLowerCase()),
onFilterDropdownVisibleChange: visible => {
if (visible) {
setTimeout(() => this.searchInput.select());
}
},
render: text =>
this.state.searchedColumn === dataIndex ? (
<Highlighter
highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
searchWords={[this.state.searchText]}
autoEscape
textToHighlight={text.toString()}
/>
) : (
text
),
});
Error Uncaught TypeError: Cannot read property 'toString' of undefined
thrown when trying to pass nested values in the ANTD Table:
<Table bordered size='small' dataSource={data} rowKey='_id'>
....
<Column
title='Name'
dataIndex={['profile', 'name']}
{...this.getColumnSearchProps(['profile', 'name'])}
/>
....
</Table>
Here is how the structure of the data
(dataSource) for the table:
[
{_id: 'xxx1', profile : { name : 'username1' }, roles: ['xxx1']},
{_id: 'xxx2', profile : { name : 'username2' }, roles: ['xxx2']}
]
As per outlined in the documentation: /ponents/table/#Migrate-to-v4 :
Besides, the breaking change is changing dataIndex from nest string path like
user.age
to string array path like['user', 'age']
. This help to resolve developer should additional work on the field which contains.
.
hence the dataIndex={['profile', 'name']}
, but this is not the same case for the getColumnSearchProps
.
Anyone can help?
Using codes from the example: https://ant.design/ponents/table/#ponents-table-demo-custom-filter-panel
getColumnSearchProps = dataIndex => ({
filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
<div style={{ padding: 8 }}>
<Input
ref={node => {
this.searchInput = node;
}}
placeholder={`Search ${dataIndex}`}
value={selectedKeys[0]}
onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
onPressEnter={() => this.handleSearch(selectedKeys, confirm, dataIndex)}
style={{ width: 188, marginBottom: 8, display: 'block' }}
/>
<Space>
<Button
type="primary"
onClick={() => this.handleSearch(selectedKeys, confirm, dataIndex)}
icon={<SearchOutlined />}
size="small"
style={{ width: 90 }}
>
Search
</Button>
<Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>
Reset
</Button>
</Space>
</div>
),
filterIcon: filtered => <SearchOutlined style={{ color: filtered ? '#1890ff' : undefined }} />,
onFilter: (value, record) =>
record[dataIndex].toString().toLowerCase().includes(value.toLowerCase()),
onFilterDropdownVisibleChange: visible => {
if (visible) {
setTimeout(() => this.searchInput.select());
}
},
render: text =>
this.state.searchedColumn === dataIndex ? (
<Highlighter
highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
searchWords={[this.state.searchText]}
autoEscape
textToHighlight={text.toString()}
/>
) : (
text
),
});
Error Uncaught TypeError: Cannot read property 'toString' of undefined
thrown when trying to pass nested values in the ANTD Table:
<Table bordered size='small' dataSource={data} rowKey='_id'>
....
<Column
title='Name'
dataIndex={['profile', 'name']}
{...this.getColumnSearchProps(['profile', 'name'])}
/>
....
</Table>
Here is how the structure of the data
(dataSource) for the table:
[
{_id: 'xxx1', profile : { name : 'username1' }, roles: ['xxx1']},
{_id: 'xxx2', profile : { name : 'username2' }, roles: ['xxx2']}
]
As per outlined in the documentation: https://ant.design/ponents/table/#Migrate-to-v4 :
Besides, the breaking change is changing dataIndex from nest string path like
user.age
to string array path like['user', 'age']
. This help to resolve developer should additional work on the field which contains.
.
hence the dataIndex={['profile', 'name']}
, but this is not the same case for the getColumnSearchProps
.
Anyone can help?
Share Improve this question asked May 10, 2020 at 16:27 Hafiz HanifHafiz Hanif 4711 gold badge7 silver badges22 bronze badges1 Answer
Reset to default 6Since the dataIndex could be an array now, you need to take care of that case as well.
An example is provided below:
You basically have to
Replace
record[dataIndex]
with
get(record, dataIndex) // import get from "lodash.get";
and
this.state.searchedColumn === dataIndex
with
isequal(this.state.searchedColumn, dataIndex) // import isequal from "lodash.isequal";
本文标签: javascriptANTD Table getColumnSearchProps broken with nested objectStack Overflow
版权声明:本文标题:javascript - ANTD Table getColumnSearchProps broken with nested object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745418881a2657813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论