Redraw datatables after using ajax to refresh the table content?

前端 未结 9 608
遇见更好的自我
遇见更好的自我 2021-01-30 04:22

I am using Datatables and have a button on the page that refreshes the table using AJAX. To be clear the table isn\'t using an ajax source of data, we are just using ajax to ref

相关标签:
9条回答
  • 2021-01-30 05:11

    This works for me

    var dataTable = $('#HelpdeskOverview').dataTable();
    
    var oSettings = dataTable.fnSettings();
    dataTable.fnClearTable(this);
    for (var i=0; i<json.aaData.length; i++)
    {
       dataTable.oApi._fnAddData(oSettings, json.aaData[i]);
    }
    oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
    dataTable.fnDraw();
    
    0 讨论(0)
  • 2021-01-30 05:13

    I'm not sure why. But

    oTable6.fnDraw();
    

    Works for me. I put it in the next line.

    0 讨论(0)
  • 2021-01-30 05:18

    In the initialization use:

    "fnServerData": function ( sSource, aoData, fnCallback ) {
                        //* Add some extra data to the sender *
                        newData = aoData;
                        newData.push({ "name": "key", "value": $('#value').val() });
    
                        $.getJSON( sSource, newData, function (json) {
                            //* Do whatever additional processing you want on the callback, then tell DataTables *
                            fnCallback(json);
                        } );
                    },
    

    And then just use:

    $("#table_id").dataTable().fnDraw();
    

    The important thing in the fnServerData is:

        newData = aoData;
        newData.push({ "name": "key", "value": $('#value').val() });
    

    if you push directly to aoData, the change is permanent the first time you draw the table and fnDraw don't work the way you want.

    0 讨论(0)
提交回复
热议问题