BUG: using 'ajax' function with GET .DataTable() does not return api instance

BUG: using 'ajax' function with GET .DataTable() does not return api instance

babablacksheepbabablacksheep Posts: 41Questions: 23Answers: 0
edited December 2015 in Free community support

Dear Alan,
Check my jsfiddle @ http://jsfiddle.net/bababalcksheep/8vpg6dp6/

After a load of head banging I came to know that if I use GET with ajax function , .DataTable() method does not return api instance of datatable.
However If I change GET to post then it does return api instance after .DataTable() method.

even the method $.fn.dataTable.tables( { visible: true, api: true } ) does not seem to find it.

Please let me know how to fix it ?

$(document).ready(function () {
  var table =  $('#example').DataTable({
    "columns": [{
      'title': 'userId',
      'data': 'userId'
    }, {
      'title': 'Title',
      'data': 'title'
    }],  
    'ajax': function (data, callback) {
      $.ajax({
        'type': 'GET',
        'url': 'http://jsonplaceholder.typicode.com/posts',
        'cache': false,
        'contentType': 'application/json;',
        'dataType': 'json',
        'data': data,
        'success': function (jSon) {
          callback( {
            data: jSon
          });
        },
        'error': function () {
          callback([]);
        }
      });
    }
  });
});
//
var apis = $.fn.dataTable.tables( { visible: true, api: true } );
console.log(apis);
// table does not exist
// from firebug console "ReferenceError: table is not defined"
var tableInfo = table.page.info();
console.log(tableInfo );

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 62,998Questions: 1Answers: 10,371 Site admin
    Answer ✓

    Try running your var apis... inside the document ready function. It sounds like it might be getting executed before the DataTable is initialised.

    Allan

This discussion has been closed.