日期:2014-05-16  浏览次数:20351 次

JSF的dataTable使用selectBooleanCheckbox
JSF
<h:form>
    <h:dataTable value="#{myBean.dataList}" var="item">
        <h:column>
            <f:facet name="header">
                <h:outputText value="Select" />
            </f:facet>
            <h:selectBooleanCheckbox value="#{myBean.selectedIds[item.id]}" />
        </h:column>
          ...
    </h:dataTable>
    <h:commandButton value="Get selected items" action="#{myBean.getSelectedItems}" />
</h:form>


后台Bean
public class MyBean ...{
    private Map selectedIds = new HashMap();
    private List selectedDataList;
    public String getSelectedItems() ...{
         selectedDataList = new ArrayList();
        for (MyData dataItem : dataList) ...{
            //如果使用了分页的话这个判断不可少.
            if(selectIds.get(dataItem.getId())==null)...{  
                continue;
             }
            if (selectedIds.get(dataItem.getId()).booleanValue()) ...{
                 selectedDataList.add(dataItem);
                 selectedIds.remove(dataItem.getId()); // 删掉已取的项.
             }
         }
        // 这里加需要处理的代码, 比如批量删除或修改.
        return "selected";
     }
    public Map getSelectedIds() ...{
        return selectedIds;
     }
    public List getSelectedDataList() ...{
        return selectedDataList;
     }
}