PrimeFaces: how can I dinamically check a p:column?

UI Components for JSF
Post Reply
otuka
Posts: 5
Joined: 21 Nov 2011, 19:28

21 Nov 2011, 19:40

I have a <p:dataTable> with a <p:column selectionMode="multiple" />. When I have a single row displayed in the dataTable, I want my application to automatically check this row (and also the "checkAll" checkbox displayed on the table header).
I've done this via JavaScript and it visually worked. However, when submitting the form, it seems the "checked" value isn't submitted. If I click on the checkbox twice (to uncheck and check it again), it works.

Code: Select all

<p:dataTable var="invoiceGroup"
             selection="#{chargeBean.selectedInvoiceGroupBeans}"
             value="#{chargeBean.invoiceGroupList}"
             rowKey="#{invoiceGroup.id}">
    <p:column selectionMode="multiple" />
    ...
</p:dataTable>

Code: Select all

function selectTableElements() {
    var checkBoxes = $('#invoiceGroupTable td.ui-selection-column input[type="checkbox"]');
    var paginatorPages = $('#invoiceGroupTable .ui-paginator .ui-paginator-pages .ui-paginator-page'); 
    if (checkBoxes.length === 1 && paginatorPages.length === 1) {
        setTimeout(function () {
            $('#invoiceGroupTable th input[type="checkbox"]').click();
        }, 10);
    }
}
Is there a way of doing this server-side?

Primefaces 3.0M2 - JSF 2.0 - JBOSS 4.2.3.GA

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

21 Nov 2011, 20:22

Code: Select all

<p:dataTable var="invoiceGroup"
             selection="#{chargeBean.selectedInvoiceGroupBeans}"
             value="#{chargeBean.invoiceGroupList}"
             rowKey="#{invoiceGroup.id}">
    <p:column selectionMode="multiple" />
    ...
</p:dataTable>
I think you can do the following server side (or something similar), or this is what I would do. I like using AJAX to do everything server-side, and honestly, my goal is to do NO javascript at all. Years ago, when I first did HTML/Javascript/server-side coding via Powerbuilder, I fell in love with JavaScript, and tried to do JavaScript as much as possible, but now with Java/J2EE/JSF/PrimeFaces, my goal is to avoid Javascript!!! 8-)

This should meet the requirement of the use case you described. I handwritten code below just now; i prefer to copy/paste my code, if I have one similar.

Code: Select all

public <datatype???> getInvoiceGroupList () {
    ...
    if (invoiceGroupList.size() == 1) {
        selectedInvoiceGroupBeans[0] = invoiceGroupList.get(0);
    }
    return invoiceGroupList;
}
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

otuka
Posts: 5
Joined: 21 Nov 2011, 19:28

21 Nov 2011, 20:58

It didn't work. When the table gets rendered (after an event), it still shows the checkboxes unchecked.
Debugging, the method getInvoiceGroupList, it successfully sets the array of elements. However, the method getSelectedInvoiceGroupBeans isn't called.

That's what I've done by now:
* with JavaScript, checking the amount of elements. If it's == 1, then check it and disable the uncheck.
* on Server Side, always consider the element checked if the list contains only one element.

Am I doing something wrong? Why isn't the method getSelectedInvoiceGroupBeans() being called?

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

21 Nov 2011, 21:27

Please copy/paste your entire bean and xhtml/jsf file, and I'll take a look and advise further.
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 48 guests