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

6 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
  2. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Disability niche

    ReplyDelete
  3. It is essential to verify your gain is considered "base" gain, as base advantages are absolutely unbiased and could now no longer be decreased with the aid of using gain from social protection or different authorities programs.disability articles

    ReplyDelete
  4. Support groups do not focus on "group process" and therefore are not meant to uncover or treat the psychological or pathological dynamics of the members NDIS Providers Kingscliff

    ReplyDelete
  5. Training can last anywhere from six weeks to two years depending on the desired career and area of specialization. cna classes online

    ReplyDelete

LinkWithin

Related Posts Plugin for WordPress, Blogger...