0

我正在使用 primefaces 3.5 并在编辑数据时观察到一个奇怪的数据表行为。如果我在“年份”字段中输入错误数据并单击勾号,我会收到有关错误的消息。但是,如果我不点击勾号并点击“添加行”按钮,我不会收到消息。仅添加新行。我希望保持编辑模式。如何解决这个问题呢?

<h:panelGrid columns="2" cellpadding="5">

       <h:outputText value="In cell editing" />
       <p:dataTable id="inCellEditing" var="car" value="#{dataTableController.cars}" rowKey="#{car.name}" editable="true">

          <p:ajax event="rowEdit" listener="#{dataTableController.onEdit}" update=":mainForm:growl" />
          <p:ajax event="rowEditCancel" listener="#{dataTableController.onCancel}" update=":mainForm:growl" />

          <p:column headerText="Year">
             <p:cellEditor>
                <f:facet name="output">
                   <h:outputText value="#{car.year}" />
                </f:facet>
                <f:facet name="input">
                   <p:inputText value="#{car.year}">
                      <p:ajax event="blur" update="@this"/>
                   </p:inputText>
                </f:facet>
              </p:cellEditor>
           </p:column>
           <p:column headerText="Name">
              <p:cellEditor>
                <f:facet name="output">
                  <h:outputText value="#{car.name}" />
                </f:facet>
                <f:facet name="input">
                  <h:selectOneMenu value="#{car.name}" >
                    <f:selectItems value="#{dataTableController.carNames}"
                                   var="name"
                                   itemLabel="#{name}"
                                   itemValue="#{name}" />
                   </h:selectOneMenu>
                 </f:facet>
              </p:cellEditor>
           </p:column>
           <p:column headerText="Actions">
              <p:rowEditor />
           </p:column>
       </p:dataTable>
       <p:commandButton action="#{dataTableController.addRow()}" value="Add row" ajax="false"/>         
</h:panelGrid>


    public class DataTableController implements Serializable {

    private List<Car> cars;
    private Car selectedCar;
    private Car[] selectedCars;
    private List<Car> selectedCarsList;
    private SelectItem[] carNamesOptions;

    public DataTableController() {
        cars = new ArrayList<Car>(CarConverter.cars.values());
    }

    public String[] getCarNames() {
        return CarConverter.cars.keySet().toArray(new String[0]);
    }

    public SelectItem[] getCarNamesAsOptions() {
        carNamesOptions = createFilterOptions(CarConverter.cars.keySet().toArray(new String[0]));
        return carNamesOptions;
    }

    private SelectItem[] createFilterOptions(String[] data) {
        SelectItem[] options = new SelectItem[data.length + 1];

        options[0] = new SelectItem("", "Select");
        for(int i = 0; i < data.length; i++) {
            options[i + 1] = new SelectItem(data[i], data[i]);
        }

        return options;
    }

    public void onEdit(RowEditEvent event) {
        MessageUtil.addInfoMessage("car.edit", ((Car) event.getObject()).getName());
    }

    public void onCancel(RowEditEvent event) {
        MessageUtil.addInfoMessage("car.edit.cancelled", ((Car) event.getObject()).getName());
    }

    public Car getSelectedCar() {
        return selectedCar;
    }

    public void setSelectedCar(Car selectedCar) {
        this.selectedCar = selectedCar;
    }

    public Car[] getSelectedCars() {
        return selectedCars;
    }

    public void setSelectedCars(Car[] selectedCars) {
        this.selectedCars = selectedCars;
    }

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }

    public List<Car> getSelectedCarsList() {
        return selectedCarsList;
    }

    public void setSelectedCarsList(List<Car> selectedCarsList) {
        this.selectedCarsList = selectedCarsList;
    }

    public void addRow(){
        cars.add(new Car("",0));
    }
}
4

0 回答 0