Attempting to use fnGetData results in 'fnGetData is not a function' error

Attempting to use fnGetData results in 'fnGetData is not a function' error

doncullendoncullen Posts: 32Questions: 2Answers: 0

Example: http://live.datatables.net/yoyubuvi/1/edit

    var oTable = $('#example').DataTable({
        "columnDefs": [
            {
                "targets": [ 4 ],
                "visible": false,
                "searchable": false
            },
            {
                "targets": [ 5 ],
                "visible": false,
                "searchable": false
            }
        ]
    });

    $('#example').on('click', 'tr', function(event) {
        var memberID = oTable.fnGetData(this)[5]; // getting the value of the last (invisible) cell in the row
        console.log(memberID);
    });

Basically I have an invisible column at the end of the row, and I want to retrieve the value of the cell at the end of that row of whatever row the user clicks. With my current code, it keeps failing saying 'fnGetData is not a function' error.

I must be missing something here. I was able to duplicate the issue in the above example link. Ideas would be appreciated!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,096Questions: 1Answers: 10,390 Site admin
    Answer ✓

    Top FAQ :-).

    To use the legacy API use $().dataTable() ( fnGetData is legacy). To use the modern API use $().DataTable() has you have done. data() is the "modern" equivalent of fnGetData .

    Allan

  • doncullendoncullen Posts: 32Questions: 2Answers: 0
    edited April 2016

    Good to hear from you Allan. Hope all is well with you. Haven't chatted with you in the last five years -- hope my contributions from back then was of help to you!

    Doing:

    $('#example').on('click', 'tr', function(event) {
    var memberID = oTable.row(this).data()[5];
    console.log(memberID);
    

    Results in an 'undefined' error.

    I will keep trying to figure it out, but hopefully you'll save me a few hours of introducing my head to the wall. I suspect the problem is completely obvious, and will become apparent when the coffee I just drank finally kicks in.

    Google is of no help, because 'row' is a very common term. Searching the DataTables forums using .row() also doesn't help, because row is also a very common terms on the forums that usually don't reference .row(). Maybe consider renaming it to fnRow or something like that, make it easier for searches on Google/forums to pick up posts on it and people can help themselves instead of yelling at you for help? :)

    I'm thinking using just 'this' simply will not work, and may require having to find the nearest tr and passing that to rows.

    I'll keep thinking on it, hopefully I don't burn a few more brain cells while I'm at it; I need all of the brain cells I've got to be able to keep up with my kids!

    Edit: I was able to solve the problem.

    I googled 'datatables get nearest tr', which brought up this post:

    https://www.datatables.net/forums/discussion/24660/applying-id-or-data-to-retrieve-td-value

    And the solution provided by you in that post was exactly what I was looking for:

    $(document.body).on('click', 'td.print-control', function () {
        var table = $(this).closest('table').DataTable();
        console.log(table.row($(this).closest('tr')).data()['Client ID']);
    });
    

    So I did it like this:

        $('#example').on('click', 'tr', function(event) {
            var memberID = oTable.row($(this).closest('tr')).data()[5]; 
            console.log(memberID);
        });
    

    Working code: http://live.datatables.net/yoyubuvi/2/edit

    Working like a charm now. Thanks for getting me pointed in the right direction, I was able to figure things out!

  • allanallan Posts: 63,096Questions: 1Answers: 10,390 Site admin

    Hi,

    The first block of code in your post above should work perfectly. Here is an example showing it exactly as is: http://live.datatables.net/viyusopo/1/edit .

    Possibly you have an inner table with an inner row? If so, then yes, you would need to get the DataTable row element.

    Allan

  • doncullendoncullen Posts: 32Questions: 2Answers: 0

    Strange. It works now, but it didn't before. But both solutions work well, and since you pointed out that if I'm using it within another table (which I'm not), then it'd be best to use the second solution. So I'll just stick with the second solution so I don't go nuts trying to figure out why in the name of all that is good it isn't working as expected when and if I absentmindedly put it inside a table.

This discussion has been closed.