How to redraw DataTable with new data

前端 未结 7 434
感动是毒
感动是毒 2020-12-13 08:45

I have checked several questions already about this topic here in stackoverflow, but they are all using the old dataTable. I am using DataTable. I populated my DataTable by

相关标签:
7条回答
  • 2020-12-13 08:54
    datatable.rows().iterator('row', function ( context, index ) {
        var data = this.row(index).data();
        var row = $(this.row(index).node());
        data[0] = 'new data';
        datatable.row(row).data(data).draw();
    });
    
    0 讨论(0)
  • 2020-12-13 08:54

    Another alternative is

    dtColumns[index].visible = false/true;
    

    To show or hide any column.

    0 讨论(0)
  • 2020-12-13 08:57

    If you want to refresh the table without adding new data then use this:

    First, create the API variable of your table like this:

    var myTableApi = $('#mytable').DataTable(); // D must be Capital in this.
    

    And then use refresh code wherever you want:

    myTableApi.search(jQuery('input[type="search"]').val()).draw() ;
    

    It will search data table with current search value (even if it's blank) and refresh data,, this work even if Datatable has server-side processing enabled.

    0 讨论(0)
  • 2020-12-13 08:59

    You have to first clear the table and then add new data using row.add() function. At last step adjust also column size so that table renders correctly.

    $('#upload-new-data').on('click', function () {
       datatable.clear().draw();
       datatable.rows.add(NewlyCreatedData); // Add new data
       datatable.columns.adjust().draw(); // Redraw the DataTable
    });
    

    Also if you want to find a mapping between old and new datatable API functions bookmark this

    0 讨论(0)
  • 2020-12-13 08:59

    The following worked really well for me. I needed to redraw the datatable with a different subset of the data based on a parameter.

    table.ajax.url('NewDataUrl?parameter=' + param).load();
    

    If your data is static, then use this:

    table.ajax.url('NewDataUrl').load();
    
    0 讨论(0)
  • 2020-12-13 09:07

    The accepted answer calls the draw function twice. I can't see why that would be needed. In fact, if your new data has the same columns as the old data, you can accomplish this in one line:

    datatable.clear().rows.add(newData).draw();
    
    0 讨论(0)
提交回复
热议问题