primeng icon indicating copy to clipboard operation
primeng copied to clipboard

DataTable paging dynamic number of rows

Open Bernd0 opened this issue 10 years ago • 15 comments

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

Bernd0 avatar Apr 28 '16 13:04 Bernd0

There is rowsPerPage property that displays a dropdown with available options.

cagataycivici avatar Apr 28 '16 14:04 cagataycivici

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.

Bernd0 avatar Apr 28 '16 14:04 Bernd0

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.

cagataycivici avatar Apr 29 '16 14:04 cagataycivici

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.

MikeUC avatar Feb 09 '17 12:02 MikeUC

Same problem here. How can I adjust max number of rows to viewport height?

bbonczek avatar Mar 10 '17 10:03 bbonczek

Same problem here. Please re-open.

slugworth avatar Nov 09 '17 19:11 slugworth

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.

slugworth avatar Nov 10 '17 18:11 slugworth

+1 here!

matiaslabra avatar Dec 01 '17 15:12 matiaslabra

me too, please!

tifcohen avatar Dec 10 '17 15:12 tifcohen

+1 Seems that this feature is needed :) Can any developer let us know if there is a work-around/solution for this? Thanks !

LucaAndrei avatar Jan 25 '18 11:01 LucaAndrei

me too ! Please !

FabienLH avatar Nov 12 '18 23:11 FabienLH

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.

DonnyDepp avatar Jan 25 '19 09:01 DonnyDepp

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

Muji86 avatar Jul 12 '19 14:07 Muji86

+1

stephanebouget avatar Feb 09 '24 08:02 stephanebouget

+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);
      }
 }

niklsss avatar Sep 13 '24 14:09 niklsss