Wilkin Ho
Wilkin Ho

Reputation: 178

Primefaces datatable with checkbox selection ONLY

In my project, I use datatable with <p:column selectionMode="multiple" /> so that the entries can be multiple selected using the checkboxes.

However, when the user click on a row, all previous selection is unselected and only that row is selected. This behaviour is unexpected and annoying. I would like to disable the behaviour of rowSelect and rowUnselect on row clicking, but seems I had no way to do but hacking the source of datatable.js.

Does anyone implement this before? Thanks for answering.

Using:

primefaces 3.5

mojarra 2.1.6

glassfish 3.1.2.2

Upvotes: 10

Views: 14508

Answers (2)

Sumeet
Sumeet

Reputation: 9

Put this in your page, might help stopping selection when you click a row.

$(document).ready(
    function() {
        $("tr td").click(function(event) {
            if (!$(this).find("div").hasClass("ui-chkbox") 
                  || $(this).find("div").find("div").hasClass("ui-state-disabled")
                  || this == event.target) {
                event.stopPropagation();
        }
    });
});

The last condition (this == event.target) is needed when clicking the checkbox column itself outside of the checkbox.

Upvotes: 0

YAM
YAM

Reputation: 1382

According to this feature request, this feature is now supported in Primefaces versions 5.0.3 & 5.1 by simply adding rowSelectMode="checkbox" to the datatable.

Documentation reference (PF 5.1):

Use rowSelectMode option to customize the default behavior on row click of a multiple selection enabled datatable. Default value is "new" that clears previous selections, "add" mode keeps previous selections same as selecting a row with mouse click when metakey is on and "checkbox" mode allows row selection with checkboxes only.

Upvotes: 17

Related Questions