Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reload/refresh javascript array in DataTables

I'm using DataTables to display data from a search function. I'm sure if this was intended method for passing data into DataTables. I'm initializing the DataTable with a blank javascript array. Then I want to populate Datatables in a different function.

I've tried $().DataTable().ajax.reload() and $().DataTable().fnAddData(dataSet) but both didn't work.

https://jsfiddle.net/owxz7e22/3/

<table id="test" class="table table-striped table-bordered table-hover dt-responsive">
</table>

<a href="#" onclick="LoadData();">Test</a>


$(document).ready(function ()
    {

        $('#test').DataTable({
            data: dataSet,
            columns: [
                { title: "Name" },
                { title: "Position" },
                { title: "Office" },
                { title: "Extn." },
                { title: "Start date" },
                { title: "Salary" }
            ]
        });
    });

    var dataSet = [];

    function LoadData()
    {
        dataSet = [
                ["Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800"],
                ["Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750"],
                ["Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000"],
        ];

        var tbl = $('#test').DataTable();

        tbl.fnClearTable();
        tbl.fnDraw();
        tbl.fnAddData(dataSet);


        //$('#test').DataTable().ajax.reload();

    }
like image 777
Kathy Judd Avatar asked Mar 14 '17 18:03

Kathy Judd


People also ask

How do you refresh a DataTable without losing your current page or ordering?

Thankfully one of the DataTables forum guys came up with a handy bit of extension code which introduces a new function called fnStandingRedraw() which extends the datatables API and allows you to fresh the datatable without losing your current view.

What is Deferrender in DataTables?

This option allows DataTables to create the nodes (rows and cells in the table body) only when they are needed for a draw.

What is stateSave in DataTables?

When DataTables' state saving option is enabled ( stateSave ) KeyTable will automatically store the last cell focused in a table and then restore that state when the page is reloaded.

How much data can DataTables handle?

The maximum number of rows that a DataTable can store is 16,777,216.


1 Answers

You have to store your datatable in a variable and adding rows: Look here: https://jsfiddle.net/63235xk2/

$(document).ready(function ()
        {
            var dataSet = [];

            var datatable = $('#test').DataTable({
                data: dataSet,
                columns: [
                    { title: "Name" },
                    { title: "Position" },
                    { title: "Office" },
                    { title: "Extn." },
                    { title: "Start date" },
                    { title: "Salary" }
                ]
            });


      $('.asd').on('click',function(){
                dataSet = [
                    ["Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800"],
                    ["Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750"],
                    ["Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000"],
          ];

        alert(dataSet.length);

          datatable.clear();
          datatable.rows.add(dataSet);
          datatable.draw();
      });

        });
like image 182
enno.void Avatar answered Sep 29 '22 07:09

enno.void