1
votes

I want to add an inputText to my dataTable. this inputText will be used to add a new user (Name, Role, Age) and save it to the database. i can olso update the existing values in the dataTable. For example let's say i have this dataTable.

enter image description here

What i would like to do is to dynamically add an inputText fields to the first row of the Table on button click. I fill in the values to insert and click on a save button.

The idea i come with is to add editable="true" to the dataTable and use <p:cellEditor> and <p:rowEditor/> like this:

<p:dataTable var="item" value="#{userControler.users}" editable="true">

    <p:column>
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{item.name}"/>
           </f:facet>
           <f:facet name="input">
                <p:inputText value="#{item.name}" style="width:100%"/>
           </f:facet>
        </p:cellEditor>
    </p:column>

   <p:column>
       <p:rowEditor  />
    </p:column>
</p:dataTable>

and then add a new epmty user to my list, refresh the dataTable, edit the new user and save it on the event rowEdit. is there another way to do this?

Because the problem i face is due to my composite-id. when i enter new value i can't know if i'm going to update or save the given value. (suppose the composite-id is Name and role. if i want to insert a new value and then i make a mistake and insert an existing value i'm going to have an update instead of an error message).

Hope I'm clear enough. if not i can provide more information.Thank you.

1

1 Answers

1
votes

I did as BalusC suggested.

<p:dataTable var="item" value="#{userControler.users}" editable="true">

    <p:column>
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{item.name}"/>
           </f:facet>
           <f:facet name="input">
                <p:inputText value="#{item.name}" style="width:100%"/>
           </f:facet>
        </p:cellEditor>
           <f:facet name="footer">
              <p:inputText value="#{userControler.newUser.name}" />
           </f:facet>
    </p:column>

   <p:column>
       <p:rowEditor />
       <f:facet name="footer"><p:commandButton action="#{userControler.add}"/></f:facet>
    </p:column>
</p:dataTable>

Works fine.