4

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

3 Answers 3

18

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

Sign up to request clarification or add additional context in comments.

1 Comment

This solved my problem. The old dataTable() should not even exist.
4
 $('#addRow').on( 'click', function () {
       prntTable.row.add( [
            counter +'.1',
            counter +'.2',
            counter +'.3'
        ] ).draw();

        counter++;
    } ); 

In this code the prntTable is undefined.Either define it as a global variable or redefine it in the click function

Add the

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

after the click function

Fiddle

4 Comments

i got error Uncaught TypeError: Cannot read property 'add' of undefined
the fiddle works fine for me..Have you checked it..??
the undefined error is caused due to the above reason.Do clear your browser cache before checkin it mate.. :)
I think it is important to note that Ankur's answer below helps clarify the answer. Even though Outlooker says to use DataTable, I glossed over the capital D the first time I read it. table.row.add() will not work if you are using dataTable. you must use DataTable. This fixed the problem I was having and I verified it in the fiddle from Outlooker.
2

try to use fnAddData it works for me

table.fnAddData(['1','1','2','3','4']);

1 Comment

table.fnAddData(['1','1','2','3','4']); i am getting error table.fnAddData is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.