DataTable oTable returns [object Object] and row().data() is undefined

DataTable oTable returns [object Object] and row().data() is undefined

ellamampustiellamampusti Posts: 1Questions: 1Answers: 0

I am using Datatables and trying to get the row data using row().data() or rows().data(). Problem is the row is undefined. Console shows the row is undefined that is why I couldn't get the data.

The data is loaded from a local json file. See my code below. I have tried all the solution in the web and still can't get the data.
When I log the oTable, it shows the _API structure, when I alert it, it shows [object Object]. I have tried using the data("audienceId") already. Am I missing something here?

$(document).ready(function() {
            var oTable = $('#example').DataTable();     


            function reloader(val) { 

                $('#example').DataTable( {
                    "destroy" : true,
                    "ajax": val + ".json",
                    "columns": [
                        { "data": "selected" },
                        { "data": "audienceName" },
                        { "data": "audienceId" },
                        { "data": "uniques" }
                    ],

                                             columnDefs: [ {
                        orderable: false,
                        //className: 'select-checkbox',
                        targets:   0,
                        'checkboxes': {
                           'selectRow': true
                        }
                    } ],
                    select: {
                        style:    'multi'
                    },
                    order: [[ 1, 'asc' ]]
                } ); 
            }; 

            $('#example tbody').on( 'click', 'tr', function () {
                $(this).toggleClass('selected');
                console.log(this); // prints the row element
                var data = oTable.row( this ).data(); //undefined
                alert(data) // undefined
                console.log(data('uniques')); // error
                console.log(oTable); //_Api structure
                alert(oTable); //[object Object]
            } );

            $('select').on('change', function() {
              //alert( this.value );
              reloader(this.value);
            });
} );  

And the JSON

{
  "data": [
    {
      "selected" : "",
      "audienceName":"Automotive",
      "audienceId":"111111",
      "uniques": "888"
    },
    {
      "selected" : "",
      "audienceName":"Automotive_Hybrid",
      "audienceId":"1111",
      "uniques": "888"
    }
]
}

Answers

  • colincolin Posts: 15,158Questions: 1Answers: 2,587

    Hi @ellamampusti ,

    This is working for me here.

    The only real issue for is that your line 37 should be

    console.log(data['uniques']); // error
    

    note data is an object, not a function.

    If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.

    Cheers,

    Colin

This discussion has been closed.