问题
As a follow on to an answered question on Adding Components Dynamically in JSF 2.0 (see link below), I like the approach of using a dataTable, but what about Removing one of the added components?
How to dynamically add JSF components
回答1:
Based on the code snippet in the other question you linked, you need to do the following changes:
Add a column with a delete button to the table.
<h:column><h:commandButton value="delete" action="#{bean.delete}" /></h:column>
Add a DataModel<Item> property to the bean and wrap the list of items in it so that you will be able to obtain the table row where the button was clicked.
private DataModel<Item> model = new ListDataModel<Item>(items);
(don't forget the getter, note that you can also instantiate this in bean constructor or postconstruct)
Use this in the datatable instead.
<h:dataTable value="#{bean.model}" var="item">
Add a delete method to the bean.
public void delete() { items.remove(model.getRowData()); }
See also:
- Benefits and pitfalls of @ViewScoped - contains JSF 2.0 CRUD table example
来源:https://stackoverflow.com/questions/4973384/jsf-2-0-dynamically-remove-components