Error When Using fnGetPosition/fnGetData - Cannot read property 'nTr' of undefined

Error When Using fnGetPosition/fnGetData - Cannot read property 'nTr' of undefined

jkard883jkard883 Posts: 8Questions: 0Answers: 0
edited February 2012 in General
I am using DOM table, and then attaching the DataTable to it. I then have a JQuery click event tied to a click of each row, which I then use to get cell data via the fnGetPosition and fnGetData.

All is well until I add a row to the table via JQuery 'prepend'. If I then select the newly added row, I'm getting the error:

[code]
Uncaught TypeError: Cannot read property 'nTr' of undefined
L jquery.dataTables.min.js:94
da jquery.dataTables.min.js:27
[/code]

Any thoughts on why this would be happening?

Thanks
Jay

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > All is well until I add a row to the table via JQuery 'prepend'

    That's fine for HTML tables, but DataTables doesn't know that you've added a new row - it doesn't know anything about the new row, hence why you get an error when trying to select it.

    To add a new row you need to use the API to tell DataTables about the new row, and let DataTables add the row - the fnAddData API method is what you want here. There is also plug-in called fnAddTr which might be useful if you already have a TR node ready to go.

    Allan
  • jkard883jkard883 Posts: 8Questions: 0Answers: 0
    edited February 2012
    Thanks Allan,

    So the fnAddtr is perfect as I've already constructed the HTML in Javascript, but I'm not totally understanding the syntax. I unsuccessfully tried the following, the function create_default_player_row() returns a formatted HTML 'tr' row:

    [code]

    $(document).ready(function () {

    var oTable = $('#table-player-list').dataTable();
    oTable.fnAddTr(create_default_player_row());

    });

    [/code]

    When I run, I get "Uncaught TypeError: Object # has no method 'fnAddTr' ".

    Any thoughts?

    Best
    Jay
  • jkard883jkard883 Posts: 8Questions: 0Answers: 0
    edited February 2012
    Forgot this part:

    [code]



    [/code]

    I copied the code from http://datatables.net/plug-ins/api for the 'fnAddtr()' into the file "dataTables.fnAddtr.js". The logic to populate the table is in the "playerconfiguaration.js' file.

    I am using Datatables 1.9.0

    Thanks
    Jay
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hmmm - that looks correct to me. Don't suppose you can link to the page?

    Allan
  • jkard883jkard883 Posts: 8Questions: 0Answers: 0
    edited February 2012
    Unfortunately it is a site still being developed.

    I did try adding the API code to my 'playerconfiguration.js' file, and it seemed to get further. I got an error, like 'Warning - added data does not match, not added new TR" suggesting the row being added didn't match the header (it does match, so not sure why I got an error).

    I'm a little confused why referencing a file didn't work though. I'd rather have any DataTable API stuff in a separate file. I've seen some demos where it's set up like me, but obviously something is still out of order?

    Thanks Allan!

    Jay
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > I got an error, like 'Warning - added data does not match, not added new TR" suggesting the row being added didn't match the header

    Does it match when taking into account hidden columns - if you have any? All columns (including hidden ones) need to be in the added data. Also if you have aoColumns in your init object, make sure you haven't got a trailing comma.

    However, having the DataTables plug-ins in a separate file is a good idea and it certainly should work - I use it a lot myself! I would suspect that it is loading order or something similar, but I'm afraid that without being able to see it I'm not going to be able to be too much help.

    Allan
This discussion has been closed.