How to make a each in Datatable and get only the selected lines?

Asked

Viewed 1,461 times

0

Good morning,

I am needing to "SCAN" my Datatable and get only the selected line, could someone please help me?

The action will occur after the click of any button.

I have this code below where I can scan the Datatable and get the obj value of the column I need to play in an array, but I only need the values where I have marked the lines and not all, so it’s taking all the lines:

$('#btnAceitar').on('click', function(event) {
    if (confirm('Tem certeza que deseja dar aceite de saída de NF?')) {
    $('#tabela_listaFuncVeic').DataTable().columns(3).every(function() {
        data = this.data();   

        //FAZ O LOOP PARA ADD NA VARIAVEL DO TIPO ARRAY
        $.each(data, function(idx, obj) {
            console.log(idx);

            vObj += (obj + '||');
        });
    });
} else {
    return false;              
};                 
});

The screen is according to the image below: inserir a descrição da imagem aqui

Thanks in advance.

1 answer

1


Selected lines win class .selected. Then you just go through all the rows of the table and take only the ones that have this class and add the value in the array vObj[]:

$(document).ready(function() {
    $('#tabela_listaFuncVeic').DataTable( {
        columnDefs: [ {
            orderable: false,
            className: 'select-checkbox',
            targets:   0
        } ],
        select: {
            style:    'os',
            selector: 'td:first-child'
        }
    } );
} );


$('#btnAceitar').on('click', function(event) {
    if (confirm('Tem certeza que deseja dar aceite de saída de NF?')) {
    $('#tabela_listaFuncVeic').DataTable().columns(3).every(function() {
       
       vObj = []; // cria a array
       var linhas = this.rows()[0]; // pega todas as linhas
       var $this = this; // atribui a tabela a uma variável

        //FAZ O LOOP PARA ADD NA VARIAVEL DO TIPO ARRAY
        $.each(linhas, function(idx, obj) {
           
           // pega cada linha
           var linha = $this.row(obj).nodes().to$()[0];
            // verifica se a linha tem a classe .selected
            if( $(linha).hasClass("selected") ){
               vObj.push($this.data()[obj]); // coloca o valor da coluna na array
            }

        });
        
        console.log(vObj);
    });
} else {
    return false;              
};                 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/select/1.3.1/js/dataTables.select.min.js"></script>

<table id="tabela_listaFuncVeic" class="display" style="width:100%">
        <thead>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td></td>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td></td>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td></td>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>66</td>
                <td>$86,000</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>
    <button id="btnAceitar">Aceitar</button>

  • Sam worked well only to take the selected line, but with the exchange of this line $.each(data, Function(idx, obj) { for $.each(lines, Function(idx, obj) { how do I now take the value of Columns(3) and assemble my array?

  • I forgot that detail. I changed the answer.

  • Perfect Sam, it worked, thank you very much.

Browser other questions tagged

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