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!!

2 comments:

  1. It's a wrong approach, the code on the section 2 will throw a Concurrent Modification Exception.

    See at:

    http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

    ReplyDelete
    Replies
    1. it's already mentioned this is not the proper way.
      but It's working fine for me,
      btw, I'm using PF3.5 for this implementation

      Delete

LinkWithin

Related Posts Plugin for WordPress, Blogger...