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

107
Visualizações
How to add checkbox Select/Unselect All to HTML data table

I have this datatable:

<div class="card-body">
    <table id="example1" class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>
                    <input type="checkbox" onchange="checkAll(this)" name="chk[]" />
                </th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Department</th>
                <th>Admission Year</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td valign="middle" align="center" style="width: 2%;">
                   
                </td>
                <td></td>
                <td>
   
                </td>
                <td></td>
                <td> </td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

The above is a datatable that have pagination. Then I added this script below to achieve my goal.

<script>
    $(function () {
        $("#example1").DataTable({
            "responsive": true, "lengthChange": false, "autoWidth": false
        }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
        $('#example2').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": true,
            "autoWidth": false,
            "responsive": true,
        });
    });
    function checkAll(ele) {
        var checkboxes = document.getElementsByTagName('input');
        if (ele.checked) {
            for (var i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].type == 'checkbox') {
                    checkboxes[i].checked = true;
                }
            }
        } else {
            for (var i = 0; i < checkboxes.length; i++) {
                console.log(i)
                if (checkboxes[i].type == 'checkbox') {
                    checkboxes[i].checked = false;
                }
            }
        }
    }
</script>

I want to add checkbox to Select/UnselectAll, and to also check/uncheck each row.

The script above is not doing it.

How do I achieve this?

Thank

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

$(function() {
  $("#example1").DataTable({
    "responsive": true,
    "lengthChange": false,
    "autoWidth": false
  }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
  $('#example2').DataTable({
    "paging": true,
    "lengthChange": false,
    "searching": false,
    "ordering": true,
    "info": true,
    "autoWidth": false,
    "responsive": true,
  });
});

function checkAll(ele) {
  var checkboxes = document.getElementsByTagName('input');
  if (ele.checked) {
    for (var i = 0; i < checkboxes.length; i++) {
      if (checkboxes[i].type == 'checkbox') {
        checkboxes[i].checked = true;
      }
    }
  } else {
    for (var i = 0; i < checkboxes.length; i++) {
      console.log(i)
      if (checkboxes[i].type == 'checkbox') {
        checkboxes[i].checked = false;
      }
    }
  }
}

function checkSingle(ele) {
  var checkbox = document.getElementsByTagName('input');
  console.log(ele.checked)
  if (ele.checked) {

    if (checkbox.type == 'checkbox') {
      checkbox.checked = false;
    }

  } else {

    checkbox.checked = true;

  }

}
<div class="card-body">
    <table id="example1" class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>
                    <input type="checkbox" onchange="checkAll(this)" name="chk[]" />
                </th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Department</th>
                <th>Admission Year</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td valign="middle" align="center" style="width: 2%;">
                   <input type="checkbox" onchange="checkSingle(this)" name="chk[]" />
                </td>
                <td>test</td>
                <td>
   test
                </td>
                <td>test</td>
                <td>test </td>
                <td>test</td>
            </tr>
            <tr>
                <td valign="middle" align="center" style="width: 2%;">
                   <input type="checkbox" onchange="checkSingle(this)" name="chk[]" />
                </td>
                <td>test</td>
                <td>
   test
                </td>
                <td>test</td>
                <td>test </td>
                <td>test</td>
            </tr>
        </tbody>
    </table>
</div>

Is this what you were looking for?

almost 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