question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reload datatable when changing dtOptions

See original GitHub issue

In my context, I have a search form. When you submit this form, the client will send an ajax request to retrieve result rows. Once this rows are client-side, I create a datatable and I’m playing around with the rows inside fnServerData.

Basically I have something like this:

self.searchButtonClick = function() {
    myService.getData(myCriteria).then(function(rows) {
        self.dtColumnDefs = [ ... ];
        self.dtOptions = DTOptionsBuilder.newOptions()
            .withOption('fnServerData', function (sSource, aoData, fnCallback) {
                // Do some work with rows and aoData
                fnCallBack(json);
        });
    }
}

The first search works great. When I submit a second time, I retrieve well the result rows from the server, but rewrite dtOptions is not enough to update the datatable content.

I would like to erase and recreate the datatable each time the user submit the form.

I tried to do it using this link but I don’t understand it well.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
l-lincommented, Feb 18, 2015

The API reloadData and rerender are from the DTInstance. For example:

var self = this; // I suppose
var dtInstance;

self.searchButtonClick = function() {
    myService.getData(myCriteria).then(function(rows) {
        self.dtColumnDefs = [ ... ];
        self.dtOptions = DTOptionsBuilder.newOptions()
            .withOption('fnServerData', function (sSource, aoData, fnCallback) {
                // Do some work with rows and aoData
                fnCallBack(json);
        });
    }
}

self.doSomething = function() {
    // ...

    // This will resend the data to the server
    dtInstance.reloadData();
    // If you want to rerender your table, you can use the following.
    // This will redraw completly. This does not necessarly resend the data.
    dtInstance.rerender();
};

DTInstances.getLast().then(function(inst) {
    dtInstance = inst;
});


1reaction
pidupuiscommented, Feb 18, 2015

Thanks, it works!

Final code:

var self = this;
var dtInstance;

self.searchButtonClick = function() {
    myService.getData(myCriteria).then(function(rows) {
        self.dtColumnDefs = [ ... ];
        self.dtOptions = DTOptionsBuilder.newOptions()
            .withOption('fnServerData', function (sSource, aoData, fnCallback) {
                // Do some work with rows and aoData
                fnCallBack(json);
        });
        if(dtInstance) {dtInstance.rerender();}
    }
}

DTInstances.getLast().then(function(inst) {
    dtInstance = inst;
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reload table with new data? — DataTables forums
I should point out that I am using this as test code before I try to implement a table reload function with user...
Read more >
How to refresh jquery data table in Angular 6 after event ...
I'm going to delete country and call rerender function but datatable not refreshed. angular · datatable · Share.
Read more >
Angular 7 | How to Detect Changes And Re-Render DataTables
In this video tutorial, we will lear the process to re-render Datatable after the new product is added to the Database. We will...
Read more >
Angular 12 Refresh Reload Datatable on Button Click ...
Today in this blog post, I am going to show you, Angular 12 Refresh Reload Datatable on Button Click Functionality. Guy's for adding...
Read more >
jQuery Datatables | Reloading data after filter values are ...
Suppose there are two filters on the screen: Date from Date to We need to reload data of Jquery datatable based on changed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found