Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

903
Visualizações
jquery Datatables checkbox get all checked rows

I have a datatable in which in which I'm trying to get all the checked rows. This table has row grouping and uses a checkbox plugin from gyrocode. I've tried the code listed on the api, but I had no luck. I only get the first record returned, regardless of what is selected. The code I used for the is shown below:

var tbl;
$(document).ready(function (){
          tbl = $('#example').DataTable({
            columnDefs: [{
                targets: 0,
                data: 2,
                'checkboxes': {
                    'selectRow': true
                }
            },
            { "visible": false, "targets": 1 }],
            select: {
                style: 'multi'
            },
            order: [[1, 'asc']],
            iDisplayLength: 10,
            drawCallback: function () {
                var api = this.api();
                var rows = api.rows({ page: 'current' }).nodes();
                var last = null;

                api.column(1, { page: 'current' }).data().each(function (group, i) {
                    if (last !== group) {
                        $(rows).eq(i).before(
                            '<tr class="group"><td colspan="6">' + group + '</td></tr>'
                        );
                        last = group;
                    }
                });
            }
        });
});

function getSelected(){
    alert(tbl.columns().checkboxes.selected().length);
}

I have the code in my jfiddle here. I'm not sure if their is interence between the checkbox and the row grouping? Please let me know where I am going wrong.

Note: The checkbox is based on the plugin by gyrocode The datatables is version 1.10.12

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I'm too late to answer this question. But my answer can help others in the community.

//datatable has to be initialized to a variable
var myTable = $('#calltable').dataTable();

//checkboxes should have a general class to traverse
var rowcollection = myTable.$(".call-checkbox:checked", {"page": "all"});

//Now loop through all the selected checkboxes to perform desired actions
rowcollection.each(function(index,elem){
    //You have access to the current iterating row
    var checkbox_value = $(elem).val();
    //Do something with 'checkbox_value'
});

I hope that helps.

about 4 years ago · Santiago Trujillo Relatório

0

I did a quick check and Eric Guan is correct. I'll just post a code snippet:

function getSelected() {
   var selectedIds = tbl.columns().checkboxes.selected()[0];
   console.log(selectedIds)

   selectedIds.forEach(function(selectedId) {
       alert(selectedId);
   });
}

See: https://jsfiddle.net/nwmmbLso/3/

I just noticed you have duplicatie Student Id's which might also cause unexpected behavior from the library you are using. The code provided above should work if the Student Id's are unique.

about 4 years ago · Santiago Trujillo Relatório

0

Working and tested.

      var id = "";
      var oTable = $(".table").dataTable();

      $(".check_quality:checked", oTable.fnGetNodes()).each(function() {
          if (id != "") {
              id = id + "," + $(this).data('id');
          } else {
              id = $(this).data('id');
          }
      });
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda