我写了一些代码-我需要使用inputTextarea添加和删除动态行。它可以工作,但是在删除并添加一些值后-inputTextarea中的值更改不正确。我用
<jsf.version>2.2.8</jsf.version>
<primefaces.version>3.0</primefaces.version>
我的豆子
@ManagedBean
@SessionScoped
public class HelloWorld {
private final static List<String> ENTITIES = Arrays.asList(new String[]{"1", "2", "3", "4", "5"});
@PostConstruct
public void init() {
selectedFields = ENTITIES.stream().filter(s -> !"5".equals(s)).collect(Collectors.toSet());
availableFields = ENTITIES.stream().filter(s -> "5".equals(s)).collect(Collectors.toSet());
values.put("1", "1");
values.put("2", "2");
values.put("3", "3");
values.put("4", "4");
}
private String selectedType = "";
private Set<String> availableFields;
private Set<String> selectedFields;
private Map<String, String> values = new HashMap<String, String>();
public Set<String> getAvailableFields() {
return availableFields;
}
public void setAvailableFields(Set<String> availableFields) {
this.availableFields = availableFields;
}
public Set<String> getSelectedFields() {
return selectedFields;
}
public void setSelectedFields(Set<String> selectedFields) {
this.selectedFields = selectedFields;
}
public Map<String, String> getValues() {
return values;
}
public void setValues(Map<String, String> values) {
this.values = values;
}
public void changeOfferType(String type) {
Optional<String> f = availableFields.stream().filter(s -> s.equals(type)).findFirst();
selectedFields.add(f.get());
availableFields.remove(f.get());
values.put(f.get(), null);
}
public void onDelete() {
String type = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("type");
Optional<String> f = selectedFields.stream().filter(s -> s.equals(type)).findFirst();
selectedFields.remove(f.get());
availableFields.add(f.get());
values.put(f.get(), null);
}
public String getSelectedType() {
return selectedType;
}
public void setSelectedType(String selectedType) {
this.selectedType = selectedType;
}
}
我的xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>PrimeFaces Hello World Example</title>
</h:head>
<h:body>
<h:form>
<p:panel id="main">
<h:panelGrid columns="2">
<h:outputText value="Add value"
style="vertical-align: top; width: 200px;"/>
<p:selectOneMenu
widgetVar="offerType"
style="width: 350px"
value="#{helloWorld.selectedType}">
<f:selectItem itemValue="#{null}" itemLabel=""/>
<f:selectItems value="#{helloWorld.availableFields}" var="def" itemLabel="#{def}" itemValue="#{def}"/>
<p:ajax event="change"
update="main"
listener="#{helloWorld.changeOfferType(helloWorld.selectedType)}"/>/>
</p:selectOneMenu>
</h:panelGrid>
<h:panelGroup id="panelRepeatWrapper">
<c:forEach id="panelRepeat" items="#{helloWorld.selectedFields}" var="field">
<p:remoteCommand name="rc" update="main" action="#{helloWorld.onDelete}"/>
<h:panelGrid columns="3">
<h:outputText value="#{field}"
style="vertical-align: top; width: 200px;"/>
<h:inputTextarea value="#{helloWorld.values[field]}"
style="width: 550px; height: 50px;"/>
<p:commandButton value="x" onclick="#{'rc({type : \''.concat(field).concat('\'})')}">
</p:commandButton>
</h:panelGrid>
</c:forEach>
</h:panelGroup>
</p:panel>
</h:form>
</h:body>
</html>
稍后,我添加带有示例的github repo,它可能在码头上运行
以下图片显示-删除并添加第二个元素后发生的情况-第三个元素的值不是3,而是2 代码有什么问题?!