-1

我正在执行 ajax 调用,但数据表在响应后没有更新。我已经尝试了几个功能。请注意,我使用 jQuery 版本 2.2.4

$.ajax({
        type: 'POST',
        url: URL,
        data: parsed,
        dataType: "json",
        beforeSend: function() {}

    }).done(function(data) {
        let Content = '';
        $("#order-table-data").html(Content);
        $("#datatable").trigger("update");
        $("#datatable").ajax.reload(); //Cannot read property 'reload' of undefined
    })

html:

   <table id="datatable" class="table table-striped table-bordered">
     <thead>
     <tr>
                <th>Product</th>
                <th>Stock</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Order</th>
     </tr>
     </thead>

         <tbody id="order-table-data"></tbody>
     </table>              
4

3 回答 3

0

这可能会帮助你

$("#datatable").DataTable().ajax.reload(null, false );

或者

$("#datatable").ajax.reload(null, false);
于 2019-08-21T05:47:13.877 回答
0

你可以试试下面的代码:

$(document).ready(function() {

    $('#results').html('<table id="table-output" class="display" cellspacing="0" width="100%"></table>');

    var table_config = {
        "bDestroy": true,
        "paging": false,
        "language": {
            "zeroRecords": "No results found",
            "processing": "<div align='center'><img src='/static/ajax-loader.gif'></div>",
            "loadingRecords": "<div align='center'><img src='/static/ajax-loader.gif'></div>"
        }
    };      

    $("form").submit(function(e) {

        e.preventDefault();

        var form_data = JSON.stringify($(this).serializeArray());

        $.ajax({
            type: 'POST',
            url: /the_url,
            data: form_data,
            contentType: 'application/json',
            dataType: 'json',
            success: function(response) {

               $('#results').html('<table id="table-output" class="display" cellspacing="0" width="100%"></table>');

                    table_config.columns = response.columns;

                    var table = $('#table-output').DataTable(table_config);
                    table.clear();
                    table.rows.add(response.data);
                    table.draw();

            }
        });
    });
});
于 2019-08-21T06:11:37.447 回答
0

Ajax调用在数据表中有点不同,试试这种方式

$('#datatable').dataTable( {
  "ajax": {
  "url": URL,
  "type": "POST",
  dataType: "json",
  "data": function ( d ) {
     $("#order-table-data").html(d);
   }
 }
} );
于 2019-08-21T05:54:02.200 回答