简体   繁体   中英

Conditional Pagination on Datatable

I need conditional pagination on data-table, that is, need to add pagination only if the number of records is > say 5.

The reason is when the pagination is included in the data table, it occupies the space of a row on screen. In maximum cases, the number of rows would be 5 only. So, the intent is to save the screen space for these majority cases.

According to primefaces' documentation:

set dataTable paginator like this

 paginator="true" rows="5" paginatorAlwaysVisible="false"

- the paginator will be visible only if there are more than 5 records in the table.

Say you've got this dataTable (from the showcase ):

<p:dataTable id="dataTable" var="car" value="#{tableBean.cars}"  
             paginator="true" rows="10"  
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
             rowsPerPageTemplate="5,10,15"> 

You could turn the paginator conditionally off like this:

<p:dataTable id="dataTable" var="car" value="#{tableBean.cars}"  
             paginator="#{tableBean.exceedsFive}" rows="10"  
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
             rowsPerPageTemplate="5,10,15"> 

The bean:

public boolean isExceedsFive() {
    return cars.size() > 5;
}

Notice the reference with EL in paginator="" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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