Eg 2 Editing values in the table
Example#2 with tables – Editing values in the table
Start with the code from Example#1
- Add an “editable” property to Name.java with corresponding getter and setter.
| Name | |
|---|---|
3 4 5 6 7 | |
| canEdit | |
|---|---|
29 30 31 32 33 34 35 | |
- Add a method in the TableData.java to allow the property to be set to true. The method takes a Name object as an argument and it calls the setCanEdit method on the Name object.
editName()
public String editName(Name name){
name.setCanEdit(true);
return null;
}
- Assign an “Edit” link to the end of each row in the table. If clicked, the editName method will be called in the TableData object. This in turn will set set “canEdit” = true. Conditional rendering is introduced here. The “Edit” is only rendered (displayed) when the canEdit property of the name object is set to false.
| xhtml | |
|---|---|
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |

- In the JSF page, if “editable”=true, display the input text box for edit. Otherwise display the normal output text.
| xhtml | |
|---|---|
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |

- Provide a button below the table to save the changes (after the table and before the closing form tag).
xhtml
</h:column>
</h:dataTable>
<h:commandButton value="Save Changes" action="#{tableData.saveAction}" />
</h:form>
A method saveAction must be added to the TableData backing bean. The saveAction method sets the canEdit back to false for all objects in the list.
| saveAction() | |
|---|---|
3 4 5 6 7 8 | |

Click the Edit button

Edit the values and Press Save
