【问题标题】:How to do column level search in ant-design using function component?如何使用功能组件在ant-design中进行列级搜索?
【发布时间】:2021-09-28 05:44:44
【问题描述】:

我正在使用函数组件并绘制我正在使用 ant-design 的表格。现在我想对表中存在的每一列进行列搜索

我试过这个 https://ant.design/components/table/

这是一个我有点困惑的类组件。谁能建议如何使用函数组件在 ant 设计表中搜索列级搜索?

谢谢

【问题讨论】:

标签: reactjs antd


【解决方案1】:

只是为了补充上一个答案:searchInput 是一个响应,在类组件中它是自动的,但在函数中您必须执行以下操作:

....

      const searchInput = useRef(null);

....

    <Input
        ref={searchInput}

....

    onFilterDropdownVisibleChange: visible => {
                if (visible) {
                    setTimeout(() => searchInput && searchInput.current && searchInput.current.select())
                }
            }

.....

【讨论】:

    【解决方案2】:

    应该是这样的,如果我忘了替换这个,对不起。我这里按照antd table API https://ant.design/components/table/#components-table-demo-custom-filter-panel

    const {  Table, Input, Button, Space  } = antd;
    import Highlighter from 'react-highlight-words';
    const {  SearchOutlined  } = icons;
    
    const data = [
      {
        key: '1',
        name: 'John Brown',
        age: 32,
        address: 'New York No. 1 Lake Park',
      },
      {
        key: '2',
        name: 'Joe Black',
        age: 42,
        address: 'London No. 1 Lake Park',
      },
      {
        key: '3',
        name: 'Jim Green',
        age: 32,
        address: 'Sidney No. 1 Lake Park',
      },
      {
        key: '4',
        name: 'Jim Red',
        age: 32,
        address: 'London No. 2 Lake Park',
      },
    ];
    
    const App = () => {
      
      const [state, setState] = useState({searchText:'', searchedColumn:''})
      
      const handleSearch = (selectedKeys, confirm, dataIndex) => {
        confirm();
        setState({
          searchText: selectedKeys[0],
          searchedColumn: dataIndex,
        });
      };
    
      const handleReset = clearFilters => {
        clearFilters();
        setState({ searchText: '' });
      };
      
      const 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={() => handleSearch(selectedKeys, confirm, dataIndex)}
              style={{ marginBottom: 8, display: 'block' }}
            />
            <Space>
              <Button
                type="primary"
                onClick={() => handleSearch(selectedKeys, confirm, dataIndex)}
                icon={<SearchOutlined />}
                size="small"
                style={{ width: 90 }}
              >
                Search
              </Button>
              <Button onClick={() => handleReset(clearFilters)} size="small" style={{ width: 90 }}>
                Reset
              </Button>
              <Button
                type="link"
                size="small"
                onClick={() => {
                  confirm({ closeDropdown: false });
                  setState({
                    searchText: selectedKeys[0],
                    searchedColumn: dataIndex,
                  });
                }}
              >
                Filter
              </Button>
            </Space>
          </div>
        ),
        filterIcon: filtered => <SearchOutlined style={{ color: filtered ? '#1890ff' : undefined }} />,
        onFilter: (value, record) =>
          record[dataIndex]
            ? record[dataIndex].toString().toLowerCase().includes(value.toLowerCase())
            : '',
        onFilterDropdownVisibleChange: visible => {
          if (visible) {
            setTimeout(() => searchInput.select(), 100);
          }
        },
        render: text =>
          state.searchedColumn === dataIndex ? (
            <Highlighter
              highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
              searchWords={[state.searchText]}
              autoEscape
              textToHighlight={text ? text.toString() : ''}
            />
          ) : (
            text
          ),
      });
    
      
        const columns = [
          {
            title: 'Name',
            dataIndex: 'name',
            key: 'name',
            width: '30%',
            ...getColumnSearchProps('name'),
          },
          {
            title: 'Age',
            dataIndex: 'age',
            key: 'age',
            width: '20%',
            ...getColumnSearchProps('age'),
          },
          {
            title: 'Address',
            dataIndex: 'address',
            key: 'address',
            ...getColumnSearchProps('address'),
            sorter: (a, b) => a.address.length - b.address.length,
            sortDirections: ['descend', 'ascend'],
          },
        ];
        
        return <Table columns={columns} dataSource={data} />;
      
    }
    
    ReactDOM.render(<App />, mountNode);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

    【讨论】:

    • 什么是 searchInput ?我收到此错误无法设置未定义的属性(设置'searchInput')
    猜你喜欢
    • 2019-10-22
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2021-08-09
    • 2020-01-23
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多