Reputation: 431
i am using bootstrap datatable for pagination.I also added click event to each row.But the click event fired only in the first page. It does not work after sorting or pagination.Here is my php code to display data
<table id='tblCustomers' class='table table-bordered table-striped'>
<thead>
<tr>
<th>Customer id</th>
<th>Company</th>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Last login</th>
<th>No Of logins </th>
</tr>
</thead>
<tbody>";
foreach ($this->result as $row) {
echo "<tr>
<td>{$row['customerid']} </td>
<td>{$row['company']} </td>
<td>{$row['firstname']} </td>
<td>{$row['lastname']} </td>
<td>{$row['email']} </td>
<td>{$row['lastlogin']} </td>
<td>{$row['count']}</td>
</tr>";
}
echo "</tbody></table>";
and the jquery code is
$(function () {
$("#tblCustomers").dataTable();
$("#tblCustomers tr").click(function(){
alert($(this).find('td:first').text());
});
});
Upvotes: 4
Views: 5521
Reputation: 115
Please note that, even the search and filter won't take the row click event. if you are providing the events on row like click,double-click.
On filter and On search make sure you call that function which has all your custom scripts.
Upvotes: 0
Reputation: 6031
change code to below . this is call Event Delegation
$(function () {
$("#tblCustomers").dataTable();
$(document).on('click',"#tblCustomers tr",function(){
alert($(this).find('td:first').text());
});
});
Upvotes: 7
Reputation: 8851
Try using $("#tblCustomers tr").on('click', function(){...}
instead of the regular click
..
Upvotes: 0