如何在表格中进行搜索?

时间:2019-03-25 06:44:52

标签: reactjs antd

我要搜索所有的分页和排序字段。 那就是我想用searchkeyword调用handleChange。

那么我该如何调用handleSearch而不是从handleSearch内调用handleChange?

import React from "react";
import { withRouter } from "react-router-dom";
import { Table, Button, Icon, Row, Col, Input } from "antd";

import axios from "axios";

const Search = Input.Search;

class App extends React.Component {
  state = {
    data: [],
    searchValue: "",
    loading: false,
    visible: false,
    pagination: {}
  };

  componentDidMount() {
    this.fetch();
  }

  handleTableChange = (pagination, filter, sorter, value) => {
    console.log(pagination, value, sorter, "Table Change");
    let sorterOrder = "";
    let sorterField = "";
    let searchVal = "";
    const pager = { ...this.state.pagination };
    pager.current = pagination.current;
    this.setState({
      pagination: pager
    });
    if (value) {
      console.log("inside if");
      searchVal = undefined;
    } else {
      console.log("inside else");
      searchVal = value;
    }
    if (sorter) {
      if (sorter.order === "ascend") {
        sorterOrder = "ASC";
      }
      if (sorter.order === "descend") {
        sorterOrder = "DESC";
      }
      sorterField = sorter.field;
    }
    this.fetch({
      page: pagination.current,
      sortField: sorterField,
      sortOrder: sorterOrder,
      ...filter,
      value: searchVal
    });
  };

  fetch = (params = {}) => {
    this.setState({ loading: true });
    axios.post("***/****", params).then(res => {
      const pagination = { ...this.state.pagination };
      let apiData = res.data.result;
      if (res.data.status === 1) {
        const objects = apiData.map(row => ({
          key: row.id,
          id: row.id,
          firstName: row.firstName,
          lastName: row.lastName,
          status: row.status,
          email: row.email
        }));
        console.log(res.data);
        pagination.total = res.data.count;
        this.setState({
          loading: false,
          data: objects,
          pagination
        });
      } else {
        console.log("Database status is not 1!");
      }
    });
  };

 

  handleSearch = value => {
    console.log("value", value);
    this.setState({
      searchValue: value
    });
    let searchkey = this.state.searchValue;
    const pagination = { ...this.state.pagination };
    const sorter = { ...this.state.sorter };
    console.log("search", value, pagination, sorter);
    this.handleTableChange({
      value
    });
    
  };


  render() {
    const columns = [
      {
        title: "First Name",
        dataIndex: "firstName",
        sorter: true
      },
      {
        title: "Email",
        dataIndex: "email",
        sorter: true
      }
    ];
    return (
      
          <div>
            
                    <Search
                      placeholder="input search text"
                      className="searchBut"
                      onSearch={value => this.handleSearch(value)}
                    />
                  
                    <Button
                      className="addBut"
                      style={{ marginTop: "0" }}
                      type="primary"
                      onClick={() => this.openForm()}
                    >
                      + Add Admin
                    </Button>
                  
          </div>
          <Table
            bordered
            columns={columns}
            dataSource={this.state.data}
            pagination={{ total: this.state.pagination.total, pageSize: 4 }}
            loading={this.state.loading}
            onChange={this.handleTableChange}
          />
        
    );
  }
}

export default withRouter(App);

在这里,当我在搜索字段中提供值时,它将使用请求有效负载调用发布请求,如下所示:

{sortField: "", sortOrder: ""}
sortField: ""
sortOrder: ""

那我该怎么办?

1 个答案:

答案 0 :(得分:0)

我不确定您要在这里实现什么。

但是您始终可以过滤表的源数据。就像关注

<Table
     bordered
     columns={columns}
     dataSource={this.state.data.filter(data=> Object.keys(data).filter(key => data[key].includes(searchValue)).length > 0 ? true: false)}
     pagination={{ total: this.state.pagination.total, pageSize: 4 }}
     loading={this.state.loading}
     onChange={this.handleTableChange}
/>

让我知道是否有帮助。