基于seam的datatable全选解决方案
目前的table只支持单行操作和全部行操作,而不支持多选操作,比如每行号前有个复选框,用于批量删除。
通常的做法是改写值对象,或者改写值对象的基类,在其中增加checked属性
------------------------------------------------
public abstract class BaseObject implements Serializable {
private boolean checked;
public void setChecked(boolean value) {
this.checked = value;
}
public boolean isChecked(){
return this.checked;
}
...
}
在页面上使用<h:selectBooleanCheckbox value="#{e.checked}" />
<h:dataTable var="e" value="#{typeallList.typeallModel }">
<f:facet name="header"><h:outputText value="#{text['typeall.typelist'] }"/>
</f:facet>
<h:column>
<f:facet name="header"><h:outputText value="#{text['list.checkall'] }"/></f:facet>
<h:selectBooleanCheckbox value="#{e.checked}" />
</h:column>
提交后循环全部行,取出选择的行
public String removeChecked(){
List tempList = typeallModel.getWrappedObject();
for (int i=0;i<tempList.size();i++){
OaTypeall obj=(OaTypeall)tempList.get(i);
if (obj.isChecked()) {
//删除obj
}
return null;
}
该方法的缺点是需要修改值对象,而添加的属性对值对象而言是没有意义的。这种方式破坏了对象的内聚性,把与该对象不相关的属性强添加到对象中。
尽量做到最大程度的通用性,不用修改值对象的类,用CheckeableList转换list,其中的对象用CheckedWrapped来包装,这样相当于给每行对象增加了checked属性,那样就可以在table的每行增加复选框了,提交后需要通过CheckeableList.getCheckedList(list)获取选种的记录。
具体代码参照如下
package com.bhl.commons.web;
public class CheckedWrapped {
private Object data;
private boolean checked;
public CheckedWrapped(Object data,boolean checek){
this.data=data;
this.checked=checek;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
package com.bhl.commons.web;
import ………
@Name("checkeableList")
public class CheckeableList {
private boolean selected=false;
private String dataModel;
public static String eventType="select_all";
public String getDataModel() {
return dataModel;
}
public void setDataModel(String dataModel) {
this.dataModel = dataModel;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public static List transformList(List list){
if(list!=null&&list.size()>0){
int length=list.size();
for(int i=0;i<length;i++){
Object o=list.get(i);
CheckedWrapped wrap=new CheckedWrapped(o,false);
list.set(i, wrap);
}
}
return list;
}
public static List<Object> getCheckedList(List<CheckedWrapped> list){
List obList=new ArrayList();
if(list!=null&&list.size()>0){
for(CheckedWrapped wrap:list){
if(wrap.isChecked()){
obList.add(wrap.getData());
}
}
}
return obList;
}
public void selectAll(String dataName){
Object o=Contexts.lookupInStatefulContexts(dataName);
if(o!=null&&o instanceof DataModel){
DataModel dm=(DataModel)o;
List<CheckedWrapped> temList=(List<CheckedWrapped>)dm.getWrappedData();
if(temList!=null&&temList.size()>0){
for(CheckedWrapped wrap:temList){
if(selected){
wrap.setChecked(true);
}else{
wrap.setChecked(false);
}
}
}
}
}
//用于页面提交动作后的选择状态清除
@Observer("select_all")
public void clearSelected(){
selected=false;
}
}
黑体加粗部分与全选有关
<a:outputPanel id="searchResults">
<div class="section">
<h:form>
<h:inputText value="#{hotelSearch.name}" style="width: 165px;"/>
<h:inputText value="#{hotelSearch.city}" style="width: 165px;"/>
<h:outputText value="No Hotels Found" />
<a:region>
<rich:spacer height="30" />
<rich:spacer height="30" />
<h:dataTable id="hotels" value="#{hotels}" columnClasses="col" var="wrap" >
<h:column>
<f:facet name="header">
<h:panelGroup>
<h:selectBooleanCheckbox name="checkname" value="#{checkeableList.selected}">
<a:support event="onclick" actionListener="#{checkeableList.selectAll('hotels')}" reRender="searchResults" />
</h:selectBooleanCheckbox>
<h:outputText value="selectAll"/>
</h:panelGroup>
</f:facet>
<h:selectBooleanCheckbox id="checkid" name="checkname" value="#{wrap.checked}"></h:selectBooleanCheckbox>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
dd
</h:column>
<h:column>
<f:facet name="header">Address</f:facet
页面下面的代码显示不出来了,请参加附件。。。。