在按钮点击时加载react-table的服务器数据

时间:2017-11-08 19:07:29

标签: reactjs react-server

我正在尝试显示一个带有服务器端分页的反应表,它运行正常。问题是react-table在页面加载期间从服务器加载数据,但我只是在用户单击页面上的链接时尝试在react-table中加载数据。

以下是react-table的代码:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import ReactTable from 'react-table';
import 'react-table/react-table.css';
import { Loader, Message } from 'semantic-ui-react';
import { getColumnHeaders } from '../components/search-result-headers/columnHelper';
import axios from 'axios';

const requestData = (pageSize, page, sorted, filtered,ROOT_URL) => {
   return new Promise((resolve, reject) => {
      // You can retrieve your data however you want, in this case, we will just use some local data.
       // construct the url and make a request to the search api
      const url = ROOT_URL + '?pageSize=' + pageSize+"&pageNo="+(page+1);
      axios.get(url).then(response => { 
         if(response.status === 200){
             // You must return an object containing the rows of the current page, and optionally the total pages number.
            const res = {
               rows: response.data.resultData.searchResults,
               pages: Math.ceil(response.data.resultData.totalAvailable / pageSize)
            };
            resolve(res);
         }else{
            console.log(`Error :${response.status}`);
         }
      }) 
    });
 }


class ResultsContainer extends Component {

   constructor() {
      super();
      this.state = {
        data: [],
        pages: null,
        loading: false
      };
      this.fetchData = this.fetchData.bind(this);
      this.forceUpdate();
    }

    fetchData(state, instance) {
      //set the loading message on table
      this.setState({ loading: true });
      let url = `/api/sample`;

      // Request the data from API
      requestData(
        state.pageSize,
        state.page,
        state.sorted,
        state.filtered,
        url
      ).then(res => {
        // Now just get the rows of data to your React Table (and update anything else like total pages or loading)
        this.setState({
          data: res.rows,
          pages: res.pages,
          loading: false
        });
      });
    }

   render() {
      // get the search results column header as per the search type
      let columns = getColumnHeaders(this.props.searchType);
      const { data, pages, loading } = this.state;

      return (
        <div>
          <ReactTable
            manual
            pages={pages}
            loading={loading}
            onFetchData={this.fetchData}
            defaultPageSize={10}
            filterable={true}
            data={data}
            columns={columns}
          />
        </div>        
      );
   }
}

export default ResultsContainer;

在这种情况下,当浏览器加载页面时,它会调用api并将数据加载到react-table中。如何在页面加载期间阻止react-table加载数据,而不是在用户单击页面上的链接时加载数据。

getColumnHeaders的实现:

export function getColumnHeaders(searchType) {
   let columns = [];

if (searchType === 'RETURN_SEARCH') {

  columns =  [
      {
         Header: "Case",

         accessor: "caseId"
      },
      {
         Header: "Ticket",

         accessor: "ticketNo"
      },
      {
         Header: "Reason",

         accessor: "reason"
      }
      {
         Header: "Seller Name",

         accessor: "UserName"
      }
 ]

} else {
      //implement this feature later
    }

   return columns;
}

提前感谢您的任何帮助,建议。

1 个答案:

答案 0 :(得分:0)

您可以使用 react 将数据加载到表中。我做了一个例子

<tbody id="tblcustomer">
          {this.state.tableInData.map(obj => {
              return(
                <tr>
                <td >{num++}</td>
                <td >{obj[1]}</td>
                <td >{obj[2]}</td>
                <td >{obj[3]}</td>
                <td >{obj[4]}</td>

                </tr>
              )
        
          })}
          </tbody>