Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

row click event not fired after pagination in bootstrap datatable

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());
                         });
                       });
like image 248
Sherin Avatar asked Oct 28 '25 20:10

Sherin


1 Answers

change code to below . this is call Event Delegation

    $(function () {
           $("#tblCustomers").dataTable();
           $(document).on('click',"#tblCustomers tr",function(){
                     alert($(this).find('td:first').text());
           });
    });
like image 87
Nishit Maheta Avatar answered Oct 31 '25 12:10

Nishit Maheta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!