Jsf 2 使用p:dataTable进行交替单选择和多选择
需要一些帮助来了解我的解决方案是否有效 我有一个具有单行选择的primefaces数据表,我需要添加一个多选择复选框列。。。我的想法是点击一个按钮切换选择模式,将选择模式从单个切换到多个,反之亦然Jsf 2 使用p:dataTable进行交替单选择和多选择,jsf-2,primefaces,datatable,selection,Jsf 2,Primefaces,Datatable,Selection,需要一些帮助来了解我的解决方案是否有效 我有一个具有单行选择的primefaces数据表,我需要添加一个多选择复选框列。。。我的想法是点击一个按钮切换选择模式,将选择模式从单个切换到多个,反之亦然 <pf:dataTable value="${bean.notifications}" var="notif" selection="#{bean.isMultiple() ? consulterCorbeilleBean.selectedNotifs : cons
<pf:dataTable
value="${bean.notifications}"
var="notif"
selection="#{bean.isMultiple() ? consulterCorbeilleBean.selectedNotifs : consulterCorbeilleBean.selectedNotif}"
selectionMode="#{not bean.isMultiple() ? 'single' : ''}"
rowKey="${notification.cle.idNotification}">
<pf:ajax event="rowSelect" disabled="${bean.isMultiple()}"
listener="${bean.function()}" update=":table:notificationTable"
oncomplete="stopPropagationClick()" />
<pf:column selectionMode="multiple" rendered="#{bean.isMultiple()}"/>
</pf:dataTable>
我的选择绑定有问题。我有一个错误:
集合操作的语法非法:
javax.el.propertynotwriteableexception:/index.xhtml@114,50
selection=“#{bean.isMultiple()?bean.selectedNotifs:
bean.selectedNotif}”
有解决办法吗?我使用PrimeFaces3.2
向您致意并感谢您的帮助:)您不能在所选内容上使用动态设置器值
如果我是你,我会重新考虑设计,但如果你真的需要在同一个表上同时使用单个和多个选择选项,你可以使用两个数据表,一次只渲染其中一个,然后通过单击按钮在它们之间切换。如果你真的需要,那么你可以尝试这个工作示例 看法
双选
模型
豆子
import java.io.Serializable;
导入java.util.ArrayList;
导入java.util.List;
导入javax.annotation.PostConstruct;
导入javax.faces.application.FacesMessage;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.ManagedProperty;
导入javax.faces.bean.ViewScoped;
导入javax.faces.context.FacesContext;
导入org.primefaces.context.RequestContext;
导入org.primefaces.event.SelectEvent;
导入org.primefaces.event.UnselectEvent;
@ManagedBean(name=“dtSelectionView”)
@视域
公共类SelectionView实现可序列化{
私人名单1;
私家车选择私家车;
私家车名单;
私有布尔倍数=false;
@ManagedProperty(“#{carService}”)
私家车服务;
@施工后
公共void init(){
cars1=service.createCars(10);/**随机生成的列表。参见PF showcase,datatable示例*/
selectedCars=newarraylist();
}
公共布尔检查选择(汽车){
if(isMultiple())
返回所选汽车。包含(汽车)?真:假;
其他的
返回车。等于(selectedCar)?真:假;
}
公共车辆选择(汽车){
String summary=“Car was”;
if(isMultiple())
如果(所选车辆。包含(车辆)){
所选车辆。删除(车辆);
摘要+=“已从列表中删除。”;
}
否则{
所选汽车。添加(汽车);
摘要+=“已添加到列表中。”;
RequestContext.getCurrentInstance().execute(“PF('multiCarDialog').show();”;
}
其他的
如果(车等于(选定车)){
selectedCar=null;
摘要+=“未选择。”;
}
否则{
selectedCar=car;
摘要+=“已选定。”;
RequestContext.getCurrentInstance().execute(“PF('carDialog').show();”;
}
FacesContext.getCurrentInstance().addMessage(“基本CDT”,新的FacesMessage(摘要));
}
公共无效设置服务(CarService服务){
服务=服务;
}
公共汽车getSelectedCar(){
返回所选车辆;
}
公共列表getSelectedCars(){
返回所选车辆;
}
行上的公共无效选择(选择事件事件){
FacesMessage msg=新的FacesMessage(“选定的汽车”,((汽车)事件.getObject()).getId());
FacesContext.getCurrentInstance().addMessage(null,msg);
}
公共无效onRowUnselect(UnselectEvent事件){
FacesMessage msg=新的FacesMessage(“未选择的汽车”,((汽车)事件.getObject()).getId());
FacesContext.getCurrentInstance().addMessage(null,msg);
}
public void addMessage(){
selectedCar=null;
selectedCars=newarraylist();
字符串摘要=多个?“选中”:“未选中”;
FacesContext.getCurrentInstance().addMessage(“基本CDT”,新的FacesMessage(摘要));
}
公共列表getCars1(){
还车1;
}
公共作废设置cars1(列表cars1){
this.cars1=cars1;
}
公共汽车服务{
回程服务;
}
公共布尔isMultiple(){
返回倍数;
}
public void setMultiple(布尔倍数){
这个。多=多;
}
}
希望您的方向正确。我试图更改selectionMode属性,但selection属性似乎不可编辑。。。
import java.io.Serializable;
public class Car implements Serializable{
private String Id;
private String Brand;
private int Year;
private String Color;
private int Price;
private boolean SoldState;
public Car(String id, String brand, int year, String color, int price,
boolean soldState) {
super();
Id = id;
Brand = brand;
Year = year;
Color = color;
Price = price;
SoldState = soldState;
}
@Override
public boolean equals(Object o){
if(o == null) return false;
if(!(o instanceof Car)) return false;
Car other = (Car) o;
if(! this.Id.equals(other.Id)) return false;
if(! this.Brand.equals(other.Brand)) return false;
if(this.Year != other.Year) return false;
if(! this.Color.equals(other.Color)) return false;
if(this.Price != other.Price) return false;
if(this.SoldState != other.SoldState) return false;
return true;
}
@Override
public int hashCode(){
return (int) Id.hashCode() *
Brand.hashCode() *
Year *
Color.hashCode() *
Price *
(SoldState ? 31 : 32)
;
}
/** getters/setters */
}