2012-06-25 18 views
8

我们的产品所有者希望我们的空表在表中没有数据时只显示表头。我似乎无法阻止dataTable创建一个“空...”消息的行。如何防止jquery dataTable插件在没有数据时添加行和消息

这是我用来初始化dataTable的代码。我知道这里有些事情是错误的。我一直在试验。 :)

$('#InBox').dataTable({ 
    "bFilter": false, 
    "bPaginate": false, 
    "bLengthChange": false, 
    "bInfo": false, 
    "oLanguage": { 
     "sEmptyTable": '', 
     "sInfoEmpty": '' 
    } 
}); 

下面是一些代码,我试图把DataTable的初始化函数,但我不知道如何得到它的工作。

/* Table is empty - create a row with an empty message in it */ 
      var anRows[0] = document.createElement('tr'); 

      if (typeof oSettings.asStripClasses[0] != 'undefined') { 
       anRows[0].className = oSettings.asStripClasses[0]; 
      } 

      var nTd = document.createElement('td'); 
      nTd.setAttribute('valign', "top"); 
      nTd.colSpan = oSettings.aoColumns.length; 
      nTd.className = oSettings.oClasses.sRowEmpty; 
      if (oSettings.fnRecordsTotal() > 0) { 
       if (oSettings.oLanguage.sZeroFilterRecords.indexOf("_MAX_") != -1) 
        oSettings.oLanguage.sZeroFilterRecords = oSettings.oLanguage.sZeroFilterRecords.replace("_MAX_", oSettings.fnRecordsTotal()); 
       nTd.innerHTML = oSettings.oLanguage.sZeroFilterRecords; 
      } else { 
       nTd.innerHTML = oSettings.oLanguage.sZeroRecords; 
      } 

      anRows[iRowCount].appendChild(nTd); 

回答

8

试试这个

$('#InBox').dataTable({ 
    "bFilter": false, 
    "bPaginate": false, 
    "bLengthChange": false, 
    "bInfo": false, 
    "oLanguage": { 
    "sEmptyTable": '', 
    "sInfoEmpty": '' 
    }, 
    "sEmptyTable": "There are no records", 
}); 

否则,你可以试试这个

$('#InBox').dataTable({ 
    "bFilter": false, 
    "bPaginate": false, 
    "bLengthChange": false, 
    "bInfo": false, 
    "oLanguage": { 
    "sEmptyTable": '', 
    "sInfoEmpty": '' 
    } 
}); 
$('.dataTables_empty').html("No record found."); 
+0

我的目标当表为空时不显示任何消息。我宁愿没有创建行。我有一个按钮,将行添加到dataTable。在添加新行之前,我只需添加逻辑即可删除'dataTables_empty'行。我用你的代码。我只是设置空的报价,以便他们没有消息。如果没有提供更优雅的方式,我会将您的答案标记为答案。谢谢你的帮助。 – dcary

+0

第二种方法是为我工作。谢谢 – YeeKhin

3

老帖子,但对于使用搜索引擎的人着想寻找正确的答案这是我如何完成的。

删除或注释掉从数据表源以下行:

anRows[iRowCount].appendChild(nTd); 

在缩小的版本,搜索和删除:

b[i].appendChild(c); 
2

您可以通过使用oLanguange自定义数据表插件:

"oLanguage": { "sZeroRecords": "-Put customized text-", "sEmptyTable": "-Put customized text-" } 

And if you want to remove those, just put these components into null: 
"oLanguage": { "sZeroRecords": '', "sEmptyTable": '' } 

希望它有帮助!

3

如果你想删除数据表插件attacched的TBODY,你可以尝试以下解决方法:

$('.dataTables_empty').parent().parent().remove(); 
1

的最新方法来隐藏这些消息是通过使用language option

$('#loggedMessages').DataTable({ 
    "language": { 
     "emptyTable": ' ', 
     "zeroRecords": ' ' 
    } 
}); 
相关问题