lundi 27 novembre 2017

Select Checkbox !== Select Row Table

The select/unselect button works on the checkbox.

But it does not work for the row table.

//Select row table
$('#example').on('click', 'tr', function() {
  var $row = $(this),
    isSelected = $row.hasClass('selected')
  $row.toggleClass('selected')
    .find(':checkbox').prop('checked', !isSelected);
});

// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
  var selectAll = this.id === 'selectAll';
  $("#example tr :checkbox").prop('checked', selectAll);
});

I think the checklist is just for display, for row selected and to mark it.

How when the select / unselect button is clicked,

it select on row table too, Not just on the checkbox?

Code Snippet Demonstration :

$('#example').dataTable();

//Select row table
$('#example').on('click', 'tr', function() {
  var $row = $(this),
    isSelected = $row.hasClass('selected')
  $row.toggleClass('selected')
    .find(':checkbox').prop('checked', !isSelected);
});

// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
  var selectAll = this.id === 'selectAll';
  $("#example tr :checkbox").prop('checked', selectAll);
});
<script src="http://ift.tt/20g0BuL"></script>
<script src="http://ift.tt/2eY7LrW"></script>
<link href="http://ift.tt/2jfHpCW" rel="stylesheet"/>


<button type="button" id="selectAll"> Select </button>
<button type="button" id="unselectAll"> UnSelect </button>

<table id="example" class="myclass" />
<thead>
  <tr>
    <th>
    </th>
    <th>Name</th>
    <th>Company</th>
    <th>Employee Type</th>
    <th>Address</th>
    <th>Country</th>
  </tr>
</thead>
<tbody>

  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Calvin</td>
    <td>TCS</td>
    <td>IT</td>
    <td>San Francisco</td>
    <td>US</td>
  </tr>

  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Ananda</td>
    <td>TCS</td>
    <td>IT</td>
    <td>San Francisco</td>
    <td>US</td>
  </tr>

  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>John</td>
    <td>TCS</td>
    <td>IT</td>
    <td>San Francisco</td>
    <td>US</td>
  </tr>

  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Doe</td>
    <td>TCS</td>
    <td>IT</td>
    <td>San Francisco</td>
    <td>US</td>
  </tr>
</tbody>

JSFiddle




Aucun commentaire:

Enregistrer un commentaire