Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datatables 1.10 add.row() not working within ajax response

Using a datatables 1.10 with dom created table. I am attempting to add a new row of data within an ajax response yet nothing happens. The same code by itself (no ajax) works perfectly fine. Yes, the response.success is returning 'true'.

// this works and adds the new row to the table

$('#test').on('click', function () {
    dt.row.add( [
        'td0',
        'td1',
        'td2',
        'td3'
    ] ).draw();
});

//same code does not work within an ajax response...

$('#dtCreate').on('click', function () {

    $.ajax({
        type: 'post',
        url: '/test/process/p_db_create.php'
    }).done(function (response) {

        //double check response
        console.log(response);

        if (response.success)
        {
            //add the row since this is not serverside
            dt.row.add( [
                'td0',
                'td1',
                'td2',
                'td3'
            ] ).draw();
                ...more code below...
like image 665
user756659 Avatar asked Apr 22 '14 05:04

user756659


People also ask

Why can't I add a row to a table using Ajax?

row.add is not appropriate for server-side processing. You are trying to mix data types in your table. Your AJAX is bring back and array of arrays (rows and cells). Your first add is all adding an array of cells so it works. Your second add is an object with named fields so DataTable does not know what to do with them.

Why can't I use Ajax with DataTables?

This is needed because DataTables expects the data array to be called data - and the normal approach (using dataSrc: 'row_objects') is not available when the ajax function is used. An example of the JSON handled by the above code is:

How do I add a row dynamically to a responsive DataTable?

As you state here, adding a row dynamically to a responsive DataTable works the same way as adding it to a non-responsive DataTable; using row.add (). For some reason, this is not working as shown here. Am I doing something wrong or is it broken?

How do I add a row to my data source?

With server side processing you will need to use an ajax request to add the row to your data source. The row will be displayed when appropriate through normal server side processing events. This discussion has been closed.


1 Answers

the td should be initiated by DataTable() not dataTable(). I ran into this problem and wasted my good few hours.

like image 103
Enziz Avatar answered Sep 23 '22 15:09

Enziz