Tuesday, October 28, 2014

How to disable row selection in PrimeFaces datatable

This is not a official/proper way to disable the row selection in <p:dataTable />
But a workaround since there is no way to disable the row selection at the moment.

assuming we have the following dataTable.
<p:dataTable
    value="#{myBean.resultList}"
    selection="#{myBean.selection}"
    var="var"
    ..... />

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

</p:dataTable>

To disable row selection in the above <p:dataTable />
we need to add <p:ajax /> into the <p:dataTable />

1. add the rowSelect event into dataTable
<p:dataTable id="myTable"
    value="#{myBean.resultList}"
    selection="#{myBean.selection}"
    var="var"
    ..... />

    <p:ajax event="rowSelect"listener="#{myBean.unselectDisabledRow}" update="myTable" />
    <p:column selectionMode="multiple" />
    <p:column .... />

</p:dataTable>vvv

2. remove the "disabled" from selection
public void unselectDisabledRow(AjaxBehaviorEvent e) {

    for(MyObject myObject : resultList) {
        if(disable condition) {
            resultList.remove(myObject);
        }
    }
}

The idea is to remove the row that we wanted to disable from the selection immediately.
p/s: more <p:dataTable /> selection events here.


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...