Reputation: 135
I've been having trouble to make this work. I have 2 columns with checkboxes and 1 checkbox in each column that selects all others.
The problem is that when I select the other column the child checkboxes keep selected
I am using the next jquery function to make it work but it is only working partially
Script to select all checkboxes:
$("th input[type='checkbox']").on("change", function () {
debugger;
var cb = $(this), //checkbox that was changed
th = cb.parent(), //get parent th
col = th.index() + 1; //get column index. note nth-child starts at 1, not zero
$("tbody td:nth-child(" + col + ") input").prop("checked", this.checked); //select the inputs and [un]check it
});
script to only select checkboxes in desired column:
$('table').attr('id', 'test');
$('input[type="checkbox"]').on('change', function () {
var checado = $(this).prop('checked');
$(this).closest('tr').find('input[type="checkbox"]').each(function () {
$(this).prop('checked', false);
});
$(this).prop("checked", checado);
});
Html:
<table class="table table-responsive grid-table" id="test">
<thead>
<tr>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Folio</span>
</th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Banco Destino</span>
</th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Tipo de documento</span>
</th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Estado</span>
</th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Conforme</span>
<input type="checkbox" id="checkConforme"> </th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">No conforme</span>
<input type="checkbox" id="checkNoConforme"> </th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Comentarios</span>
</th>
<th class=" hidden" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">lolol</span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">2017100000793</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">058 - Banregio</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">Copia certificada</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">ENTREGADO</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000793|1| " name="check" id="conforme"></td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000793|0| " name="check" id="noConforme"></td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="text" class="lol" style="width:100%" value="" name="comentarioCierre" id="comentarioCierre" pattern="^[A-Za-zÀ-úÑñ 0-9]{1,}$"></td>
<td class="hidden" style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"> </td>
</tr>
<tr>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">2017100000790</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">058 - Banregio</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">Imagen</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">ATENDIDO</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000790|1|" name="check" id="conforme"></td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000790|0|" name="check" id="noConforme"></td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="text" class="lol" style="width:100%" value="" name="comentarioCierre" id="comentarioCierre" pattern="^[A-Za-zÀ-úÑñ 0-9]{1,}$"></td>
<td class="hidden" style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"></td>
</tr>
</tbody>
</table>
Thanks for the help
Upvotes: 3
Views: 748
Reputation: 67505
You could uncheck all the checkboxes first (except the current clicked) then check the desired ones :
//Uncheck all the checkboxes except the current clicked
$("table input:checkbox").not(this).prop("checked", false);
Hope this helps.
$("th input[type='checkbox']").on("change", function() {
debugger;
//Uncheck all the checkboxes except the current clicked
$("table input:checkbox").not(this).prop("checked", false);
var cb = $(this), //checkbox that was changed
th = cb.parent(), //get parent th
col = th.index() + 1; //get column index. note nth-child starts at 1, not zero
$("tbody td:nth-child(" + col + ") input").prop("checked", this.checked); //select the inputs and [un]check it
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-responsive grid-table" id="test">
<thead>
<tr>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Folio</span>
</th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Banco Destino</span>
</th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Tipo de documento</span>
</th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Estado</span>
</th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Conforme</span>
<input type="checkbox" id="checkConforme"> </th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">No conforme</span>
<input type="checkbox" id="checkNoConforme"> </th>
<th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">Comentarios</span>
</th>
<th class=" hidden" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt">
<span class="mvc-grid-header-title">lolol</span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">2017100000793</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">058 - Banregio</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">Copia certificada</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">ENTREGADO</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000793|1| " name="check" id="conforme"></td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000793|0| " name="check" id="noConforme"></td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="text" class="lol" style="width:100%" value="" name="comentarioCierre" id="comentarioCierre" pattern="^[A-Za-zÀ-úÑñ 0-9]{1,}$"></td>
<td class="hidden" style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"> </td>
</tr>
<tr>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">2017100000790</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">058 - Banregio</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">Imagen</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">ATENDIDO</td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000790|1|" name="check" id="conforme"></td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000790|0|" name="check" id="noConforme"></td>
<td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="text" class="lol" style="width:100%" value="" name="comentarioCierre" id="comentarioCierre" pattern="^[A-Za-zÀ-úÑñ 0-9]{1,}$"></td>
<td class="hidden" style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"></td>
</tr>
</tbody>
</table>
Upvotes: 1