【问题标题】:Unable to Update Body of DataTable using an Ajax call无法使用 Ajax 调用更新 DataTable 的正文
【发布时间】:2019-06-03 17:13:10
【问题描述】:

我有一个 DataTable,我试图通过 ajax 调用加载数据,但第一行数据总是显示:

“表中没有可用数据”

但在它下面包含了加载的 ajax 数据。如何删除 No data 行并将 ajax 数据加载到该位置?

代码如下:

<div class="box-body">
  <table id="changeTicketsTable" class="table table-bordered table-striped">
    <thead>
      <tr>
        <th>Ticket Number</th>
        <th>Description</th>
        <th>Risk</th>
        <th>Primary CI</th>
        <th>State</th>
        <th>Planned Start Date</th>
        <th>Actual Start Date</th>
        <th>Actual End Date</th>
        <th>Affected Partners</th>
      </tr>
    </thead>
    <tbody>  

    </tbody>
    <tfoot>
      <tr>
        <th>Ticket Number</th>
        <th>Description</th>
        <th>Risk</th>
        <th>Primary CI</th>
        <th>State</th>
        <th>Planned Start Date</th>
        <th>Actual Start Date</th>
        <th>Actual End Date</th>
        <th>Affected Partners</th>
      </tr>
    </tfoot>
  </table>
</div>

DataTable的实现:

<script>
    getChangeTicketInformation();
    $('#changeTicketsTable').DataTable({
      "pageLength": 5,
      'paging'      : true,
      'lengthChange': true,
      'searching'   : false,
      'ordering'    : true,
      'info'        : true,
      'autoWidth'   : false
    });
  })
</script>

用于进行 Ajax 调用的 Javascript:

function getChangeTicketInformation(){
  $.ajax({
     type: "GET",
     url: "../../get_change_ticket_info",
      success: function(data) {
        $.each(data, function (i, item) {
         $('#changeTicketsTable').find('tbody').append(
            '<tr>' +
            '<td>' + item.number + '</td>' +
            '<td>' + item.short_description + '</td>' +
            '<td>' + item.risk + '</td>' +
            '<td>' + item.cmdb_ci_display_value + '</td>' +
            '<td>' + item.state + '</td>' +
            '<td>' + item.start_date + '</td>' +
            '<td>' + item.work_start + '</td>' +
            '<td>' + item.work_end + '</td>' +
            '<td>' + 'FILL IN' + '</td>'
            + '</tr>');
        });
      }
    });
}

【问题讨论】:

    标签: javascript jquery ajax datatables


    【解决方案1】:

    在 Chrome 中打开开发者工具,然后在您的页面中输入 JUST jQuery(也在下方),看看会发生什么。然后检查 html 并查看它是否按预期更新 - 它可能在那里,但可能被隐藏。

    但更好的方法实际上是使用 DataTable 的内置 ajax 功能: https://datatables.net/examples/ajax/simple.html

    $('#changeTicketsTable').find('tbody').append(
                '<tr>' +
                '<td>' + item.number + '</td>' +
                '<td>' + item.short_description + '</td>' +
                '<td>' + item.risk + '</td>' +
                '<td>' + item.cmdb_ci_display_value + '</td>' +
                '<td>' + item.state + '</td>' +
                '<td>' + item.start_date + '</td>' +
                '<td>' + item.work_start + '</td>' +
                '<td>' + item.work_end + '</td>' +
                '<td>' + 'FILL IN' + '</td>'
                + '</tr>');
            });
    

    【讨论】:

    • 我会支持这个答案 if 解决方案,基于原生 DataTables ajax 选项已发布。相反,恐怕 OP 已经接受了这个答案,因为它的代码编写了&lt;tbody&gt; HTML,这也是完全错误的 - DataTables 可以自己做到这一点,此外,表数据不受这种方法的影响,因此,你不能使用浪费了大多数 DataTables API 方法。所以,不得不投反对票。
    【解决方案2】:

    借助 DataTable,您可以使用以下 API 添加新行:

    row.add():向表中添加新行。

    function getChangeTicketInformation() {
            var data = [{number: 1, short_description: '1', risk: 1, cmdb_ci_display_value: 1, state: '1', start_date: '1', work_start: '1', work_end: '1'},
                {number: 2, short_description: '2', risk: 2, cmdb_ci_display_value: 2, state: '2', start_date: '2', work_start: '2', work_end: '2'},
                {number: 3, short_description: '3', risk: 3, cmdb_ci_display_value: 3, state: '3', start_date: '3', work_start: '3', work_end: '3'}];
            //$.ajax({
                //type: "GET",
                //url: "../../get_change_ticket_info",
                //success: function (data) {
                    var dti = $('#changeTicketsTable').DataTable();
                    $.each(data, function (i, item) {
                        dti.row.add([
                            item.number,
                            item.short_description,
                            item.risk,
                            item.cmdb_ci_display_value,
                            item.state,
                            item.start_date,
                            item.work_start,
                            item.work_end,
                            'FILL IN'
                        ]).draw(false);
                    });
                //}
                //});
        }
    
    $('#changeTicketsTable').DataTable({
        "pageLength": 5,
        'paging': true,
        'lengthChange': true,
        'searching': false,
        'ordering': true,
        'info': true,
        'autoWidth': false
    })
    getChangeTicketInformation();
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
    <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    
    
    <div class="box-body">
        <table id="changeTicketsTable" class="table table-bordered table-striped">
            <thead>
            <tr>
                <th>Ticket Number</th>
                <th>Description</th>
                <th>Risk</th>
                <th>Primary CI</th>
                <th>State</th>
                <th>Planned Start Date</th>
                <th>Actual Start Date</th>
                <th>Actual End Date</th>
                <th>Affected Partners</th>
            </tr>
            </thead>
            <tbody>
    
            </tbody>
            <tfoot>
            <tr>
                <th>Ticket Number</th>
                <th>Description</th>
                <th>Risk</th>
                <th>Primary CI</th>
                <th>State</th>
                <th>Planned Start Date</th>
                <th>Actual Start Date</th>
                <th>Actual End Date</th>
                <th>Affected Partners</th>
            </tr>
            </tfoot>
        </table>
    </div>

    【讨论】:

    • 还有更好的解决方案 - 使用 DataTables Embedded ajax 选项来拉取数据。
    • 你是对的。还有一个jQuery插件可以考虑。我仅将此答案添加到证据数据表是具有自己方法的插件....
    【解决方案3】:

    我可以给你一个简单的例子,this.get array response from your ajax url and set them as following 制作&lt;tbody id='table_tbody_id'&gt;&lt;/tbody&gt;and

    试试这个

    function getChangeTicketInformation() {
    $.ajax({
        type: "GET",
        url: "../../get_change_ticket_info",
        success: function (data) {
    
            for (let i in data) {
                let item = data[i];
                let number = item[0];
                let short_description = item[1];
                let risk = item[2];
                let cmdb_ci_display_value = item[3];
                let state = item[4];
                let start_date = item[5];
                let work_start = item[6];
                let work_end = item[7];
    
                let tableRow = "<tr>\n" +
                    "                        <td> " + number + "</td>\n" +
                    "                        <td> " + short_description + "</td>\n" +
                    "                        <td> " + risk + "</td>\n" +
                    "                        <td> " + cmdb_ci_display_value + "</td>\n" +
                    "                        <td> " + state + "</td>\n" +
                    "                        <td> " + start_date + "</td>\n" +
                    "                        <td> " + work_start + "</td>\n" +
                    "                        <td> " + work_end + "</td>\n" +
                    "                        <td> " + 'FILL IN' + "</td>\n" +
                    "                    </tr>";
    
                $('#table_tbody_id').append(tableRow);
            }
    
        }
    });
    }
    

    和调用方法

    getChangeTicketInformation();
    

    【讨论】:

    • 我对这个答案投了反对票,原因有两个。首先,您应该建议调用 jQuery $.ajax() 方法来填充 DataTable,而嵌入的 ajax 选项存在,它会在需要的时间和地点触发 AJAX 调用。其次,手动做DataTables的工作并编写&lt;tbody&gt; html是没有意义的,更何况它不会影响基础表数据源,并使大部分DataTables API无用。
    猜你喜欢
    • 2018-01-28
    • 2019-03-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多