1

我用谷歌搜索了很多我的问题,但似乎没有人有同样的问题,或者它没有按照我想要的方式解决

我有一个可编辑的<p:dataTable editable="true" editMode="cell">单元格或行(在 Primefaces 3.5 中)
我有一个验证,当我输入一个字符串> 4 验证错误发生在<p:inputText>仍然可编辑的地方
我有一个<p:commandButton>当我点击这个按钮时我正在添加一个行到此<p:dataTable>,我正在更新表格以显示该行
问题是当我添加一行并且在前一行中存在验证错误(单元格处于可编辑模式<p:inputText>)时,<p:dataTable>将按照我所说的进行更新,并且单元格具有验证错误将更改为<p:outputText>空值
,如果我按前一个单元格(具有错误验证并且为空),将显示错误的输入,如果我按 Tab,它将被提交(<p:inputText>更改为<p:outputText>输入错误)
我如何才能对可编辑

代码示例进行验证:

<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;
    }
}
4

1 回答 1

0

关于您的要求,我几乎没有澄清。(1)您是否一次向数据表添加一行(2)表中的单元格是否始终可编辑,或者只有您要添加的行才可编辑。(3)当您单击按钮时,您是验证所有行还是仅验证要添加到数据表的行?

于 2013-02-22T15:01:25.463 回答