How to get the values of a table?

Asked

Viewed 716 times

3

My doubt is very basic:

I have a table, need to store in a variable all values of the line that was clicked, I am trying it with this method below but the result is "Undefined".

What could be wrong with the code? Or how could I?

 $(document).ready(function () {
    var table = $('#example2').DataTable();

    $('#example2 tbody').on('click', 'tr', function () {
        var data = table.row(this).data();
        alert(data[0]);
    });
});

  $(document).ready(function () {
    $('#example2').DataTable({
        "ajax": 'http://localhost:55959/Cliente/getBydesc?json=""',
        "columns": [
            { "data": "codigo" },
            { "data": "codinterno" },
            { "data": "razao" },
            { "data": "fantasia" },
            { "data": "cnpj" },
            { "data": "ie" },
            { "data": "cep" },
            { "data": "endereco" },
            { "data": "numero" },
            { "data": "complemente" },
            { "data": "bairro" },
            { "data": "cidade" },
            { "data": "uf" },
            { "data": "email" },
            { "data": "fone1" },
            { "data": "fone2" },
            { "data": "fone3" },
            { "data": "datacad" },
            { "data": "proprietario" }
        ],
        "columnDefs": [
             { "targets": [0, 2, 3, 18], "visible": true },
             { "targets": '_all', "visible": false },
             { "targets": [18], "width": "20%" },
             { "targets": [2, 3, 18], 'className': 'mdl-data-table__cell--non-numeric' },           
        ],
        "buttons": [
            'copy'
        ]
    });
});

html

 <table id="example2" class="display" cellspacing="0">
        <thead>
            <tr>
                <th>codigo</th>
                <th>codinterno</th>
                <th>razao</th>
                <th>fantasia</th>
                <th>cnpj</th>
                <th>ie</th>
                <th>cep</th>
                <th>endereco</th>
                <th>numero</th>
                <th>complemente</th>
                <th>bairro</th>
                <th>cidade</th>
                <th>uf</th>
                <th>email</th>
                <th>fone1</th>
                <th>fone2</th>
                <th>fone3</th>
                <th>datacad</th>
                <th>proprietario</th>
            </tr>
        </thead>          
    </table>
  • put table generation code also please

  • Alexandre, I edited the post and put the table creation code

  • already tried to put each un nivel above its code $('#example2 tbody'). on('click', 'tr', Function() { var data = table.Row(this). data(); Alert(date[0]); });

  • I did as you said, but Alert displays "Undefined". see code: $(Document). ready(Function () { table = $('#example2'). Datatable(); $('#example2 tbody'). on('click', 'tr', Function() { var data = table.Row(this). data(); Alert(date[0]); }); });

  • 1

    I was able to solve, instead of using date[0], I used date.code, so I gave it right, thanks for the help.

  • put as answer to the question then, since it managed to solve, so it helps other people. :)

Show 1 more comment

1 answer

-1

You can use $("tr th").next(); and make a loop.

  • 1

    @Diegof the comment came out in English, someone already notified?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.