Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Cómo agregar una casilla de verificación Seleccionar/Deseleccionar todo a la tabla de datos HTML

Tengo esta tabla de datos:

 <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>

Lo anterior es una tabla de datos que tiene paginación. Luego agregué este script a continuación para lograr mi objetivo.

 <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>

Quiero agregar una casilla de verificación a Seleccionar/Desmarcar todo, y también marcar/desmarcar cada fila.

El script anterior no lo está haciendo.

¿Cómo logro esto?

Gracias

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

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>

Esto es lo que estabas buscando?

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!