对选框的设计
使用控件
ice:selectOneListbox ice:commandButton
页面部分代码
一:可选设备
<ice:outputLabel value="可选设备"/>
<ice:selectOneListbox partialSubmit="true" style="width:100;height:150" size="2" valueChangeListener="#{Services$AddServiceForDevices.selectingDevicesAction}">
<f:selectItems value="#{Services$AddServiceForDevices.selectingDevices}"/>
</ice:selectOneListbox>
二:全部添加 全部删除 按钮
<ice:commandButton action="#{Services$AddServiceForDevices.addAllDevicesAction}" disabled="#{Services$AddServiceForDevices.adddisabled}" partialSubmit="true" value="添加全部"/>
<ice:commandButton action="#{Services$AddServiceForDevices.deleteAllDevicesAction}" disabled="#{Services$AddServiceForDevices.deldisabled}" partialSubmit="true" value="删除全部"/>
三:已选设备
<ice:selectOneListbox partialSubmit="true" style="width:100;height:150" size="2" effect="#{Services$AddServiceForDevices.deviceHighLight}" valueChangeListener="#{Services$AddServiceForDevices.selectedDevicesAction}">
<f:selectItems value="#{Services$AddServiceForDevices.selectedDevices}"/>
</ice:selectOneListbox>
对应bean的内容
准备工作 需要以下变量
//被选择的设备列
private List<SelectItem> selectingDevices = new ArrayList<SelectItem>();
//已选择的设备列
private List<SelectItem> selectedDevices = new ArrayList<SelectItem>();
//添加按钮可用
private boolean adddisabled = true;
//删除按钮可用
private boolean deldisabled = false;
//可选的设备项
private SelectItem selectingDeviceItem;
/**
* 判断已选设备列表中是否已含有设备选项
* @param selectingDeviceItem
* @return true(含有) false(不含有)
*/
public boolean ifDeviceSelectItemHave(SelectItem selectingDeviceItem) {
int num = 0;
while (num < selectedDevices.size()) {
if (selectingDeviceItem.getValue().toString().equals(selectedDevices.get(num).getValue().toString())) {
selectingDevices.remove(selectingDeviceItem);
setIfDeviceHave(true);
return true;
}
num++;
}
setIfDeviceHave(false);
return false;
}
/**
* 单击备选设备选项添加设备列表方法
* @param vce
*/
public void selectingDevicesAction(ValueChangeEvent vce) {
//不选择 没有触发时间不做任何操作
if (vce.getNewValue() != null) {
//得到选择的Id
String devcieId = vce.getNewValue().toString();
for (int i = 0; i < selectingDevices.size(); i++) {
//循环备选设备列框 拿出对应Id的选择项
selectingDeviceItem = selectingDevices.get(i);
if (devcieId.equals(selectingDeviceItem.getValue().toString())) {
//把已经选择的设备放进已选设备列框
//已选设备列表框为空 直接添加到已选列表框 同时把设备选项从备选列表框中删除
if (selectedDevices.isEmpty()) {
selectedDevices.add(selectingDeviceItem);
selectingDevices.remove(selectingDeviceItem);
break;
} else {
//已选设备列表框不为空 判断此次选择的项时都已经包含在已选列表框中 不在 直接添加到已选列表框 同时把设备选项从备选列表框中删除 若含有 不填加到已选列表框 但要把选项从被选框中删除
if (!ifDeviceSelectItemHave(selectingDeviceItem)) {
selectedDevices.add(selectingDeviceItem);
selectingDevices.remove(selectingDeviceItem);
//只要操作完毕 直接退出循环 以免多余的循环 影响性能
break;
}
}
}
}
//每次操作完成后 清空全局变量 selectingDeviceItem
selectingDeviceItem = null;
}
}
/**
* 点击已选设备列表删除设备方法
* @param vce
*/
public void selectedDevicesAction(ValueChangeEvent vce) {
if (vce.getNewValue() != null) {
setIfDeviceHave(false);
String devcieId = vce.getNewValue().toString();
for (int i = 0; i < selectedDevices.size(); i++) {
selectingDeviceItem = selectedDevices.get(i);
if (devcieId.equals(selectingDeviceItem.getValue().toString())) {
//把已经选择的设备从已选设备框删除
selectedDevices.remove(selectingDeviceItem);
selectingDevices.add(selectingDeviceItem);
break;
}
}
selectingDeviceItem = null;
}
}
/**
* 全部添加按钮
*/
public void addAllDevicesAction() {
int i = 0;
while (i < selectingDevices.size()) {
//这个地方有点要注意 while循环中并没有用到i++ 这是因为使用selectingDevices.remove(i)删除被选框中的设备时,被选框的长度会自减。删除完了后selectingDevices.size()也就变成零了。
if (!ifDeviceSelectItemHave(selectingDevices.get(i))) {
selectedDevices.add(selectingDevices.get(i));
selectingDevices.remove(i);
}
}
}
/**
* 全部删除按钮方法
*/
public void deleteAllDevicesAction() {
setIfDeviceHave(false);
int i = 0;
while (i < selectedDevices.size()) {
//同全部添加一样的问题
selectingDevices.add(selectedDevices.get(i));
selectedDevices.remove(i);
}
}
/**
* 获取可选设备列表方法
* 其中设置全部删除 和全部添加 可用性
* @return selectingDevices
*/
public List<SelectItem> getSelectingDevices() {
if (selectingDevices.isEmpty()) {
setAdddisabled(true);
} else {
setAdddisabled(false);
}
if (selectedDevices.isEmpty()) {
setDeldisabled(true);
} else {
setDeldisabled(false);
}
return selectingDevices;
}