无法使用Ajax调用更新数据表主体

时间:2019-06-03 17:13:10

标签: javascript jquery ajax datatables

我有一个DataTable,我正在尝试通过ajax调用来加载数据,但是数据的第一行总是说:

“表中无可用数据” enter image description here

但是在它下面包含了已加载的ajax数据。如何删除No数据行并将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>');
        });
      }
    });
}

3 个答案:

答案 0 :(得分:1)

在Chrome中打开开发者工具,然后在页面中键入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>');
        });

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我可以举一个简单的例子,从您的ajax url对此this.get数组响应并将其设置如下 制作<tbody id='table_tbody_id'></tbody>

尝试这个

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();