问题
i googled my problem a lot but it seems that no one has the same exact problem or it is solved not in what i want
I have an editable <p:dataTable editable="true" editMode="cell"> it doesnot matter if cell or row (in Primefaces 3.5)
I have a validation on one column which is when i enter a string > 4 validation error occur where <p:inputText> remains editable
I have a <p:commandButton> when i click this button i am adding a row to this <p:dataTable> and i am updating the table for displaying the row
The problem is when i add a row and in the previous row there is a validation error (cell is being in editable mode <p:inputText>) the <p:dataTable> will be updated as i said and the cell having the validation error will change to a <p:outputText> with null value
and if i press the previous cell (which has an error validation and is null) the wrong input will be displayed and if i press tab it wil be submitted (<p:inputText> changes to <p:outputText> with the wrong input)
How can i acheive the validation on editable
code sample :
<h:form id="form">
<p:growl id="messages" showDetail="true"/>
<p:contextMenu for="cars" widgetVar="cMenu">
<p:menuitem value="Edit Cell" icon="ui-icon-search" onclick="carsTable.showCellEditor();return false;"/>
<p:menuitem value="Hide Menu" icon="ui-icon-close" onclick="cMenu.hide()"/>
</p:contextMenu>
<p:dataTable id="cars" var="car" value="#{tableBean.carsSmall}" editable="true" editMode="cell" widgetVar="carsTable">
<f:facet name="header">
In-Cell Editing
</f:facet>
<p:ajax event="cellEdit" listener="#{tableBean.onCellEdit}" update=":form:messages" />
<p:column headerText="Model" style="width:25%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.model}" />
</f:facet>
<f:facet name="input">
<p:inputText id="modelInput" value="#{car.model}" style="width:96%">
<f:validateLength minimum="0" maximum="4">
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Year" style="width:25%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.year}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.year}" style="width:96%" label="Year"/>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
<p:commandButton action="#{tableBean.addCar}" update=":cars :messages" process="@this"/>
</h:form>
public class TableBean implements Serializable {
private List<Car> carsSmall;
public TableBean() {
carsSmall = new ArrayList<Car>();
}
public List<Car> getCarsSmall() {
return carsSmall;
}
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
if(newValue != null && !newValue.equals(oldValue)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
public String addCar(){
carsSmall.add(new Car());
return null;
}
}
回答1:
I have few clarification regarding your requirement.(1) Are you adding one row at a time to datatable (2) Are the cells in the table always editable or only the row which you are about to add is only editable. (3) When you click on button are you validating all the rows or only the row you are going to add to data table??
来源:https://stackoverflow.com/questions/14985577/primefaces-editable-datatable-updates-after-validation-failure-in-previous-rows