【问题标题】:checking datatables is has data检查数据表是否有数据
【发布时间】:2018-04-03 02:43:07
【问题描述】:

所以我有数据表让说名称表是reportpr,然后我在这些表上有 jquery(使用 ajax 和 json 重新加载) 这是我的代码:

var table = $('#reportpr');
        var target = table.attr('data-table');
        var tblDetail = table.attr('data-detail');
        var oTable = table.on( 'processing.dt', function ( e, settings, processing ){
            if (processing) {
                $(this).find('tbody').addClass('load1 csspinner');
            } else{
                $(this).find('tbody').removeClass('load1 csspinner');
            };
        }).DataTable({
            "bServerSide": true,
            "dom": 'Bfrtip',
            "buttons": [
                'excelHtml5'
            ],
            "scrollX": true,
            "scrollY":        "200px",
            "scrollCollapse": true,
            "iDisplayLength": 10,
            "ajax": {
                "url" : url+"datatable",
                "type": "POST",
                "data" :{
                           title: target
                        },
            }
        });

好吧,它显示我的数据,但我的问题是我如何检查这些表是否有数据或为空(无数据)。 我在这些选项上尝试了很多东西,比如$("reportpr").DataTable().rows().count();$("reportpr").DataTable().page.info(); 但仍然无法正常工作,有人建议吗???我只需要检查我的表是否有数据。

编辑:

我编辑我的问题 我想再做一个偶数触发器,所以不要在数据表上显示 masaage,比如

if(datatable is empty)
{
 button it will enable
}
else
{
 button is will disabled
}

更新:我尝试将我的版本 DataTables 从版本 1.10.5 更新到 1.10.16,并尝试使用 $("reportpr").DataTable().data().any();$("reportpr").DataTable().rows().count();,仍然无法正常工作,当该表有数据时说我没有数据或为空数据。请仍然帮助这些事情。

【问题讨论】:

标签: jquery datatables


【解决方案1】:

您可以对此使用 any() 函数。

var table = $('#example').DataTable();

if ( ! table.data().any() ) {
    alert( 'Empty table' );
}

【讨论】:

  • 不工作,显示错误Uncaught TypeError: $(...).DataTable(...).data(...).any is not a function at <anonymous>:1:34
  • 你为数据表使用的库是什么? “jquery.dataTables.js”还是别的什么?
  • 我使用 jquery.dataTables.min.js,版本 1.10.5
【解决方案2】:

可能最简单的方法是计算行数 - 如果为空,则为 0:

table.rows().count()

【讨论】:

  • 看看我的问题老兄,我试试看。
  • 啊,是的,我看到了你的更新。您是否按照您所说的那样使用$("reportpr").DataTable().rows().count();?如果是,那就错了,应该是$("#reportpr").DataTable().rows().count();
  • Akkh 是的,我忘了添加那个东西 #。时长
【解决方案3】:

您可以使用fnGetData来判断表是否有数据。

$(document).ready(function() {
  var table = $('#maintable').dataTable();

  // Get the table's data.
  var data = table.fnGetData();
  
  // Check the data array length.
  if(data.length==0){
    console.log("Table is empty");
  }else{
    console.log("Table has "+data.length+ " rows.")
  }
});
#tableWrapper{
  width:90%;
  margin: 0 auto;
}
table{
  margin: 0 auto;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/css/dataTables.jqueryui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/jquery.dataTables.min.js"></script>

<div id="tableWrapper">
  <table id="maintable">
    <thead>
      <tr>
        <th>
          test1
        </th>
        <th>
          test2
        </th>
        <th>
          test3
        </th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
</div>

在这个CodePen 中是相同的示例,有 2 个数据行。

【讨论】:

  • no 不工作。获得fnGetData() 是不同的,因为我使用bServerSide
【解决方案4】:

对于服务器端,最好的方法是使用 Datatable drawCallBack 功能如下:

$('#example').dataTable( {
        serverSide: true,            
        ajax: 'https://dog.ceo/api/breeds/list/all',
     columns: [
          {
            data: ""
          },
          {
            data: ""
          }
     ],

    "drawCallback": function( settings ) {

        var rows = this.fnGetData();

        if (rows.length === 0 ) {

        }else{

        }    
    }
} );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-25
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多