How to refresh AJAX/JSON content, from another javascript function?
How to refresh AJAX/JSON content, from another javascript function?
Hello, I've been fighting with this datatables thing for a long time and I've already gotten it by asking the question in the forum, I'll show you the code and the problem
I have the following code, to load data using ajax and json and display the table correctly
...
var table= $('#example').dataTable( {
"ajax": {
"url": "getData.php",
"contentType": "application/json",
"type": "GET",
"data": function ( d ) {
return JSON.stringify( d );
}
}
} );
...
This shows me all the results, that is, it works fine
Now to test that my js is able to refresh I have used the following, and it works correctly for me.
...
table.api().ajax.reload();
...
the above works correctly inside setinterval
My problem is that this list has to be refreshed when a function like the following example is executed.
...
function addData(){
//add content to de DB
table.api().ajax.reload();
}
...
I can't find a way to solve it, because in the setinterval it refreshes the data without a problem, if I call it from within the function it doesn't do anything, it doesn't even give me an error in the console
Thanks for all
Replies
Without a link to a page showing the issue it is going to be really hard to debug this. For example, where does
addData
get called from? When executed does it have the scope to access thetable
variable. I'd really need a link to a page showing the issue to be able to help.Also:
You could use:
Note the capital
D
and thentable.ajax.reload()
without calling the.api()
method.Allan
Hello, unfortunately, that has not worked, it returns the following error
table.api is not a function
Unfortunately I can't give you access to any file so you can help me, because it's a bit of a complex project. Actually all the javascript code that I have I have put in the thread.
What can I contribute to help?
Or how can I reload the table after adding data?
I rectify, the problem has been solved, with this
var table= $('#example').DataTable( {
I have a problem with the css, I create another thread?
Take a look at the API docs.
If you changed to this:
Then remove the
.api()
so you havetable.ajax.reload()
. See this example:http://live.datatables.net/kaguhiho/1/edit
If you still need help then please update the test case to show the issue you are having.
Kevin
Yes, create another thread. For styling issues we will need to see an example of the issue to help debug. In the new thread post a link to your page or a running test case showing the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin