Using ajax.reload() for multiple DataTables with different ajax parameters on a single page

Using ajax.reload() for multiple DataTables with different ajax parameters on a single page

rbandirbandi Posts: 3Questions: 1Answers: 0

Hi everyone,

I have a really strange case where I'm not sure if I'm doing something wrong or if this really is the expected API behaviour.

I instantiate two DataTables on the same page using the same ajax url but different parameters (ajax data). Upon refreshing both tables at a later time using ajax.reload() on each of them, both do exactly the same ajax call - namely using the parameters of the second DataTable.

Here is the code i use:

$("#firstTableId").DataTable({
    ajax : {
        "url": "/myurl",
        "data": 
            function( data ) {
                return $.extend( {}, data, {
                    "myParameter": self.getCurrentParameter()
                });
            }
    }
});

The second table is created using the same code, just with a different id (#secondTableId) and gets a different value of self.getCurrentParameter().

Now, when i try to reload the first table via $("#firstTableId").DataTable().ajax.reload();, it does an ajax call using the self.getCurrentParameter() value of the second table.

So it seems to me, only the latest ajax call will be remembered. Even for different DataTable instances. Is this an expected behaviour or am I doing something wrong? Thanks in advance for your help!

This question has an accepted answers - jump to answer

Answers

  • rbandirbandi Posts: 3Questions: 1Answers: 0
    edited January 2018

    After another hour of investigating, I found the error on my side. I overwrote the parameter myself inside self.getCurrentParameter() by using a global instead of a local variable.

    So everything is fine with DataTables and works like expected.

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    Answer ✓

    The first thing you need to check is that self is in fact different in each ajax call and selfhasn't been reassigned at any point. This can not be determined without seeing the rest of your code.

  • rbandirbandi Posts: 3Questions: 1Answers: 0

    Yes, it was something similar. If I made a code sample in the first place, I probably would have figured it out o:) .

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    Glad you got it sorted :smile:

This discussion has been closed.