Trigger/activate the RowEditor from bean for a primefaces In-Cell editing enabled p:dataTable

前端 未结 9 856
时光说笑
时光说笑 2021-01-02 10:16

I have a primefaces p:dataTable with InCell editing enabled and want to trigger/activate the RowEditor for the newly added row.

Excerpt of XHTML

9条回答
  •  离开以前
    2021-01-02 10:30

    This solution aims to resolve a few drawbacks of the original answer of:

    oncomplete="jQuery('.ui-datatable-data tr').last().find('span.ui-icon-pencil').each(function(){jQuery(this).click()});"
    

    The above statement retrieves all "tr" elements which are descendants (at any level) of elements with the ".ui-datatable-data" class. The potential issues with that are:

    1. As mentioned by @Gnappuraz, if there are multiple p:datatable's in the document, this statement will choose the last table.
    2. Additionally, I ran into a problem where even with one datatable, a column with a p:selectOneRadio component will also render a html table (that contains "tr" elements). In this case the selector chooses the last "tr" for the p:selectOneRadio's table, rather than the p:datatable.
    3. Not a problem, but the statement can be shortened to exclude the each() because of jQuery's implicit iteration.

    What I ended up with was:

    oncomplete="jQuery('#tableForm\\:table .ui-datatable-data > tr').last().find('span.ui-icon-pencil').click();"
    

    This selector says - get the last "tr" element, which is a direct child of an element with class ".ui-datatable-data", which is also a descendant of the element with id "table" in form "tableForm". In other words you can now have multiple p:datatables, and any include any components that render html tables.

提交回复
热议问题