2013-02-27 18 views
1

在Primefaces 2.2中,如果您想要獲取DataTable選擇,則需要提交數據表而無需驗證失敗或轉換器失敗。如何在驗證程序中的primefaces 2.2中獲取DataTable選擇(CheckBox)

象下面這樣:

 <p:dataTable update="outputPanel" id="dataTable" var="car" 
       value="#{tableBean.cars}" selection="#{tableBean.selectedCars}"> 

       <p:column selectionMode="multiple" /> 
       <p:column style="width:200px" id="Model"> 
        <f:facet name="header"> 
         Model 
        </f:facet> 
        <h:outputText value="#{car.model}" /> 
        <h:inputText value="#{car.model}" > 
         <f:attribute name="datatableClientId" value="form:dataTable" /> 
         <f:attribute name="datatable" value="#{dataTableBinding}" /> 
         <f:validator validatorId="dataTableRequiredValidator" /> 
        </h:inputText> 
       </p:column> 

      </p:dataTable> 

當驗證器falied,如何獲得驗證的selectedCars?

回答

1

我查看Primefaces 2.2源代碼,解碼數據表時將使用DataHelper類來decodeSelection。所以我複製代碼編寫一個實用程序類,如下圖所示:

//when the validator in a row, the datatable clientId will be wrong append the row number. so please specify the table clientId. 
    public Object getDataTableSelection(FacesContext context, DataTable table, String dataTableclientId) { 
     String clientId = dataTableclientId != null ? dataTableclientId : table.getClientId(context); 
     Map<String, String> params = context.getExternalContext().getRequestParameterMap(); 

     String selection = params.get(clientId + "_selection"); 
     Object data = null; 
     if (table.isSingleSelectionMode()) { 
      data = decodeSingleSelection(table, selection); 
     } else { 
      data = decodeMultipleSelection(table, selection); 
     } 
     table.setRowIndex(-1); // clean 
     return data; 
    } 

    private Object decodeSingleSelection(DataTable table, String selection) { 
     Object data = null; 
     if (isValueBlank(selection)) { 
      table.setSelection(null); 
      table.setEmptySelected(true); 

     } else { 
      int selectedRowIndex = Integer.parseInt(selection); 
      int first = table.getFirst(); 
      int rows = table.getRows(); 
      int last = rows == 0 ? table.getRowCount() : rows; 

      if (first <= selectedRowIndex && (first + last) > selectedRowIndex) { 
       table.setRowIndex(selectedRowIndex); 
       data = table.getRowData(); 
       table.setSelection(table.getRowData()); 
      } 
     } 
     return data; 
    } 

    private boolean isValueBlank(String value) { 
     if (value == null) 
      return true; 

     return value.trim().equals(""); 
    } 

    private Object decodeMultipleSelection(DataTable table, String selection) { 
     Class<?> clazz = 
      table.getValueExpression("selection").getType(FacesContext.getCurrentInstance().getELContext()); 
     Object data = null; 
     if (isValueBlank(selection)) { 
      data = Array.newInstance(clazz.getComponentType(), 0); 
      table.setSelection(data); 

     } else { 
      if (!table.isCellSelection()) { 
       String[] rowSelectValues = selection.split(","); 
       data = Array.newInstance(clazz.getComponentType(), rowSelectValues.length); 

       for (int i = 0; i < rowSelectValues.length; i++) { 
        table.setRowIndex(Integer.parseInt(rowSelectValues[i])); 

        Array.set(data, i, table.getRowData()); 
       } 
       table.setSelection(data); 
      } 
     } 
     return data; 
    } 

所以,你可以使用getDataTableSelection(current facescontext instance, datatable instance, table clientId)方法來獲取selection.It將返回一個數組對象(不能爲空)。

注意:當驗證器在一行中時,數據表clientId會錯誤地附加行號。所以請指定表clientId。

相關問題