Using a function with `dt-api ajax.relaod()`

Using a function with `dt-api ajax.relaod()`

Cedk06Cedk06 Posts: 2Questions: 1Answers: 0

Hi (great job!),

I have some troubles with the method ajax.relaod() ... nothing happens.
I've tested with this javascript:

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "ajax": "arrays.txt",
        "ajax": {
            "async": false,
            "url": "arrays.txt",
            "dataSrc": function(json){return json;} // really necessary ?
        }
    } );
 
    $('#reload').click(function () {
        table.ajax.reload(function(data){
            console.log(data);
        }, false);
    } );
} );

arrays.txt contents :

[
    [
      "Tiger Nixon",
      "System Architect",
      "Edinburgh",
      "5421",
      "2011/04/25",
      "$320,800"
    ],
    [
      "Garrett Winters",
      "Accountant",
      "Tokyo",
      "8422",
      "2011/07/25",
      "$170,750"
    ]
]

and html contents

<button id="reload">reload</button>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>

If I change "your" code (dataTables.js) to

if(callback){
  var api = new _Api( settings );
  callback( api.ajax.json() );
}

instead of

if(callback){
  var api = new _Api( settings );
  api.one( 'draw', function(){
    callback( api.ajax.json() );
  });
}

it works for me...

Actually, it works if you click a second time on the button...

Best regards,
Ced

Answers

  • allanallan Posts: 61,805Questions: 1Answers: 10,119 Site admin

    "dataSrc": function(json){return json;} // really necessary ?

    No - use dataSrc: '' if you are just returning an empty array.

    The problem is being caused by:

    "async": false,

    Or rather it is triggering a bug in DataTables whereby the event handler isn't being added until Ajax the Ajax request - and with async disabled that is after the new data...

    Very certainly a bug that I'll fix - thanks for noting that. However, it is worth pointing out that the latest XHR specs have the async option as deprecated (indeed Chrome's console shows a warning about this now). If possible it is best to avoid using async: false.

    Allan

  • allanallan Posts: 61,805Questions: 1Answers: 10,119 Site admin

    Quick update on this. I've just committed the fix and it is now in the nightly. It will be in 1.10.6 when released.

    Allan

  • Cedk06Cedk06 Posts: 2Questions: 1Answers: 0

    OK. Thanks a lot :-) !
    Btw, I think I've found sometging else : when you fix the height of the table, the sorts' images shows twice (first line and second). I can't explain better (holidays and no computer, only my smartphone)...

  • allanallan Posts: 61,805Questions: 1Answers: 10,119 Site admin

    Hah - hope you enjoy your holiday :-).

    When you get back, if you would be able to provide a link to a test case showing the issue, that would be great.

    Thanks,
    Allan

This discussion has been closed.