简体   繁体   English

我如何在 React 中使用分页搜索数据?

[英]How do I search data with pagination in React?

class Employee extends Component {
  constructor() {
    super();
    this.state = {
      // employees: employees,
      employees: [],
      searchfield: "",
      currentPage: 1,
      resultsPerPage: 50,
      holder: [],
      filteredEmployees: [],
      value: "",
    };
  }

  componentDidMount = async () => {
    axios.get(`http://localhost:5000/`).then((res) => {
      const employees = res.data;         
      this.setState({
        employees: employees.recordsets[0],
        holder: employees.recordsets[0],
      });
    });
  };

  onSearchChange = (event) => {
    let { value } = event.target;
    this.setState({ value }, () => {
      var updatedList = this.state.holder;

      updatedList = updatedList.filter((employee) => {
        const fullName = employee.empFirstNm + " " + employee.empLastNm;
        return (
          fullName.toLowerCase().search(this.state.value.toLowerCase()) !== -1
        );
      });

      this.setState({ employees: updatedList });
    });
  };

  //change page
  onPaginate = (pageNumber) => {
    this.setState({ currentPage: pageNumber });
  };

  render() {
    const { employees, searchfield, currentPage, resultsPerPage } = this.state;
    const { onRouteChange } = this.props;

    //Get current employees
    const indexOfLastEmployee = currentPage * resultsPerPage;
    const indexOfFirstEmployee = indexOfLastEmployee - resultsPerPage;
    console.log("indexOfFirstEmployee: ", indexOfFirstEmployee);
    console.log("indexOfLastEmployee: ", indexOfLastEmployee);
    const filteredEmployees = employees.slice(
      indexOfFirstEmployee,
      indexOfLastEmployee
    );

    return !employees.length ? (
      <div className="tc">
        <h1>Loading</h1>
        <Spinner animation="border" variant="danger" />
      </div>
    ) : (
      <FadeIn>
        <div className="tc">
          <div
            className="
          d-flex
          justify-content-between
          flex-wrap
          flex-md-nowrap
          align-items-center
          pt-3
          pb-2
          mb-3
          border-bottom"
          >
            <h1 className="display-2">Employees</h1>
            <div
              className="tr"
              style={{
                margin: "15px 0",
              }}
            >
              <NewEmployee employees={employees} />
              <SearchBox
                searchChange={this.onSearchChange}
                value={this.state.value}
              />
            </div>
          </div>

          <Scroll>
            <CardList
              employees={filteredEmployees}
              onDelete={this.handleDelete}
              onRouteChange={onRouteChange}
            />
            <Table
              employees={filteredEmployees}
              onRouteChange={onRouteChange}
            />

            <Pagination
              resultsPerPage={resultsPerPage}
              totalResults={employees.length}
              onPaginate={this.onPaginate}
            />
          </Scroll>
        </div>
      </FadeIn>
    );
  }
}

   

I have tried implementing it with conditions of whether the results is greater than or equal to the results on a page but it did not work since it doesnt cover every single case based on the result.我已经尝试在结果是否大于或等于页面上的结果的条件下实现它,但它没有用,因为它没有涵盖基于结果的每一个案例。 I am getting the search to work but when my current page is anything besides 1 is when the search doesn't work.我正在让搜索工作,但是当我的当前页面是 1 以外的任何内容时,就是搜索不起作用的时候。

Any idea how I can get the filtering to work regardless of what page I am on?无论我在哪个页面上,我都知道如何让过滤工作吗?

Figured this out.想通了。 I just needed to set currentPage to 1 at the end of my search:我只需要在搜索结束时将 currentPage 设置为 1:

  onSearchChange = (event) => {
    let { value } = event.target;
    this.setState({ value }, () => {
      //running this after setting the value in state because of async
      var updatedList = this.state.holder;
      updatedList = updatedList.filter((employee) => {
        const fullName = employee.empFirstNm + " " + employee.empLastNm;
        return (
          fullName.toLowerCase().search(this.state.value.toLowerCase()) !== -1
        );
      });
      this.setState({ employees: updatedList });
      this.setState({ currentPage: 1 });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM