How to refresh AJAX/JSON content, from another javascript function?

How to refresh AJAX/JSON content, from another javascript function?

kezman10kezman10 Posts: 12Questions: 3Answers: 0

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

  • allanallan Posts: 63,451Questions: 1Answers: 10,465 Site admin

    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 the table variable. I'd really need a link to a page showing the issue to be able to help.

    Also:

    var table= $('#example').dataTable( {
    

    You could use:

    var table= $('#example').DataTable( {
    

    Note the capital D and then table.ajax.reload() without calling the .api() method.

    Allan

  • kezman10kezman10 Posts: 12Questions: 3Answers: 0

    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?

  • kezman10kezman10 Posts: 12Questions: 3Answers: 0

    I rectify, the problem has been solved, with this

    var table= $('#example').DataTable( {

    I have a problem with the css, I create another thread?

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945

    Take a look at the API docs.

    If you changed to this:

    var table= $('#example').DataTable( {
    

    Then remove the .api() so you have table.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

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945

    I have a problem with the css, I create another thread?

    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

Sign In or Register to comment.