JSF PrimeFaces - Save value of selected tablecell on buttonclick -
prerequisites:
- jsf 2.1
- primefaces 5.2
- glassfish 3.1
story:
i got datatable displaying editable values. amount of columns dynamic. in same form button calling save-action of bean save edited data. function self working fine
implementation:
<p:datatable id="datatable" scrollable="true" widgetvar="wigvardatatable" value="#{mybean.datalist}" var="data" editable="true" editmode="cell" rowkey="rowkey"> <p:columns var="col" value="#{mymodel.columnlist}" style="font-size: 12px"> <f:facet name="header"> <h:outputtext value="#{col.header}" /> </f:facet> <p:celleditor> <f:facet name="output"> <h:outputtext value="#{mymodel.getvaluefortablecell(data, col).value}" /> </f:facet> <f:facet name="input"> <p:inputtext id="inputtxt" value="#{mymodel.getvaluefortablecell(data, col).value}" style="width:100%"> </p:inputtext> </f:facet> </p:celleditor> </p:columns> </p:datatable>
problem:
when pressing save button while table cell being edited , didn't loose focus yet, new value inside inputtext of tablecell won't written bean , due won't saved.
question:
how can write data backing bean, before button action executed?
this behavior caused wrong ordering of ajax events. when command button invoked while having cell editor open, ajax event of command button fired before ajax event of cell edit.
we'd swap around them. without editing primefaces source code, achieve explicitly invoking savecell()
function of <p:datatable>
widget when there's cell editor open.
so, given
<h:form> <p:datatable ... widgetvar="wigvardatatable" /> <p:commandbutton ... /> </h:form>
you can achieve desired behavior adding below onclick
<p:commandbutton>
:
onclick="var d=pf('wigvardatatable'), c=d.jq.find('td:has(.ui-cell-editor-input:visible)'); if(c.length)d.savecell(c)"
basically, first finds cell open cell editor , invokes savecell()
on it.
another, less elegant, way explicitly invoking showcelleditor()
function of <p:datatable>
widget without passing expected target cell in try-catch
block exception ignored.
onclick="try{pf('wigvardatatable').showcelleditor()}catch(ignore){}"
it save opened cell first , later throw error no new target cell specified argument, ignored shown above.
noted should works regardless of whether use <p:columns>
or <p:column>
, , regardless of whether use normal model or overcomplicated model ;) trick tested on both primefaces 5.2 version , current primefaces 6.0 version.
Comments
Post a Comment