在 react-table 中实现全局搜索过滤器:React+ react-table

Implementing a global search filter across react-table : React+ react-table

我正在尝试实现一个全局搜索过滤器,用于在整个 table 中搜索密钥。我附加了一个更改处理程序,并且在每次输入时我都会触发一个回调,该回调会在该数据中搜索该键,并且当我输入字符时,值 set.It 正在被过滤,但我希望搜索在我输入时起作用在搜索输入中输入多个搜索值,如 string1、string2

代码沙箱:https://codesandbox.io/s/jolly-bhabha-iqcx1

代码

import React from "react";
import ReactDOM from "react-dom";
import { Input } from "semantic-ui-react";
import ReactTable from "react-table";
import "react-table/react-table.css";
import "./styles.css";

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [
        { firstName: "aaaaa", status: "Pending", visits: 155 },
        { firstName: "aabFaa", status: "Pending", visits: 155 },
        { firstName: "adaAAaaa", status: "Approved", visits: 1785 },
        { firstName: "aAaaaa", status: "Approved", visits: 175 },
        { firstName: "adaSaaa", status: "Cancelled", visits: 165 },
        { firstName: "aasaaa", status: "Cancelled", visits: 157 },
        { firstName: "aweaaaaaewea", status: "Approved", visits: 153 },
        { firstName: "aaaaaa", status: "Submitted", visits: 155 }
      ],
      columns: [],
      searchInput: ""
    };
  }

  componentDidMount() {
    let columns = [
      {
        Header: "First Name",
        accessor: "firstName",
        sortable: false,
        show: true,
        displayValue: " First Name"
      },
      {
        Header: "Status",
        accessor: "status",
        sortable: false,
        show: true,
        displayValue: "Status "
      },
      {
        Header: "Visits",
        accessor: "visits",
        sortable: false,
        show: true,
        displayValue: " Visits "
      }
    ];
    this.setState({ columns });
  }

  handleChange = event => {
    this.setState({ searchInput: event.target.value }, () => {
      this.globalSearch();
    });
  };

  globalSearch = () => {
    let { data, searchInput } = this.state;
    if (searchInput) {
      let filteredData = data.filter(value => {
        return (
          value.firstName.toLowerCase().includes(searchInput.toLowerCase()) ||
          value.status.toLowerCase().includes(searchInput.toLowerCase()) ||
          value.visits
            .toString()
            .toLowerCase()
            .includes(searchInput.toLowerCase())
        );
      });
      this.setState({ data: filteredData });
    }
  };

  render() {
    let { data, columns, searchInput } = this.state;
    return (
      <div>
        <br />
        <Input
          size="large"
          name="searchInput"
          value={searchInput || ""}
          onChange={this.handleChange}
          label="Search"
        />
        <br />
        <br />
        <ReactTable
          data={data}
          columns={columns}
          defaultPageSize={10}
          className="-striped -highlight"
        />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

这不是 react-table 的问题。在当前实施中,

  1. 这是您第一次从原始数据中筛选。

  2. 第二次尝试过滤时,您试图从之前过滤的数据中过滤结果(state 对象中的当前数据是上次尝试的过滤结果)。

我建议试试(这是从你那里分叉出来的):https://codesandbox.io/s/eloquent-clarke-w1ehv

将原始数据维护为不可变源,并在每次更改搜索输入时从中过滤,而不是从状态中的当前数据对象中过滤。看看我附上的叉子。

如果您的数据来自 ajax 调用,您可能应该维护两个状态变量:

this.state = {
   data: [],
   filteredData: [],
   columns: [],
   searchInput: ""
};

componentDidMount() {
  yourapicall.then(data => {
    this.setState({
      data // whatever data you get.
    });
  });
}

// And in your global search

globalSearch = () => {
    let { searchInput, data } = this.state;
    let filteredData = data.filter(value => {
    return (
        value.firstName.toLowerCase().includes(searchInput.toLowerCase()) ||
        value.status.toLowerCase().includes(searchInput.toLowerCase()) ||
        value.visits
          .toString()
          .toLowerCase()
          .includes(searchInput.toLowerCase())
      );
    });
    this.setState({ filteredData });
};

并有条件地呈现您的 react-table

<ReactTable
   data={filteredData && filteredData.length ? filteredData : data}
   columns={columns}
   defaultPageSize={10}
   className="-striped -highlight"
/>

希望对您有所帮助!