table.row( this ).data() return me undefined

table.row( this ).data() return me undefined

amineharbaouiamineharbaoui Posts: 8Questions: 2Answers: 0
edited June 2018 in Free community support

Hello guys,

I want to get some data when the row clicked so I implemented this function like in the example here but when I click on some rows I got undefined

hers's my fucntion to get data :

var table = $('#datatable-buttons').DataTable();
            $('#datatable-buttons tbody').on( 'click', 'tr', function () {
                console.log( table.row( this ).data() );   // return undefined
                console.log( table.row( this ).index());   // return undefined
            } );

I use the code above after this code :

$('#datatable-buttons').dataTable({
                    destroy: true,
                    searching: true,
                    ordering: true,
                    pageLength: 50,
                    data: students,
                    columns: [
                        {"data": "id"},
                        {"data": "accountNumber"},
                        {"data": "name"},
                        {"data": "birthday"},
                        {"data": "uiCheckbox"},
                        {"data": "pointage"},
                    ],
                    columnDefs: [
                        {
                            "targets": [ 0 ],
                            "visible": false,
                            "searchable": false
                        }
                    ],
                    language: {
                        "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/French.json"
                    },
                    dom: '<"dt-buttons"Bf><"clear">lrtp',
                    buttons: [
                        {
                            extend: 'print',
                            text: 'Imprimer ce tableau',
                            title: "Compte rendu " + date,
                            messageTop: "<h2 style='float: right'>" + nbrOfDetectedStudent + "/" + querySnapshot.size + " élèves detecté  </h2>",
                            autoPrint: true
                        },
                    ],
                });

Any idea ?

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @amineharbaoui ,

    Could you give the full error, that would make more sense to us? It would have said "something" was undefined.

    Cheers,

    Colin

  • amineharbaouiamineharbaoui Posts: 8Questions: 2Answers: 0
    edited June 2018

    When I try to display data or the index of clicked row like in my code, I got undefined in the console.

  • kthorngrenkthorngren Posts: 21,352Questions: 26Answers: 4,955

    As Colin asked, please copy and paste the full error into the thread.

    Kevin

  • amineharbaouiamineharbaoui Posts: 8Questions: 2Answers: 0

    But this is my problem, I don't know what's the error, I got undefined with those 2 line of code

                    console.log( table.row( this ).data() );   // return undefined
                    console.log( table.row( this ).index());   // return undefined
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @amineharbaoui ,

    Ah, I see.

    If you look at the example here, it does the same as what you're seeing, but only when the table is empty - i.e. no rows, so it returns undefined. As soon as there is a row (uncomment out "data: students") - it works.

    Is this your problem? Or are you seeing something different?

    Cheers,

    Colin

  • amineharbaouiamineharbaoui Posts: 8Questions: 2Answers: 0

    Hi @colin ,

    Thanks for you reply and your time, so the problem is simply to change the second part of code

    from
    $(...).dataTable()
    to
    $(...).DataTable()

    Any way thanks again.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @amineharbaoui ,

    I think we're talking cross-purposes here - my code is a cut&paste of yours. The difference between DataTable and dataTable is explained here.

    The point in my last message was that I haven't seen your problem. It's behaving as expected.

    If this is still an issue, could you either link to your website or modify my example to demonstrate the problem.

    Cheers,

    Colin

This discussion has been closed.