Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery data table not working from second page

I am using jquery Data table 1.9.4 and jquery 1.9.1 and i am trying to perform ajax call like this on click event.

$(".icon-trash").on('click',function () {
                // alert($(this).attr('id'));
                $.post('/WorkOrderRequest/DeleteWOR',
                        { id: $(this).attr('id') },

                        function (returndata) 
                        {

                            if (returndata.ok) 
                            {
                                window.alert(' deleted!');
                                $("#emp" + idemployee).hide('slow');
                            }
                            else {
                                window.alert(' error : ' + returndata.message);
                            }

                    });

            });

html part:

@foreach (var item in Model)
                    {

                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.ProjectCode)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.WO_Date)
                            </td>


                            <td>
                             <a  id='@(item.WO_ID)' class="icon-edit" />
                               |                               
                             <a  id='@(item.WO_ID)' class="icon-trash" />
                           </td>
                        </tr>
                    }

It is working fine on first page but from second page its not showing any result. Please help

like image 260
Satendra Jindal Avatar asked Apr 20 '13 11:04

Satendra Jindal


1 Answers

The way pagination works, rows not needed by current page are removed from the DOM. Therefore any event handlers you bind directly to those elements are lost when the html is removed.

You need to use delegation syntax of on() to bind the handler to an asset in page that is permanent. This could be the parent table or any other parent up the tree, including document

$('#TableID').on('click','.icon-trash',function () {...

API Reference: http://api.jquery.com/on/

like image 196
charlietfl Avatar answered Oct 14 '22 17:10

charlietfl