简体   繁体   中英

Update DataTable using php from AJAX call

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"

Can anyone help?

<script>

    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....'); 
    });
   })();

</script>

Debugging this we look at the fact that it is telling you 'add' is not a property of 'undefined'. This means that 'row' is an undefined member of t. This tells us that t is something [or it would have started by saying 'row' is not a property of undefined].

So this tells me that t is something, just not the datatable object you expect it to be.

Looking at the example link you including on your post, I see ".DataTable()" being used. Objects and properties in JavaScript are case sensitive so ".dataTable()" is not the same as ".DataTable()".

Change the case and see if this fixes your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM