在我的应用程序中,我需要为数据表动态选择“单个”或“多个”选择模式(我使用的是primefaces 5.0)并且取决于分配的选择。以下是我已经绑定的el表达式。
XHTML:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:head>
<title>Primefaces Data Table</title>
<script type="text/javascript" src="Test.js"/>
</h:head>
<h:body>
<h:form id="form">
<p:growl id="msgs" showDetail="true" />
<p:dataTable id="T" var="car" widgetVar="T_wv" value="#{ECTestScreen.cars}" selectionMode="#{ECTestScreen.selectionMode}" selection="#{ECTestScreen.selectionMode ne 'multiple'? ECTestScreen.selectedCar :ECTestScreen.selectedCars}" rowKey="#{car.id}" paginator="true" rows="5" paginatorAlwaysVisible="true">
<f:facet name="header">
Row Selection on Click
</f:facet>
<p:column headerText="Car Id">
<h:outputText value="#{car.id}" />
</p:column>
<p:column headerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Brand">
<p:inputTextarea id="ta" rows="10" cols="30" style="height: 22px; overflow:auto;" value="#{car.brand}" />
</p:column>
<p:column headerText="Color">
<h:inputText value="#{car.color}" onmouseup="onCellFocus1(event, $(this).parent(), [{name: 'screenletId', value: 'T'}, {name: 'rowIndex', value: '5'}, {name: 'colName', value: 'Color'}]);"/>
</p:column>
<p:ajax event="rowSelect" listener="#{ECTestScreen.onRowSelect}" update=":form:msgs" />
</p:dataTable>
<p:commandButton value="Update Table" actionListener="#{ECTestScreen.onButtonClick}"/>
</h:form>
</h:body>
</html>
Java代码:
@ManagedBean(name = "ECTestScreen")
@ViewScoped
public class ECTestScreen implements Serializable {
private static final long serialVersionUID = -855625904411046273L;
private List<String> products = new ArrayList<>();
private List<Integer> rowList = new ArrayList<>();
private List<Car> cars = new ArrayList<>();
private Car selectedCar ;
private List<Car> selectedCars = new ArrayList<>();
private String selectionMode="multiple";
public List<Integer> getRowList() {
return rowList;
}
public void setRowList(List<Integer> rowList) {
this.rowList = rowList;
}
public List<String> getProducts() {
return products;
}
public void setProducts(List<String> products) {
this.products = products;
}
public List<Car> getCars() {
for(int j=1; j<10;j++){
this.cars.add(new Car(j, (2000+j), "Merc-"+j, "Black-"+j));
}
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public Car getSelectedCar() {
return selectedCar;
}
public void setSelectedCar(Car selectedCar) {
this.selectedCar = selectedCar;
}
public List<Car> getSelectedCars() {
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("form");
return selectedCars;
}
public void setSelectedCars(List<Car> selectedCars) {
this.selectedCars = selectedCars;
}
public String onButtonClick(){
for(int i=1; i<6; i++){
rowList.add(i);
}
for(int j=1; j<4;j++){
products.add("td="+j);
}
for(int j=1; j<10;j++){
cars.add(new Car(j, (2000+j), "Merc-"+j, "Black-"+j));
}
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("form");
return "";
}
public String getSelectionMode(){
return selectionMode;
}
public void setSelectionMode(String selectionMode){
selectionMode="multiple";
}
public void onRowSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage("Car Selected: ", ((Car) event.getObject()).getBrand());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowUnselect(UnselectEvent event) {
FacesMessage msg = new FacesMessage("Car Unselected: ", ((Car) event.getObject()).getBrand());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
现在的问题是,如果我通过el或硬编码将selectionMode分配给单个或多个,并且如果我只指向一个函数(#{ECTestScreen.selectedCar}或#{ECTestScreen.selectedCars),则选择它可以正常工作。但是如果我动态决定选择模式单个或多个,则使用上面的代码取决于如果我通过上面添加选择el,则数据表选择不起作用意味着在选择行时根本没有调用setter。当然,分页也不起作用。它始终保持在第一页。
实际上在单一选择的情况下,期望单个对象在多个选择列表/对象数组中...如何将这两个对象分开...
请帮助..
Primefaces 5.0版
答案 0 :(得分:0)
我真的不知道为什么要做出这种区分,因为多选数据表也应该涵盖单一选择要求。
但如果你坚持,那么我可能会制作2个dataTables - 一个单一和一个多重选择并使它们#34;渲染&#34;基于布尔值。
答案 1 :(得分:0)
selectedCars
必须是数组,而不是List
。展示展示了一个带有List的示例,但它没有那样工作。