Select Datatable Line via Javascript

Asked

Viewed 1,100 times

0

How do I select a line from a Datatable via jQuery or pure Javascript?

  • Dude, just post your example code so it’s hard to help you.

  • @Luiz Felipe, if you can put some things you have tried to do, and what worked or didn’t work with them.

1 answer

1


If you are using the Plug-in datatables for jquery you can do so:

    var tabela = $('table').DataTable();

    var linha = tabela.rows(n).nodes();
   //onde n é o index da linha que deseja selecionar.

Follow an example:

$(document).ready(function () {

    for (var i = 0; i <= 5; i++) {

        $('<tr><td>linha ' + i + '</td><td>linha ' + i + '</td></tr>').appendTo('table');

    }

    var tabela = $('table').DataTable({ "paging": false, "searching": false, "bInfo": false });

    function alterarLinha(n) {

        var linha = tabela.rows(n).nodes();

        $(linha).css('color', '#ff0000');

        $(linha).find('td:first').css({ 'background-color': '#e5e5e5' });
        $(linha).find('td:last').css({ 'background-color': '#d3f9b5' });

    }

    $('input.b').on('click', function () { alterarLinha($(this).prev('input').val()); });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>

<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" />


linha <input type="text" style="width: 30px" value="1" /> <input class="b" type="button" value="Alterar" />

<table style="margin-top: 10px;">

    <thead>
    <tr>
        <th>Coluna 1</th><th>Coluna 2</th>
    </tr>
    </thead>

    <tbody></tbody>

</table>

Browser other questions tagged

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