2013-12-12 21 views
3

我有這使得從一個Bean的表,這可能是空空h:JSF中的dataTable呈現具有無效列數的空行?

<h:dataTable value="#{recController.currentRecList}" var="rec" styleClass="display" id="rec"> 
    <h:column> 
    <f:facet name="header"> 
      <h:outputText value="#{msg['rec.name']}"/> 
    </f:facet> 
    #{rec.name} 
    </h:column> 
    <h:column> 
    <f:facet name="header"> 
     <h:outputText value="#{msg['rec.notes']}"/> 
    </f:facet> 
    #{rec.notes} 
    </h:column> 
</h:dataTable> 

問題下面的JSF代碼是上面的代碼將呈現以下HTML:

<tbody><tr><td></td></tr></tbody> 

以上是錯誤的,因爲它應該render:

<tbody><tr><td></td><td></td></tr></tbody> 

其中列數是兩個而不是一個!但更好的應該只是:

<tbody></tbody> 

因此,DataTables js腳本識別沒有行並顯示一個不錯的「空表」。因爲它看到一排但隨着列的數量不正確

DataTables warning: table id=rec - Requested unknown parameter '1' for row 0. 
For more information about this error, please see http://datatables.net/tn/4 
當然


目前的DataTable js腳本返回一個錯誤。

warning datatables

現在使用Primefaces號碼:

<tr class="ui-widget-content ui-datatable-empty-message"> 
<td class=" " colspan="4">No records found.</td> 
</tr> 

如此反覆數據表:因爲表埋內幾個div的,當返回一個空的結果,它會產生一個DataTable產生甚至更多的樣板HTML代碼js(http://datatables.net/)發現錯誤的列數。 有沒有辦法告訴primefaces當沒有結果被發現時輸出什麼html代碼?

回答

1

好吧我終於找到了一個解決方法,通過使用jQuery來檢查HTML並刪除違規列,這是相當黑客,但似乎沒有人有任何解決方法,標準的JSF數據表和Primefaces數據表都有相同的問題。

<script type="text/javascript" charset="utf-8"> 
       var oTable; 
       jQuery(document).ready(function() { 
        /* JSF has a bug where in the case of an empty list it generates a table with 
        * a single row and a single column no matter how many columns where defined. 
        * This breaks DataTables script so we manually delete this offending row if 
        * it is present */ 
        length = jQuery("#myTable tbody tr").first().children().length; 
        alert(length); 
        if(length===1) 
        { 
         jQuery("#myTable tbody").html(""); 
        } 
        oTable = jQuery('#myTable').dataTable({ 
         "sDom": 'TRlfrCtip', 
         "oTableTools": { 
          "sSwfPath": "#{resource['tabletools/swf/copy_csv_xls_pdf.swf']}" 
         } 
        }); 
        //fnClickAddRow(); 
       }); 
      </script> 
+0

感謝您的解決方法。我遇到了同樣的問題,我使用了您的解決方法,並且工作完美。 –