Select DataTable row via JavaScript

-1

How do I select a row from a Datatable via jQuery or pure JavaScript?

    
asked by anonymous 10.02.2017 / 21:04

1 answer

0

If you are using the DataTables plug-in for jquery you can do this:

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

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

Here's 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><scriptsrc="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>
    
11.02.2017 / 11:06