Bug or Feature: table selection in header

UI Components for JSF
Post Reply
Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

21 Sep 2017, 14:53

Hi,

i currently have the problem with a selection in a datatable. I tried to use the primefaces selection mode, but the header selection checkbox only selects the entries on the first page. The other rows from different pages will not be selected:

Code: Select all

<p:dataTable
   id="myTable"
   widgetVar="myTable"
   var="entry"
   rowKey="#{entry.id}"
   selection="#{mySessionBean.selected}"
   rows="#{mySessionBean.pageSize}"
   rowsPerPageTemplate="#{mySessionBean.pageSizeTemplate}"
   reflow="true"
   rowHover="true"
   paginator="true"
   paginatorPosition="BOTH"
   paginatorAlwaysVisible="false"
   pageLinks="10"
   caseSensitiveSort="false"
   sortMode="single"
   value="#{mySessionBean.entries}">
   <p:ajax event="sort" listener="#{mySessionBean.tableSettings.prepare}" />

            <p:column selectionMode="multiple" style="width:16px;text-align:center" />
What is partly working is the following code, but the "broken" checkbox still stays on the page, so i have 2 selection boxes in my header:

Code: Select all

            <p:column selectionMode="multiple" style="width:16px;text-align:center">
                <f:facet name="header">
                    <p:selectBooleanCheckbox onchange="PF('myTable').selectAllRows();" />
                </f:facet>
            </p:column>
So my question now is, is it a bug that header checkbox only selects entries from the first page, or is it wanted behaviour?
If it is wanted behaviour, how can i achieve the selection of all entries in my bean, instead of only the entries on the first page?

Wbr,

Daniel
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

22 Sep 2017, 00:03

Is as intended and like in many (most?) other datatables I know of and it should never by default select all on all pages (dangerous!). Implementing a selection of all on all pages could be done by detecting that 'all' on this page is seleted (via ajax) and opening a panel (no dialog since it is to much in your face) and asking the user if he/she/it/... wants to select all on all pages. Google does (or at least did) the same in GMail. How you should actually select really all is up to you (but not to difficult)

Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

22 Sep 2017, 08:09

It looks inconsistent to me, because the other header functions (search, filter) do it for the whole table.

But if it is not complicated, can you give me a hint? Because my first try ended up in two select boxes 🙁

Thanks

Daniel
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

22 Sep 2017, 10:24

IMHO it is not inconsistent (maybe a little). Filtering and sorting always needs to be done on the whole dataset. How would you otherwise page trough 'sorted' or filtered items? All datatables I know of work this way (unless you have client-side 'filtering/sorting' over the just displayed result set, but then there is very often an external filtering already done.

And yes, you need a two step selection (unless you want to blindly select all on all pages if the user just checks the 'select all checkbox' on the datatable. If the second one needs to be a checkbox or just a link that can be clicked or whatever is just up to you. I'd use the 'toggleSelect' ajax event for this.

Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

22 Sep 2017, 12:17

Thanks for the hint, but just implementing an ajax event does not do the trick (or am i missing something)?

My ajax event (which will be called so far):

Code: Select all

    public void toggleSelected() {
        if(getSelected().isEmpty()) {
            setSelected(new ArrayList<>(getEntries()));
        } else {
            setSelected(null);
        }
    }
And my ajax event in XHTML:

Code: Select all

<p:ajax event="toggleSelect" listener="#{mySessionBean.toggleSelected}" />
What is working:
  • The event listener will be called on each select-box click (header)
  • The whole dataset (all values) will be added as selected (on second click)
What is not working:
  • Only the first page in view become selected
  • All other pages (pagination) are still not selected
  • Before the listener toggleSelected() will be called, selected list already includes entries from the first page
PS: Why is the header facet not overriding the default header? That would be more inutitive for me and you could implement your own select box by using the wanted JavaScript functions (selectAllRows() instead of selectAllRowsOnPage() in my case)?

Wbr,

Daniel
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

22 Sep 2017, 13:33

I hopefully find a workaround for my problem. I only have to take also JavaScript into the toggle-Event implementation to select/unselect all entries.

Thanks for the hint, that pointed me in the right direction :lol:

The code for others (if another has the same problem):

Code: Select all

    public void toggleSelected(ToggleSelectEvent tse) {
        RequestContext rc = RequestContext.getCurrentInstance();

        if(tse.isSelected()) {
            setSelected(new ArrayList<>(getEntries()));
            rc.execute("PF('myTable').selectAllRows();");
        } else {
            setSelected(null);
            rc.execute("PF('myTable').unselectAllRows();");
        }
    }
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 26 guests