Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter antd Table With datePicker in react

Following is the code for Ant design table with filtering the object using the input field but i want it to transform it for antd datepicker field instead of input field but not getting how to tranform it.

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);
Following is the code for Ant design table with filtering the object using the input field but i want it to transform it for antd datepicker field instead of input field but not getting how to tranform it.
like image 618
ahmed tahir Avatar asked Oct 23 '25 21:10

ahmed tahir


1 Answers

You can use something like this to display a datePicker instead of a 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>
like image 71
ngumichel Avatar answered Oct 26 '25 11:10

ngumichel