How to reload the datatable(jquery) data?

后端 未结 10 2481
萌比男神i
萌比男神i 2020-12-05 05:39

How to reload the selected tab? Actually I have problem with reloading part. When I add my data I\'ll successfully saved in datatable but in id field in database it shows pr

相关标签:
10条回答
  • 2020-12-05 06:01

    Simpler solution:

    var dt = $('#table_scroll').dataTable();
    
    $.getJSON(url, null, function (json) {
        dt.fnClearTable();
        dt.fnAddData(json.aaData);
        dt.fnDraw();
    });
    
    0 讨论(0)
  • 2020-12-05 06:06

    None of these solutions worked for me, but I did do something similar to Masood's answer. Here it is for posterity. This assumes you have <table id="mytable"></table> in your page somewhere:

    function generate_support_user_table() {
            $('#mytable').hide();
            $('#mytable').dataTable({
                ...
                "bDestroy": true,
                "fnInitComplete": function () { $('#support_user_table').show(); },
                ...
            });
    }
    

    The important parts are:

    1. bDestroy, which wipes out the current table before loading.
    2. The hide() call and fnInitComplete, which ensures that the table only appears after everything is loaded. Otherwise it resizes and looks weird while loading.

    Just add the function call to $(document).ready() and you should be all set. It will load the table initially, as well as reload later on a button click or whatever.

    0 讨论(0)
  • 2020-12-05 06:07

    To reload the table data from Ajax data source, use the following function:

    dataTable.ajax.reload()
    

    Where dataTable is the variable used to create the table.

    var dataTable = $('#your_table_id').DataTable({
         ajax: "URL"
    });
    

    See ajax.reload() for more information.

    0 讨论(0)
  • 2020-12-05 06:10

    You could use this function:

    function RefreshTable(tableId, urlData)
        {
          //Retrieve the new data with $.getJSON. You could use it ajax too
          $.getJSON(urlData, null, function( json )
          {
            table = $(tableId).dataTable();
            oSettings = table.fnSettings();
    
            table.fnClearTable(this);
    
            for (var i=0; i<json.aaData.length; i++)
            {
              table.oApi._fnAddData(oSettings, json.aaData[i]);
            }
    
            oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
            table.fnDraw();
          });
        }
    

    Dont' forget to call it after your delete function has succeded.

    Source: http://www.meadow.se/wordpress/?p=536

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