Surabhi
Surabhi

Reputation: 3

Select all checkbox in the same column

I am trying to implement something similar to Checking checkbox in column

I have two select all checkboxes in a table and selecting one would select all the checkboxes of the same column.

It an ASP.NET GridView. Plunker

function CheckHeaderCheckboxAll(obj, gridname) {    
    var objId = obj.id;
    var table= $(obj).closest('table');
    $('td input:checkbox',table).prop('checked',this.checked);
}

Can someone please help me with this?

Upvotes: 0

Views: 3616

Answers (3)

Shubh
Shubh

Reputation: 6741

Well what @epascarello said is probably better.

I just have a minor suggestion and that would be finding the checkboxes from the same table itself otherwise it would end up selecting checkboxes present in all the tables of the page:

function SelectAll(obj) {

  // find the index of column
  var table = $(obj).closest('table'); // find the current table
  var th_s = table.find('th'); // find/get all the <th> of current table
  var current_th = $(obj).closest('th'); // get current <th>

  // the value of n is "1-indexed", meaning that the counting starts at 1
  var columnIndex = th_s.index(current_th) + 1; // find index of current <th> within list of <th>'s

  console.log('The Column is = ' + columnIndex); // print the index for testing

  // select all checkboxes from the same column index of the same table
  table.find('td:nth-child(' + (columnIndex) + ') input').prop("checked", obj.checked);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>
      <input type="checkbox" onclick="SelectAll(this)" />
    </th>
    <th>Name</th>
    <th>
      <input type="checkbox" onclick="SelectAll(this)" />Is Active</th>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>John</td>
    <td>
      <input type="checkbox" name="isActive" />
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Tim</td>
    <td>
      <input type="checkbox" name="isActive" />
    </td>
  </tr>
</table>
<br/>
<h2>
Another Table<hr/>
</h2>
<table>
  <tr>
    <th>
      <input type="checkbox" onclick="SelectAll(this)" />
    </th>
    <th>Name</th>
    <th>
      <input type="checkbox" onclick="SelectAll(this)" />Is Active</th>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>John</td>
    <td>
      <input type="checkbox" name="isActive" />
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Tim</td>
    <td>
      <input type="checkbox" name="isActive" />
    </td>
  </tr>
</table>

For Reference :

nth-child Selects all elements that are the nth-child of their parent.

Fiddle Here

Upvotes: 0

epascarello
epascarello

Reputation: 207501

Basic idea is to select the cell, get the index, and than select the other table cells of the table with that index.

$("th input[type='checkbox']").on("change", function() {
   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
});
th { background-color: #CCC; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>
        <input type="checkbox">
      </th>
      <th>
        <input type="checkbox">
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="checkbox">
      </td>
      <td>
        <input type="checkbox">
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox">
      </td>
      <td>
        <input type="checkbox">
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox">
      </td>
      <td>
        <input type="checkbox">
      </td>
    </tr>
  </tbody>
</table>

If you want to do it with inline event handler like you have it, your code would look like

function CheckHeaderCheckboxAll(obj) {
   var cb = $(obj),           //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", obj.checked);  //select the inputs and [un]check it
}

Upvotes: 5

Ish
Ish

Reputation: 2105

Remove the on click event. rather bind it with class.

<input type="checkbox" value="" class="checkAll" data-class="class1" name="checkAll" id="ctl00_UserMasterContentPlaceHolder_grdUserDetails_ctl01_chkHeaderCheckbox"/>

Give the class name which you want to check

<input type="checkbox" value="" class="class1" />

Try this jquery:

$(".checkAll").click(function () {
     var datacls = $(this).attr('data-class');
     $('input:checkbox.'+datacls).not(this).prop('checked', this.checked);
});

Upvotes: 0

Related Questions