当我尝试使用p:dialog向p:dataTable添加值时,这些值不会添加到表中(甚至不更新bean)。
xhtml文件是
<h:form>
<p:dialog id="dialog" header="Goal" widgetVar="goalDlg"
resizable="false" modal="true" appendTo="@(body)" >
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel value="Minute:" />
<p:inputText value="#{matchBean.minute}" immediate="true" />
<p:outputLabel value="Is self:" />
<p:selectBooleanCheckbox value="#{matchBean.self}" immediate="true" />
</h:panelGrid>
<f:facet name="footer">
<p:commandButton value="OK" action="#{matchBean.addTheGoal}" />
</f:facet>
</p:dialog>
<h1>Add Match Players</h1>
<p:panelGrid columns="2">
<p:dataTable value="#{matchBean.homePlayers}" var="homePlayer" >
<f:facet name="header">
#{matchBean.homeGroupName}
</f:facet>
<p:column>
<f:facet name="header">Play</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Player Name</f:facet>
<p:outputLabel value="#{homePlayer.name}" />
</p:column>
<p:column>
<f:facet name="header">Start</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Yellow</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Red</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Rep</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Min</f:facet>
<p:inputText />
</p:column>
<p:column>
<f:facet name="header">Goals</f:facet>
<p:outputLabel value="#{homePlayer.yield}" />
<p:commandButton value="Add" action="#{matchBean.addGoal(homePlayer, true)}" />
</p:column>
</p:dataTable>
<p:dataTable value="#{matchBean.guestPlayers}" var="guestPlayer">
<f:facet name="header">
#{matchBean.guestGroupName}
</f:facet>
<p:column>
<f:facet name="header">Play</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Player Name</f:facet>
<p:outputLabel value="#{guestPlayer.name}" />
</p:column>
<p:column>
<f:facet name="header">Start</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Yellow</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Red</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Rep</f:facet>
<p:selectBooleanCheckbox />
</p:column>
<p:column>
<f:facet name="header">Min</f:facet>
<p:inputText />
</p:column>
<p:column>
<f:facet name="header">Goals</f:facet>
<p:outputLabel value="#{guestPlayer.yield}" />
<p:commandButton value="Add" action="#{matchBean.addGoal(guestPlayer, false)}" />
</p:column>
</p:dataTable>
</p:panelGrid>
</h:form>
来自托管bean的相关代码:
@ManagedBean
@SessionScoped
public class MatchBean {
...
private int currPlayerId = 0;
private boolean isHomeGroup = false;
public void addGoal(MatchPlayer player, Boolean isHomeGroup) {
currPlayerId = player.getId();
this.isHomeGroup = isHomeGroup;
RequestContext.getCurrentInstance().execute("PF('goalDlg').show();");
}
public void addTheGoal() {
MatchPlayer[] players = null;
if (isHomeGroup) {
players = homePlayers;
} else {
players = guestPlayers;
}
for (MatchPlayer player : players) {
if (player.getId() == currPlayerId) {
MatchGoal goal = new MatchGoal(minute, self);
player.addGoal(goal);
System.out.println("Goal added at minute " + minute + ", self = " + self);
break;
}
}
RequestContext.getCurrentInstance().execute("PF('goalDlg').hide();");
}
...
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
System.out.println("Setting minute to " + minute);
this.minute = minute;
}
和MatchPlayer:
package com.ransh.soccer.dto;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class MatchPlayer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private boolean played;
private String name;
private boolean started;
private boolean yellowCard;
private boolean redCard;
private boolean replaced;
private int minute;
private List<MatchGoal> matchGoals = null;
/**
* C'tor
*/
public MatchPlayer(int id, String name) {
this.id = id;
this.name = name;
played = false;
started = false;
yellowCard = false;
redCard = false;
replaced = false;
minute = 0;
matchGoals = new ArrayList<MatchGoal>();
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the played
*/
public boolean isPlayed() {
return played;
}
/**
* @param played the played to set
*/
public void setPlayed(boolean played) {
this.played = played;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the started
*/
public boolean isStarted() {
return started;
}
/**
* @param started the started to set
*/
public void setStarted(boolean started) {
this.started = started;
}
/**
* @return the yellowCard
*/
public boolean isYellowCard() {
return yellowCard;
}
/**
* @param yellowCard the yellowCard to set
*/
public void setYellowCard(boolean yellowCard) {
this.yellowCard = yellowCard;
}
/**
* @return the redCard
*/
public boolean isRedCard() {
return redCard;
}
/**
* @param redCard the redCard to set
*/
public void setRedCard(boolean redCard) {
this.redCard = redCard;
}
/**
* @return the replaced
*/
public boolean isReplaced() {
return replaced;
}
/**
* @param replaced the replaced to set
*/
public void setReplaced(boolean replaced) {
this.replaced = replaced;
}
/**
* @return the minute
*/
public int getMinute() {
return minute;
}
/**
* @param minute the minute to set
*/
public void setMinute(int minute) {
this.minute = minute;
}
public void addGoal(MatchGoal goal) {
matchGoals.add(goal);
}
public List<MatchGoal> getGoals() {
return matchGoals;
}
public String getYield() {
if (matchGoals.size() == 0) {
return "No Goals";
}
StringBuilder yield = new StringBuilder();
for (MatchGoal goal : matchGoals) {
if (yield.length() > 0) {
yield.append(", ");
}
if (goal.isSelf()) {
yield.append("S(" + goal.getMinute() + ")");
} else {
yield.append(goal.getMinute());
}
}
return yield.toString();
}
}
我对此代码有2个问题:
1.根本没有调用setMinute()
2. homePlayer.yield和guestPlayer.yield永远不会更新
我该如何解决这些问题?
答案 0 :(得分:0)
尝试从对话框中的表单组件中删除immediate="true"
。另外,从对话框中删除appendTo="@(body)
,或在其中添加<h:form>
。由于您要将对话框附加到正文,因此它会从您网页的<h:form>
中删除。如果您在浏览器中检查DOM,可以检查这一点。