DataTable paging dynamic number of rows
Hi,
I have a data table with lazy loading. I define the 'rows' property, e.g. to 20. But there is enough room for 33 rows, depending on the browsers size. So it need to be responsive.
Is it possible to set the number of rows automatically, in order to show as much as feasible?
Best regards, Bernd
There is rowsPerPage property that displays a dropdown with available options.
I know this feature. But this is not what I'm looking for. I'm looking for an option like 'rows=auto' or an method 'adjustRows(height)' which would calculate the best number of rows based on passed height.
I think it is quite complex and not a common requirement. If there is more demand in future, we'll reopen. Thank you for your feedback.
Hi Cagatay,
I have the same problem and from your answer I get that this functionality will not be implemented soon.
What workaround you would suggest for this problem, having in mind, that you know the PrimeFaces code Best?
Thanks in Advance,
Best Regards,
Mike.
Same problem here. How can I adjust max number of rows to viewport height?
Same problem here. Please re-open.
Alternatively, I believe I could do this myself if I could configure the PrimeNG DataTable to enforce the same row height for every row. But as far as I can tell this isn't possible either, no?
What I mean is that if one row has become larger vertically due to containing more content, then I would want all rows to resize to that same size. Currently this happens on a per-row basis.
If I can reliably know that all rows will be the same height, then I should be able to get the row height from the DOM element, the viewport height, consider the header and footer heights, and then calculate/control the number of rows in the table.
+1 here!
me too, please!
+1 Seems that this feature is needed :) Can any developer let us know if there is a work-around/solution for this? Thanks !
me too ! Please !
Of course there is a workaround.
In your Mainpage u set:
<h:head>
.....
<script type="text/javascript">
window.onresize = setResolution;
function setResolution() {
document.getElementById('bodyForm:screenHeight').value = window.innerHeight;
document.getElementById('bodyForm:screenWidth').value = window.innerWidth;
}
</script>
.....
</h:head>
<h:body onload="setResolution();">
....
<h:form id="bodyForm">
<h:inputHidden id="screenHeight"
value="#{sessionController.screenHeight}" />
<h:inputHidden id="screenWidth"
value="#{sessionController.screenWidth}" />
</h:form>
....
</h:body>
Sessioncontroller:
@Named(value = "sessionController ")
@SessionScoped
public class SessionController {
private float screenHeight;
private float screenWidth;
public float getScreenHeight() {
return screenHeight;
}
public void setScreenHeight(float screenHeight) {
this.screenHeight = screenHeight;
}
public float getScreenWidth() {
return screenWidth;
}
public void setScreenWidth(float screenWidth) {
this.screenWidth = screenWidth;
}
}
Datatablecontroller:
@Named(value = "dtController")
@SessionScoped
public class DtController {
public int calculateRows() {
int pixelsuntilfirstrow = 200;
int rowheightpixels = 60;
int rowspace = (int) (sessionController.getScreenHeight() - pixelsuntilfirstrow);
int rows = rowspace / rowheightpixels;
return rows;
}
}
Datatable:
<p:dataTable ..... rows="#{dtController.calculateRows()}" >
You only need screenHeight for this, but width also can come in handy. But i still have issues when setting the datatable multiViewState="true". Then, there is no row refreshing when refreshing the datatable :( Not sure if this is a bug or i just missed an event.
Here's my solution:
Declare variable "maxRows" in the component and bind it to the rows input on the p-table template:
maxRows = 20 //my default value
<p-table #dt [columns]="cols" [value]="records" [autoLayout]="true" [paginator]="true" [rows]="maxRows">
Then add a host listener to recalculate the max number of rows:
@HostListener('window:resize', ['$event'])
calculateRows() {
// Find the top postion for the table body
const topPos = document.querySelector('tbody').getBoundingClientRect().top;
// Calculate remaing space available
const available = window.innerHeight - topPos;
// Divide by row height (36px is default) and remove 2 rows to allow for horizontal scroller and pagination
this.maxRows = (available / 36) - 2;
}
Additionally you will need to call calculateRows() once you've loaded the rows, I'm doing it this way:
@Input()
set data(records: any[]) {
this.records = records;
setTimeout(() => this.calculateRows(), 100);
}
Where my component receives the data as an input and sets it on the records attribute
+1
+1
@Muji86, changed your code because when you zoomed out the quantity did not decrease but increased
@HostListener('window:resize', ['$event'])
calculateRows() {
const firstRow = document.querySelector('tbody tr');
if (firstRow) {
const rowHeight = firstRow.getBoundingClientRect().height;
const topPos = document.querySelector('tbody').getBoundingClientRect().top;
const availableHeight = window.innerHeight - topPos;
this.maxRows = Math.floor((availableHeight / rowHeight) - 2);
}
}