jfreak53
jfreak53

Reputation: 2349

DataTables change Ajax URL mid-fly

I'm trying to reload my table using the on-the-fly ajax method for changing the URL. I have to filter the results by a search field that is manually made.

According to the API I can use the url() method, but it's not working. I'm using DataTables 1.10.2 downloaded yesterday.

This is my code:

dTable.ajax.url('newurl.php').load();

The error I get from the console is:

TypeError: Cannot read property 'url' of undefined

But if I do a console.log for the dTable object I get the object back. So it's there.

Upvotes: 2

Views: 4423

Answers (2)

alanng
alanng

Reputation: 111

dTable.ajax.url('newurl.php').load();

... is now actually correct in DataTables v1.10, as documented here:

https://datatables.net/reference/api/ajax.url%28%29

and is intended for the same purpose as you have in mind: changing the data source location (such as adding custom search parameters to the URL) on the fly.

I have implemented this in my own app, for on-the-fly custom results filtering, and it's working excellently.

Upvotes: 0

jfreak53
jfreak53

Reputation: 2349

Evidently DataTables requires the api() function to be called before anything. I personally never found this in the documentation anywhere and only on their forum.

So the final command needed to be:

dTable.api().ajax.url('newurl.php').load();

This now allows this function to succeed.

Upvotes: 4

Related Questions