Getting undefined on click even

Getting undefined on click even

luispozoquerolluispozoquerol Posts: 3Questions: 2Answers: 0
edited February 2021 in Free community support

Hello everybody,

For some reason ONClick even (see function below I get answer: "undefined" Its like row is not being found. Need some help please.

$(document).ready(function() {
      var table = $('#myTable').DataTable({
            "destroy": true,
            "language": {
                "url": "./language/Spanish.json",              
                 },
            "scrollY":        "300px",
            "scrollCollapse": true,
            "paging":         false,
            'info': false,
            "ajax": {
                url: '/api/pedidos',
                dataSrc: '',
                type: 'GET'

              },            
            "columns": [
                {data: 'id_usuario'},
                {data: 'ipn'},
                {data: 'nombre'},
                {data: 'anulado'},
            ],            
        });
      

        $('#myTable tbody').on('click', 'tr', function () {
            var data = table.row( this ).data();
            alert( 'You clicked on '+data[0]+'\'s row' );
        } ); 

  });

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    It looks fine. We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    alert( 'You clicked on '+data[0]+'\'s row' );

    You are using array notation data[0] to access your data. But you defined columns.data which means your data is an object. Try this:

    alert( 'You clicked on '+data.id_usuario+'\'s row' );
    

    Kevin

  • luispozoquerolluispozoquerol Posts: 3Questions: 2Answers: 0

    Thank you! Mr. kthorngren.
    It works fine now

This discussion has been closed.