JSF 2.0动态移除组件 [英] JSF 2.0 Dynamically Remove Components
问题描述
作为关于在JSF 2.0中动态添加组件的回答问题(请参阅下面的链接),我喜欢使用dataTable的方法,但是如何删除其中一个添加的组件?
如何动态添加JSF组件根据其他问题,您需要进行以下更改:
-
添加
DataModel< Item>
属性到bean并包装private DataModel< Item> ; model = new ListDataModel< Item>(items);
(不要忘记getter,请注意你也可以在bean构造函数中实例化或postconstruct)
-
在datatable中使用。
< h:dataTable value =#{bean.model}var =item>
-
向bean添加删除方法。
public void delete(){
items.remove(model.getRowData());
}
< h:column>< h :commandButton value =deleteaction =#{bean.delete}/>< / h:column>
另请参见: / h3>
-
@ViewScoped
的优点和缺陷 - 包含JSF 2.0 CRUD表示例
@ViewScoped
的优点和缺陷 - 包含JSF 2.0 CRUD表示例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
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
这篇关于JSF 2.0动态移除组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!