自定义蚂蚁设计的表搜索功能,以便数字搜索返回的结果小于或大于,不仅等于

时间:2019-06-26 22:39:07

标签: javascript reactjs antd

据我所知,ant design的表格目前没有特定的数字搜索功能,可以在特定范围内返回结果,例如> 10或<= 50。

给出以下示例: codesandbox

import React from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { Table, Input, Button, Icon } from 'antd';
import Highlighter from 'react-highlight-words';

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',
  },
];

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

  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)}
          style={{ width: 188, marginBottom: 8, display: 'block' }}
        />
        <Button
          type="primary"
          onClick={() => this.handleSearch(selectedKeys, confirm)}
          icon="search"
          size="small"
          style={{ width: 90, marginRight: 8 }}
        >
          Search
        </Button>
        <Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>
          Reset
        </Button>
      </div>
    ),
    filterIcon: filtered => (
      <Icon type="search" 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 => (
      <Highlighter
        highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
        searchWords={[this.state.searchText]}
        autoEscape
        textToHighlight={text.toString()}
      />
    ),
  });

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

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

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

ReactDOM.render(<App />, document.getElementById('container'));

让我们看一下“年龄”列,如果我们指定确切的年龄,则将返回确切的结果。在键入值的 input 元素上,按Enter键或onClick时,会触发 handleSearch ,然后 handleSearch 设置反应状态,然后重新渲染组件,所有基本的反应功能。

我不确定设置应该返回什么的逻辑在哪里,所以不确定我应该在哪里以及如何实现自定义逻辑来检查特定范围的数字而不是确切的数字。有什么想法吗?

或者,我检查了是否存在用于蚂蚁设计的数字搜索/过滤器,但找不到任何东西。

有关更多参考,请参阅蚂蚁设计table documentation

1 个答案:

答案 0 :(得分:0)

您可以使用SliderInputNumber组合for example渲染任何您喜欢的过滤器:

enter image description here

filterDropdown: ({ setSelectedKeys, selectedKeys, confirm }) => (
  <div style={{ padding: 8 }}>
    <Row
      type="flex"
      gutter={10}
      style={{ marginBottom: 8, alignItems: "center" }}
    >
      <Col>Range:</Col>
      <Col>
        <InputNumber
          value={this.state.left}
          onChange={e => {
            this.setState({ left: e });
            setSelectedKeys(data.filter(d => e <= d.age).map(d => d.key));
          }}
        />
      </Col>
      <Col>
        <InputNumber
          value={this.state.right}
          onChange={e => {
            this.setState({ right: e });
            setSelectedKeys(data.filter(d => d.age <= e).map(d => d.key));
          }}
        />
      </Col>
    </Row>
    <Row>
      <Slider
        range
        value={[this.state.left, this.state.right]}
        onChange={e => this.setState({ left: e[0], right: e[1] })}
      />
    </Row>
    <Row>
      <Button
        type="primary"
        block
        size="small"
        onClick={() => {
          this.handleSearchAge(selectedKeys, confirm);
          setSelectedKeys(
            data
              .filter(
                d => this.state.left <= d.age && d.age <= this.state.right
              )
              .map(d => d.key)
          );
        }}
      >
        Confirm
      </Button>
    </Row>
  </div>
);

Edit Q-56781764-SliderInTableFilter