3

Is there a way in Primefaces to enable or disable a checkbox in a datatable? Right now my datatable is like this:

<p:dataTable id="dTable" var="tt" value="#{aBean.aList}" selection="#{aBean.selectedValue}" rowStyleClass="#{tt.state.intValue() le 1 ? 'active' : 'passive'}">
    <p:column selectionMode="multiple" />
    ...
</dataTable>

but this code just puts a column with checkboxes together with a checkbox in the header. I want checkboxes to be rendered according to a value in the backing bean.

Vixed
  • 3,429
  • 5
  • 37
  • 68
lamostreta
  • 2,359
  • 7
  • 44
  • 61

3 Answers3

9

FWIW many years later...

In PF 5.x you know longer do this in the column, you have to set up a disabledSelection criteria in the <p:datatable ...> tag.

Something like :

<p:datatable disabledSelection="#{myvar ne null}" ... >

This took me quite a bit to figure out since often questions (everywhere) are posed and answered without regards to version. Here is where I finally found the answer.

Optimus Prime's response on PF Community Forum

Dilapidus
  • 413
  • 5
  • 13
  • Thank you for the answer. Yes you're right, I should've given the version.. Now you have provided the answer acc to new version, I have selected your answer as the right answer. – lamostreta Nov 02 '15 at 08:02
  • @lamostreta Thank you! That's my first. :-) I wasn't really calling you out on that, TBH. It's true everywhere which seems weird with some 3 major versions in use. – Dilapidus Nov 02 '15 at 19:06
7

I found the solution inside PF here: http://forum.primefaces.org/viewtopic.php?f=3&t=14029 Usage of rowstyleclass and css solved the problem:

xhtml:

<p:dataTable id="dTable" var="tt" value="#{aBean.aList}" 
                                  selection="#{aBean.selectedValue}" rowStyleClass="#{tt.state.intValue() le 1 ? 'active' : 'passive'}">                    

<p:column    selectionMode="multiple"/>
...
</dataTable>

css:

.active{    
    background-image: none !important;   
}

.passive{
    background-color:gainsboro !important;
    background-image: none !important;
}

.passive td.ui-selection-column input {
       display:none;
    }

This enables/disables command button using CSS.

lamostreta
  • 2,359
  • 7
  • 44
  • 61
2

try out the disabledSelection of the <p:column

You can always do it without the , just do it manually... by placing

And you can make use of the

rowIndexVar : Name of iterator to refer each row index.

for referring the row number

And here is how the selection column should look like (more or less)

used the checkall class in order to delegate it later on using jQuery and do the select all magic

<p:column id="selection_column">
    <f:facet name="header">
        <h:selectBooleanCheckbox value="" class="checkall"/>
    </f:facet>
    <h:selectBooleanCheckbox rendered="#{myRow.selectable}" value="#{myRow.selected}"/>
</p:column>



jQuery(window).load(function() {
    jQuery(document).delegate(".checkall", "click", function(event) {
        jQuery(this).closest("table").find(':checkbox').attr('checked', this.checked);
    });
});
Daniel
  • 36,833
  • 10
  • 119
  • 200