Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to add rows to a dataTable in jquery gives TypeError

In my Jquery dataTable, row.add is not working and throws an error saying the add function is undefined. The error message is:

Uncaught TypeError: Cannot read property 'add' of undefined

jsfiddle link

html

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
                <th>Column 3</th>
            </tr>
        </thead>

        <tfoot>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
                <th>Column 3</th>
            </tr>
        </tfoot>
    </table>
<button id="addRow">Add new row</button>

javascript

$(document).ready(function() {

    var counter = 1;
    var prntTable = $('#example').dataTable( {  
       "aoColumns" : [ 
           {"bSearchable" : false}, 
           {"bSearchable" : true}, 
           {"bSearchable" : true}
        ],                                          
        "sPaginationType" : "full_numbers"
    } );

    $('#addRow').on( 'click', function () {
        prntTable.row.add( [
            counter +'.1',
            counter +'.2',
            counter +'.3'
        ] ).draw();

        counter++;
    } ); 

    $('#addRow').click();
} );
like image 601
Prasanth A R Avatar asked Nov 29 '22 14:11

Prasanth A R


1 Answers

Instead of:

var prntTable = $("#example").dataTable();

Try:

var prntTable = $("#example").DataTable();

It looks like the old datatables API dataTable() does not support the function you are calling. Use the new API with: DataTable(). Read here for more info: Datatable API

like image 70
Ankur Mahajan Avatar answered Dec 05 '22 15:12

Ankur Mahajan