PandaMastr
PandaMastr

Reputation: 695

"material-table" - How to add more rows per page and fix rendering - ReactJs

I am trying to apply more rows per page to my table from material table. Until now I found out that you can add an array with the wishful rows per page ex. rowsPerPageOptions={[5, 10, 25, 50, 100]} , but my problem is when I apply the row of 100 the table extends to a blank one. ( I receive at the moment only 24 rows (documents) from the backend ) So basically it has to be limited to the data that I have. Can someone give me a hand? Thank you

 divideItems = () => {

        let startIndex = ((parseInt(this.state.activePaginationTab) + 1) * this.state.rowsPerPage) - this.state.rowsPerPage;
        let endIndex = (parseInt(this.state.activePaginationTab) + 1) * this.state.rowsPerPage - 1;
        let tabItems = [];

        for (let i = startIndex; i <= endIndex; i++) {
            if (this.state.items[i]) {
                tabItems.push(this.state.items[i]);
            }
        }


        this.setState({
            tabItems
        }, () => {

        });

    }
    getNewIndex = (event, page) => {
        this.setState({
            activePaginationTab: page
        }, () => { this.divideItems() })
        // this.divideItems(page)


    };

    handleChangeRowsPerPage = (event) => {

        this.setState({
            rowsPerPage: event.target.value
        }, () => {
            this.divideItems();
        })
    }
render() {
  components={{
              Pagination: props => (
                  <TablePagination
                      {...props}
                      rowsPerPageOptions={[5, 10, 25, 50,100]}
                      rowsPerPage={this.state.rowsPerPage}
                      count={this.state.items.length}
                      />
    ),

Upvotes: 7

Views: 18640

Answers (3)

DevLoverUmar
DevLoverUmar

Reputation: 13933

We can add rows per page and further pagination options as

   <MaterialTable
      title=""
      columns={myColumns}
      data={myData}      
      options={{
        paging:true,
        pageSize:6,       // make initial page size
        emptyRowsWhenPaging: false,   // To avoid of having empty rows
        pageSizeOptions:[6,12,20,50],    // rows selection options
      }}
    />

Update 2023

As per the comments by Zolbayar and Jeremy options.pageSizeOptions is replaced with muiTablePaginationProps.rowsPerPageOptions. See latest-docs

<MaterialReactTable
  columns={columns}
  data={data}
  muiTablePaginationProps={{
    rowsPerPageOptions: [5, 10],
    showFirstButton: false,
    showLastButton: false,
  }}
/>

Upvotes: 21

Abdulhakim Zeinu
Abdulhakim Zeinu

Reputation: 3829

Add these attribute in the options section of the material table

    pageSize:10
    pageSizeOptions:[10,20,30]
  • PageSize: Number of rows that would be rendered on every page

  • pageSizeOptions: Page size options that could be selected by user

     options={{
         pageSize:10,
         pageSizeOptions:[10,20,30],
    

    }}

Upvotes: 5

Domino987
Domino987

Reputation: 8774

As mentioned in the docs you can use emptyRowsWhenPaging and set it to false in the options.

Upvotes: 5

Related Questions