【问题标题】:Empty h:dataTable in JSF renders empty row with invalid number of columns?JSF中的空h:dataTable呈现列数无效的空行?
【发布时间】:2013-12-31 05:35:43
【问题描述】:

我有以下 JSF 代码,它从一个可能为空的 Bean 中呈现一个表

<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>

问题是上面的代码呈现了以下 HTML:

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

上面是错误的,因为它应该渲染:

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

列数是两而不是一!但更好的是它应该只渲染:

<tbody></tbody>

以便 DataTables js 脚本识别没有行并显示一个漂亮的“空表”。
目前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

当然是因为它看到一行但列数不正确。

现在使用 Primefaces p:dataTable 会产生更多样板 html 代码,因为该表隐藏在多个 div 中,并且当返回一个空结果时,它会产生:

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

DataTables js (http://datatables.net/) 再次发现错误的列数。 有没有办法告诉primefaces在找不到结果时输出什么html代码?

【问题讨论】:

    标签: jsf jakarta-ee jsf-2 primefaces


    【解决方案1】:

    好的,我终于找到了一种解决方法,使用 jQuery 检查 html 并删除有问题的列,这很 hacky,但似乎没有人有任何解决方法,标准 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>
    

    【讨论】:

    • 感谢您的解决方法。我遇到了同样的问题,我应用了你的解决方法,效果很好。
    猜你喜欢
    • 2013-09-16
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    相关资源
    最近更新 更多