jsf - PrimeFace dataTable with variable columns and specific editable cells -


i need create table headers list brought model. table contents stored in model , p:datatable loop on data show content based on column name.

the issue need make specific cells editable. outputting data there no problem since use model method takes both entity , column name , return correct info entity based on column name. issue inputs of editable cells don't know how set in entity.

<p:datatable id="processtable"  var="entity" value="#{home.process.headerentities}" tablestyle="width:auto" draggablecolumns="true" editable="true" editmode="cell">      <p:columns value="#{home.process.columns}" var="columnhead" >          <f:facet name="header">             <h:outputtext value="#{columnhead}"/>         </f:facet>          <p:celleditor>             <f:facet name="output">                 <h:outputtext value="#{home.process.getdata(entity, columnhead)}" />             </f:facet>             <f:facet name="input">                 <p:inputtext value="#{home.process.getdata(entity, columnhead)}" rendered="#{home.process.iseditable(columnhead)}"  style="width:100%" />             </f:facet>         </p:celleditor>       </p:columns>  </p:datatable> 

after chanage based on best answer

<p:datatable id="processtable"  var="entity" value="#{home.process.headerentities}" tablestyle="width:auto" draggablecolumns="true" editable="true" editmode="cell">                  <p:columns value="#{home.process.columns}" var="columnhead" >                      <f:facet name="header">                         <h:outputtext value="#{columnhead}"/>                     </f:facet>                      <p:celleditor>                         <f:facet name="output">                             <h:outputtext value="#{entity[home.process.columnpropertymap[columnhead]]}" />                         </f:facet>                         <f:facet name="input">                             <p:inputtext value="#{entity[home.process.columnpropertymap[columnhead]]}" rendered="#{home.process.iseditable(columnhead)}"  style="width:100%" />                         </f:facet>                     </p:celleditor>                   </p:columns>              </p:datatable> 

the input component's value must bound writable value expression. you've there direct getter method invocation , read-only. indeed not going work. need specify property name of #{entity}. can use brace notation specify property name variable #{entity[propertyname]}.

so, basically:

<p:datatable value="#{bean.entities}" var="entity" editable="true" editmode="cell">     <p:columns value="#{bean.propertynames}" var="propertynames">         <p:celleditor>             <f:facet name="output">                 #{entity[propertyname]}             </f:facet>             <f:facet name="input">                 <p:inputtext value="#{entity[propertyname]}" />             </f:facet>         </p:celleditor>     </p:columns> </p:datatable> 

as column header, rather refactor out map<string, string> key propertyname , value header.

        <f:facet name="header">             #{bean.columnheaders[propertyname]}         </f:facet name="header"> 

or better yet, use normal i18n resource bundle propertyname represents part of bundle key.

        <f:facet name="header">             #{bundle['table.column.header.' += propertyname]}         </f:facet name="header"> 

as editable check, rather wrap propertyname , editable in bean (and perhaps columnheader if don't want use i18n bundle), e.g. field , use below:

    <p:columns value="#{bean.fields}" var="field">         <p:celleditor>             <f:facet name="output">                 #{entity[field.propertyname]}             </f:facet>             <f:facet name="input">                 <p:inputtext value="#{entity[field.propertyname]}" rendered="#{entity[field.editable]}" />             </f:facet>         </p:celleditor>     </p:columns> 

all in all, boils down preparing , providing right model view expects. way getdata() thing isn't necessary.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -