0

I am using BootstrapTable from react-bootstrap-table and taking data from external API. Each time I get an object of 30 items. Therefore the pageSize is 30 but I get the totalPages variable from API which is let's say 6. Unfortunately, the table data is each time 30 so there is just one page enter image description here

I would like to tell the table how many pages I want to have - 6 - and each time onClick in the pagination item I will call another API link. How can I achieve this?

  onPageChange = page => {
    alert(`Let's go to page: ${page}`);
  };

  render() {
    const options = {
      onPageChange: this.onPageChange,
      hideSizePerPage: true,
      page: 1,
      sizePerPage: this.props.pageSize,
      paginationSize: 6,
    };
    return (
      <Card>
        <CardHeader>
          <h1> Sales report</h1>
        </CardHeader>
        <CardBody>
          <BootstrapTable
            data={this.props.sales}
            version="4"
            striped
            hover
            pagination
            options={options}
            keyField="Type"
          >
            {tableHeaders.map((header, index) => (
              <TableHeaderColumn key={index} dataField={header}>
                {header}
              </TableHeaderColumn>
            ))}
          </BootstrapTable>
        </CardBody>
      </Card>
    );
  }
Alex Ananjev
  • 269
  • 3
  • 16
jake-ferguson
  • 315
  • 3
  • 11
  • 32

1 Answers1

4

You have to pass options object and specify exact number of items and callback by clicking on each page.

For example:

class PaginationHookTable extends React.Component {
  constructor(props) {
    super(props);

    this.options = {
      onPageChange: this.onPageChange,
      sizePerPage: 6,
    };

    this.state = {
        data: [],
    }
  }

  onPageChange = (page, sizePerPage) => {
    alert(`page: ${page}, sizePerPage: ${sizePerPage}`);

    // make another url
    const url = `http://someurl.com/api/endpoint?page=${page}`;

    // make request and update state with new data
    // axios just for example
    axios.get(url).then(resp => this.setState({ data: resp });
  }

  render() {
    return (
      <div>
        <BootstrapTable
          data={this.state.data}
          options={this.options}
          remote={true}

          // you need to use your number of total from backend
          // instead of 100 ofc
          fetchInfo={{ dataTotalSize: 100 }}
          pagination>
          <TableHeaderColumn dataField='id'>Product ID</TableHeaderColumn>
          <TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
          <TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
        </BootstrapTable>
      </div>
    );
  }
}

UPDATE: You have to pass additional props: remote={true} and fetchInfo: { dataTotalSize: 100 } to specify total number of items.

See more here.

Denys Kotsur
  • 2,579
  • 2
  • 14
  • 26
  • I saw this example and use part of this code, with onPageChange as well, but it doesn't solve my issue(or I don't get what you mean).The problem is that data is 30 items, page size is 30 items as well but I want to have this pagination because I will get from API another 30-items-data on page change. So page 1 -API1, page 2 -API2 etc. With your code I think there will still be 1 pagination item because table thinks that just 1 is needed – jake-ferguson Aug 16 '18 at 09:26
  • I edited the anwer - added code. So basically the react-bootstrap-table doesnt care about paginationSize: 6, because the data is 30items and pageSize is 30items as well – jake-ferguson Aug 16 '18 at 09:30
  • @jake-ferguson are you using first version of `react-bootstrap-table` ? – Denys Kotsur Aug 16 '18 at 09:32
  • You don't need to use `paginationSize` - it's for displaying bar purpose only. I've updated answer how to call another api url. For specify how many items should be per page use `sizePerPage`. Also, be sure that you're passing the correct value as prop here `sizePerPage: this.props.pageSize`. I've created codesandbox for you - check it out - [click here](https://codesandbox.io/s/l51r7znxxq). – Denys Kotsur Aug 16 '18 at 09:45
  • in this sandbox the same happens as in my example - if we change sizePerPage to 2 there is just on page - one item in pagination – jake-ferguson Aug 16 '18 at 09:59
  • @jake-ferguson I've updated my answer in **UPDATE** section. – Denys Kotsur Aug 16 '18 at 10:09
  • I have one more issue with pagination,in the end I must start with 0 as first page index and it looks for my table like: https://imgur.com/CxPqtIJ. Can the first (numer1) be page=0? how to do this? – jake-ferguson Aug 23 '18 at 06:13
  • @jake-ferguson You need to display 0? If not, you can just subtract 1 before making request to the server. – Denys Kotsur Aug 23 '18 at 06:19
  • no when I have in my store page=0 and take from backend data from page 0 I want the pagination component to display 1 etc – jake-ferguson Aug 23 '18 at 06:33
  • I understand, you should before sending request for next data subtract 1, like this: `onPageChange = (page) => {axios.get(\`https://someurl.com/api?page=${page-1}\`).then(...) }` – Denys Kotsur Aug 23 '18 at 06:35
  • @jake-ferguson or just pass in option object calculated `page` prop: `const options = { page: this.state.currentPage + 1 // Showing page 1 if currentPage 0 };` – Denys Kotsur Aug 23 '18 at 06:40
  • Well I did this and it works just at the beginning. in options I have `page: this.props.filters.page + 1` , and when entering table it shows properly 1, but when I move to page number 2 it marks and shows as it was 3, when move to 1 it shows 2... – jake-ferguson Aug 23 '18 at 06:42
  • and here my onPageChange function ` onPageChange = page => { this.props.changePage(page); this.props.getSales(); };` – jake-ferguson Aug 23 '18 at 06:43
  • Okey I just substracted 1 in the function `this.props.changePage(page - 1)` and its fine now, thank you again – jake-ferguson Aug 23 '18 at 06:45
  • no, sorry, it doesnt ;/ https://stackoverflow.com/questions/51984415/react-bootstrap-table-pagination-from-api-starts-with-0 – jake-ferguson Aug 23 '18 at 11:26