【问题标题】:"No data available in table" is displayed though we have data in the html table尽管我们在 html 表中有数据,但仍显示“表中没有可用数据”
【发布时间】:2019-06-19 01:41:26
【问题描述】:

在我的代码中,我在同一页面中创建了两个表,并且我正在使用 dataTables.min.js 和 jquery-1.10.2.js 脚本;

不幸的是,我收到一个错误“表中没有可用数据”,然后它显示了实际数据。

如何去除这个?如果我点击表格标题中的“排序”,我看不到表格中的任何数据。据我了解,没有数据绑定到 ID“datatable-buttons”

    <script src="{{ url_for('static', filename='vendors/datatables.net/js/jquery.dataTables.min.js') }}"></script>

    <div class="x_content">
          <table id="datatable-buttons"   .....

    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script>
    $( document ).ready(function() {
        $.getJSON("http://localhost:5000/api/v1/category", function (data) {
           $.each(data, function(i, item) {
              var tr = $('<tr>');
              $(tr).append("<td>" + item.code + "</td>");
              $(tr).append("<td>" + item.name + "</td>");
              $(tr).append("<td>" + item.description + "</td>");
              $(tr).append('</tr');
              $('#datatable-buttons').append(tr)
          });
        });
      });
     </script>

【问题讨论】:

  • 为什么不使用 DateTable 的 ajax 选项调用 URL ....?
  • @adiga 我在关注重复的 stackoverflow 链接时也看到了同样的问题 - 我在这里分享代码 - gist.github.com/viswesn/0308e1514dd0f7c59b6b291a0032dfc6 如果你能在我错的地方帮助我,那就太好了。我觉得我正在使用更多数量的 Javascript 来加载页面,并且它没有使用实际的 javascript 来定义 Id。
  • @vikscool 添加了 Ajax 功能并且仍然看到相同的结果。我在上面的评论中分享了代码。

标签: javascript html html-table


【解决方案1】:

首先您的表必须包含theadtbody

<table id="datatable-buttons">
   <thead>
     <tr><th>...</tr>
   </thead>
   <tbody></tbody>
</table>

然后您必须在将所有行附加到表后调用 DataTable 函数:

$(document).ready(function () {
    $.getJSON("http://localhost:5000/api/v1/category", function (data) {
        $.each(data, function (i, item) {
            var tr = $('<tr>');
            $(tr).append("<td>" + item.code + "</td>");
            $(tr).append("<td>" + item.name + "</td>");
            $(tr).append("<td>" + item.description + "</td>");
            $(tr).append('</tr');
            $('#datatable-buttons tbody').append(tr)
        });
        $('#datatable-buttons').DataTable()
    });
});

【讨论】:

  • 我在关闭 .......
    后添加了 按照您的指定,但现在我收到错误- $ 未定义
  • 你的脚本必须放在包含 jquery 脚本之后
  • 我仍然面临“无可用数据”:) - 我在这里分享了我的代码 - justpaste.it/61jqw
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
相关资源
最近更新 更多