【问题标题】:Filter antd Table With datePicker in react在反应中使用 datePicker 过滤 antd 表
【发布时间】:2020-07-27 22:13:39
【问题描述】:

以下是 Ant 设计表的代码,它使用输入字段过滤对象,但我希望它为 antd datepicker 字段而不是输入字段转换它,但不知道如何转换它。

const data = [
  {
    key: '1',
    date: '2020-04-01',
  },
  {
    key: '2',
    date: '2020-04-04',
  },
  {
    key: '3',
    date: '2020-04-03',
  },
  {
    key: '4',
   date: '2020-04-02',
  },
];

class App extends React.Component {
  state = {
    searchText: '',
    searchedColumn: '',
  };

  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' }}
        />
        <Button
          type="primary"
          onClick={() => this.handleSearch(selectedKeys, confirm, dataIndex)}
          icon={<SearchOutlined />}
          size="small"
          style={{ width: 90, marginRight: 8 }}
        >
          Search
        </Button>
        <Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>
          Reset
        </Button>
      </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
      ),
  });

  handleSearch = (selectedKeys, confirm, dataIndex) => {
    confirm();
    this.setState({
      searchText: selectedKeys[0],
      searchedColumn: dataIndex,
    });
  };

  handleReset = clearFilters => {
    clearFilters();
    this.setState({ searchText: '' });
  };

  render() {
    const columns = [
      {
        title: 'Date',
        dataIndex: 'name',
        key: 'name',
        width: '30%',
        ...this.getColumnSearchProps('name'),
      },
     
     
    ];
    return <Table columns={columns} dataSource={data} />;
  }
}

ReactDOM.render(<App />, mountNode);
以下是使用输入字段过滤对象的 Ant 设计表的代码,但我希望它为 antd datepicker 字段而不是输入字段转换它,但不知道如何转换它。

【问题讨论】:

  • 有什么更新吗?仍然无法按日期范围过滤

标签: reactjs filter datepicker ant-design-pro


【解决方案1】:

您可以使用类似的东西来显示 datePicker 而不是 searchInput。

<div style={{ padding, width }}>
  <DatePicker.RangePicker
    autoFocus={autoFocus}
    onChange={handleChange}
    placeholder={placeholder}
    value={value}
    format={format}
    style={{ marginBottom: 8 }}
  />
  <Button
    type="primary"
    role="search"
    onClick={handleSearch}
    style={{ width: btnWidth }}
    icon={<SearchOutlined />}
    size="small"
  >
    {label[0]}
  </Button>
  <Button
    role="reset"
    style={{ width: btnWidth, marginLeft }}
    onClick={handleClear}
    size="small"
  >
    {label[1]}
  </Button>
</div>

【讨论】:

  • 无法运行代码sn-p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多