13

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

2
  • what does second page mean? Using paging in table? Commented Apr 20, 2013 at 11:33
  • yes i am using pagination in my datatable Commented Apr 20, 2013 at 11:34

7 Answers 7

30

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/

Sign up to request clarification or add additional context in comments.

1 Comment

This is the best answer to this problem.
17

If you just place your click event code above datatable code in js, it will working fine.

As example.

$(".icon-trash").on('click',function () {});

$("#TableID").datatable();

2 Comments

Good answer! The order does indeed matter, thanks a ton
If the datatable ID is not bound (as in the accepted answer), then yeah, the order does matter. If you bind it, then it doesn't seem to matter where the logic goes. I've tested it both ways & they both work. Just seems safer to bind it.
1

I have the same issue and I solved it by moving the below code after I call in other code.

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

Hope it will help someone.

Comments

0

This is an old question but I have faced the same problem and calling the datatable (via script below) after the js click event solves it.

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

Comments

0

I am using yajra datatable + laravel collection. I spent my time nearly about 2-3 hours to debug this and found solution as below.

remove quote string form pageLength parameters , It will save your time.

Comments

0
var table;

$(document).ready(function (){

    table = $('#DataTableId')
        .on('draw.dt', function () {
            yourFunction();
        })

        .DataTable();
});


var yourFunction = function (){

    $('#DataTableId select').off('change')
        .on('change', function () {

        // do whatever

    });

}

1 Comment

While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained.
0

Use the draw callback.

Example:

$('#example').dataTable( {
    "drawCallback": function( settings ) {
        alert( 'DataTables has redrawn the table' );
    }
} );

https://datatables.net/reference/option/drawCallback

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.