1

in my page i have :

                    <p:dataTable id="myTable" lazy="true" widgetVar="myTable" value=".." paginator="true" paginatorPosition="bottom" rows="10" ...>
                    <p:ajax event="page" oncomplete="myTable" />
                    ...
                    </p:dataTable>

in my bean :

                    ...
                    private LazyDataModel list;
                    ...                     
                    public void search() {
                        list = new LazyDataModel<SomeDTO>() {
                            private static final long serialVersionUID = 1L;
                            List<SomeDTO> result = null;

                            @Override
                            public List<SomeDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {                                    
                                List<SomeDTO> result = service.search(searchedName, first, pageSize);
                                // SOME CODE HERE FOR PAGINATION ?? BUT WHAT
                                return result;
                            }

                        };
                        // HOW TO DELETE THIS EXTRA REQUEST
                        list.setRowCount(service.search(searchedName));
                    }

my question is how to setRowCount from result (size+1). i dont want to get the count from DB, because i'm sure we can calculate the rowCount without requesting DB.

Gogo
  • 292
  • 8
  • 19

3 Answers3

2

PrimeFaces did not support this out of the box. A fix has been checked in to trunk on Feb 11th 2016, tagged 6.0 (so it should at least be in the current 6.0RCx releases). I'm not sure if it is in the Elite release >=5.2.20 or >=5.3.7 (from Feb 12th)

One important reason for this not working is that the updated rowCount you might do in the load method serverside is not applied to the paginator client side. However, since it is transferred from the server to the client, you can update it in the oncomplete of each ajax call. In fact, that is a large part of the patch (the other part is reading the value from the ajax response).

Combined calling this in e.g. the oncomplete of a ajax page event will solve the issue:

function updatePaginator(xhr, status, args) {
    var paginator = PF('DataTableWidgetVar').paginator;
    paginator.cfg.rowCount=args.totalRecords;
    paginator.cfg.pageCount = Math.ceil(value / paginator.cfg/rows)||1;
    paginator.updateUI();
}

You can then in each call in the load method, - Try to read pagesize+1 records - Set the count to this if you can read pageSize+1 (but still return pageSize records) - Set the count to the number of rows read if they are pageSize or less.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • if you look to my solution i have : this command refreash the datatable after ajax page. i have tested it and it work perfectly. if you arn't sure in your answer dont post it. – Gogo May 13 '16 at 08:27
  • there are tow requests one for p:ajax page event and other for p:remoteCommand. but this is not a problem.my problem is avoid requesting database to get the count row not datatable. – Gogo May 13 '16 at 13:12
  • this is another problem with p:remoteCommand the load of datatable is executed twice.. do you know how to refresh the datatable without p:remoteCommand i have tried update="datatable" but not work ?? – Gogo May 13 '16 at 14:55
-1

Ok i got it. first in your page you need this:

                    <p:remoteCommand name="updateMyTable" update="#{p:component('myTable')}" process="@this" />
                    <p:dataTable id="myTable" lazy="true" widgetVar="myTable" value=".." paginator="true" paginatorPosition="bottom" rows="10" ...>
                    <p:ajax event="page" oncomplete="updateMyTable()" />
                    ...
                    </p:dataTable>

in your bean change the load method like this:

                            @Override
                            public List<SomeDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {                                    
                                List<SomeDTO> result = service.search(searchedName, first, pageSize);
                                if (first <= pageSize) {
                                    this.setRowCount(this.getRowCount() + result.size() + 1);
                                } else if (result != null && result.size() > 0 && (result.size() >= pageSize)) {
                                    this.setRowCount(this.getRowCount() + result.size());
                                }
                                return result;
                            }
Gogo
  • 292
  • 8
  • 19
  • This updates the datatable. twice. The queries are executed twice to... Not something you'd want I think. – Kukeltje May 12 '16 at 16:13
  • no Mr ho know everything, read out the problem and the solution provided before judging persones.apparently you don't know primefaces datatable lazy lodoading and how it work!! – Gogo May 13 '16 at 08:24
  • First of all, I'm not judging persons, I'm judging the answers. While it might technically work, it is not an efficient solution and not needed. Remarkable that you say about reading answers. **I'm the one that created the 'accepted' PrimeFaces patch referred to in my answer, did you notice?** I have multiple other improvements suggested for the PrimeFaces datatable in the forums and older issuelist, including a about datatables nested in a repeat. I have those working for a long time in my environment, so I **know** lazy loading**. So please get the records straight **before judging persons** – Kukeltje May 13 '16 at 11:26
  • That the datatable is updated twice here is not a problem of the `remoteCommand`. 1 Update is caused by the basic paging, the second one due to *you* calling a remoteCommand that updates the datatable as you state. So it works as implemented (by you). If you want a full update of the datatable so you can also update headers/footers, then there are several questions related to this in StackOverflow, but that is a different question – Kukeltje May 13 '16 at 15:48
-1

Primefaces pagination Lazy DataTable necessary two queries, a query to return the results of a page as limiting, and another query to know the total size of data.

rabah leghettas
  • 117
  • 1
  • 6
  • 1
    the two queries are not necessary, we can calculate the number of page dynamically from the size of returned list by our service + 1, the advantage of this solution is we don't have to duplicate the service methode to get count from db, especially for a very complex search queries. – Gogo May 12 '16 at 14:35