我在Primefaces数据表中存在依赖selectonemenus的问题,我没有找到答案。我认为其他人也有同样的问题所以必须有一个解决方案。发生的事情是,当我在数据表中编辑一行然后取消编辑时,我对第一个selectonemenu所做的任何更改都保留在后备对象中,并且不会恢复为其原始值。以下是重现的步骤:
我认为它与支持bean的监听器的ajax调用有关。这就像价值即将发布。这不是理想的行为。有没有解决这个问题?
我在下面提供了代码示例:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
<title>
<ui:insert name="title">Example One</ui:insert>
</title>
</h:head>
<h:body>
<h:form id="form">
<p:growl id="msgs" showDetail="true"/>
<p:messages/>
<p:dataTable id="editTable" var="item" value="#{exampleOne.items}" editable="true" scrollable="false" scrollHeight="350" rowIndexVar="rowIndex">
<p:ajax event="rowEdit" listener="#{exampleOne.onRowEdit}" update=":form:msgs" />
<p:ajax event="rowEditCancel" listener="#{exampleOne.onRowCancel}" update=":form:msgs" />
<p:ajax event="rowEditInit" listener="#{exampleOne.onRowInit}" />
<p:column headerText="Name" style="width: 100px;">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{item.name}"/></f:facet>
<f:facet name="input"><p:inputText id="tagId" value="#{item.name}" style="width:100%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Column One" style="width: 200px;">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{item.columnOneValue}"/></f:facet>
<f:facet name="input">
<p:selectOneMenu id="columnOneId" value="#{item.columnOneValue}" style="width:100%">
<p:ajax event="change" listener="#{exampleOne.onColumnOneChange}" update="columnTwoId" />
<f:selectItem itemLabel="Select Column One" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{exampleOne.columnOneSelectItems}" var="columnOneSelectItem" itemLabel="#{columnOneSelectItem}" itemValue="#{columnOneSelectItem}"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Column Two" style="width: 125px;">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{item.columnTwoValue}"/></f:facet>
<f:facet name="input">
<p:selectOneMenu id="columnTwoId" value="#{item.columnTwoValue}" style="width:100%">
<f:selectItem itemLabel="Select Column Two" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{exampleOne.columnTwoSelectItems}" var="columnTwoSelectItem" itemLabel="#{columnTwoSelectItem}" itemValue="#{columnTwoSelectItem}"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit" style="width: 75px;">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</f:view>
</html>
package com.windhaven_consulting.breezy.controller.ui;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.RowEditEvent;
import com.windhaven_consulting.breezy.component.MountedBoard;
@ManagedBean
@SessionScoped
public class ExampleOne implements Serializable {
private List<Item> items = new ArrayList<Item>();
private List<String> columnOneSelectItems = new ArrayList<String>();
private Map<String, List<String>> columnOneToColumnTwoMap = new HashMap<String, List<String>>();
private List<String> columnTwoSelectItems;
/**
*
*/
private static final long serialVersionUID = 1L;
@PostConstruct
public void postConstruct() {
items.add(new Item("item 1", "Select One", "Select One A"));
items.add(new Item("item 2", "Select Two", "Select Two A"));
items.add(new Item("item 3", "Select Three", "Select Three A"));
columnOneSelectItems.add("Select One");
columnOneSelectItems.add("Select Two");
columnOneSelectItems.add("Select Three");
List<String> columnTwoSelectItems = new ArrayList<String>();
columnTwoSelectItems.add("Select One One");
columnTwoSelectItems.add("Select One Two");
columnTwoSelectItems.add("Select One Three");
columnOneToColumnTwoMap.put("Select One", columnTwoSelectItems);
columnTwoSelectItems = new ArrayList<String>();
columnTwoSelectItems.add("Select Two One");
columnTwoSelectItems.add("Select Two Two");
columnTwoSelectItems.add("Select Two Three");
columnOneToColumnTwoMap.put("Select Two", columnTwoSelectItems);
columnTwoSelectItems = new ArrayList<String>();
columnTwoSelectItems.add("Select Three One");
columnTwoSelectItems.add("Select Three Two");
columnTwoSelectItems.add("Select Three Three");
columnOneToColumnTwoMap.put("Select Three", columnTwoSelectItems);
}
public void onRowInit(RowEditEvent event) {
}
public void onRowEdit(RowEditEvent event) {
}
public void onRowCancel(RowEditEvent event) {
}
public void onColumnOneChange(final AjaxBehaviorEvent event) {
String selectedValue = (String) ((UIOutput) event.getSource()).getValue();
if(StringUtils.isNotEmpty(selectedValue)) {
columnTwoSelectItems = columnOneToColumnTwoMap.get(selectedValue);
}
else {
columnTwoSelectItems = Collections.emptyList();
}
}
public List<Item> getItems() {
return items;
}
public List<String> getColumnOneSelectItems() {
return columnOneSelectItems;
}
public List<String> getColumnTwoSelectItems() {
return columnTwoSelectItems;
}
}