3

I'm new to web development and jquery and cannot work out what I'm doing wrong.

$(document).ready(function() {

 /* initialise table */
 var oTable = $('#table').DataTable( {
      "oLanguage": {
       "sEmptyTable": "No data defined!"
       },
      });


/* test */
$('#testbtn').click( function () {
    oTable.row('.selected').remove().draw( false );
    oTable.fnAddData( [ "aa", "aa", "aa" ] ); - throws error
    oTable.fnGetNodes().serialize();          - throws error
} );

});

Within document.ready I am initialising my datatable and then setting up a click handler for a button I have on the page.

Within the buttons click handler, the first line works fine. i.e it deleted the selected row from the table. However the Second line and third line through the following error;

Uncaught TypeError: undefined is not a function

However if I expand out the second two lines to

$('#table').DataTable().fnAddData( [ "aa", "aa", "aa" ] );
$('#table').DataTable().fnGetNodes().serialize();

They work fine, the first line adds a row to my table, and the second row whilst its not doing much doesn't through an error anymore.

Any ideas?

Thanks

2 Answers 2

8

fnAddData and fnGetNodes are no longer functions (which is exactly what your error says)

See https://datatables.net/upgrade/1.10-convert

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

1 Comment

thanks. thats where I've been going wrong... reading old documentation.
0

You use the DataTable API to work with the tables. The constructor doesn't return a reference to the API, just the jQuery object that was used to find the element.

You use the DataTable method without parameters to get a reference to the API:

var oTable = $('#table').DataTable( {
  "oLanguage": {
    "sEmptyTable": "No data defined!"
  },
}).DataTable();

6 Comments

According to datatables.net/manual/api. I can access the API through $( selector ).dataTable().api(); However this is not working.
@user1768233: Try the $( selector ).DataTable() method.
Isn't that what I'm already doing? I've worked it out var oTable = $('#table').dataTable{ "oLanguage": { "sEmptyTable": "No data defined!" }, }) works.. note the lowercase 'd' in .dataTable.
@user1768233: That's odd, that is exactly the opposite of what the documentation says. The dataTable method is supposed to return a jQuery object, not an API object: datatables.net/reference/api
Yeah I agree. Maybe I'm just not understanding something. Does the following example which calls fnGetNodes contradict the document you linked to? sprymedia.co.uk/dataTables-1.4/example_select_single_row.html
|

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.