AJAX call from php script to update DataTables

AJAX call from php script to update DataTables

bagofmilkbagofmilk Posts: 4Questions: 1Answers: 0
edited May 2014 in DataTables 1.10

I'm using the example -> http://datatables.net/examples/api/add_row.html

In my situation Im sending an ajax call to a php script that gathers an array of parameters. I am then taking these parameters and trying to dynamically (and visibly) insert a new row in the DataTable

at the line: t.row.add( [ I'm getting an error: "Cannot read property 'add' of undefined"


var asInitVals = new Array(); var oTable = $('.datatable-add-row table').dataTable({ "bJQueryUI": false, "bAutoWidth": false, "sPaginationType": "full_numbers", "sDom": '<"datatable-header"fl><"datatable-scroll"t><"datatable-footer"ip>', "aaSorting":[[0,'desc']], "oLanguage": { "sSearch": "<span>Filter all:</span> _INPUT_", "sLengthMenu": "<span>Show entries:</span> _MENU_", "oPaginate": { "sFirst": "First", "sLast": "Last", "sNext": ">", "sPrevious": "<" } } }); $(".dataTables_wrapper tfoot input").keyup( function () { oTable.fnFilter( this.value, $(".dataTables_wrapper tfoot input").index(this) ); }); (function newjobs() { var t = $('.datatable-add-row table').dataTable(); var inputjob = $.ajax({ type: "POST", url: "create_new_job.php", cache:false, success: function(data) { $('#invisible_button').on( 'click', function () { t.row.add( [ data[0], data[1], data[2], data[3], data[4], data[6], data[7], data[8] ] ).draw(); } ); $('#invisible_button').click(); } }); inputjob.done(function(data) { setTimeout(newjobs, 10000); }); inputjob.fail(function(data) { alert('Job not added....'); }); })();

Answers

  • bagofmilkbagofmilk Posts: 4Questions: 1Answers: 0

    Sorry about the markdown.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Problem is with this line:

    var t = $('.datatable-add-row table').dataTable();

    That is giving you a jQuery instance, not a DataTables API instance. So there is no row.add() method. Use:

    var t = $('.datatable-add-row table').DataTable();
    

    to access the API.

    For more information see the API manual on this topic.

    Allan

This discussion has been closed.