参考了http://blog.csdn.net/Samland/archive/2006/09/17/1232585.aspx。决定通过修改值对象的方法在
JSF DataTable中实现多选的功能。
首先,给数据模型对象bean中添加一个boolean的属性selected.
2、在JSF 页面的dataTable中加入一列 <h:selectBooleanCheckbox/>,其value的值和selected属性相对应,这样就可以通过点击checkbox来确定对象是否被选择。
3.在managed beans中通过如下的方式获得被选择的对象.
JSF DataTable中实现多选的功能。
首先,给数据模型对象bean中添加一个boolean的属性selected.
- public class Item {
- private Integer id;
- ...
- private boolean selected;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- ....
- public boolean isSelected() {
- return selected;
- }
- public void setSelected(boolean selected) {
- this.selected = selected;
- }
- }
- <h:form>
- <h:dataTable id="itemsTable"
- value="#{ListBean.List}" var="row" >
- <h:column>
- <f:facet name="header">
- <h:outputText value="Select" />
- </f:facet>
- <h:selectBooleanCheckbox value="#{row.selected}" />
- </h:column>
- <h:column>
- <f:facet name="header">
- <h:outputText value="ID" />
- </f:facet>
- <h:outputText value="#{row.id}"></h:outputText>
- </h:column>
- ...
- </h:dataTable>
- <h:commandButton value="Get Selected"
- actionListener="#{ListBean.getSelectedItems}" />
- </h:form>
- 1. public class ListBean {
- 2.
- 3. private List<Item> list;
- 4.
- 5. public getList()
- 6. {
- 7. //init list
- 8. return list;
- 9. }
- 10.
- 11.
- 12. public void getSelectedItems(ActionEvent event) {
- 13. for (Item item : list) {
- 14. if (item.isSelected()) {
- 15. // do sth with selected items
- 16. }
- 17. }
- 18. }
- 19. }