pagination and click events

pagination and click events

kenken Posts: 1Questions: 0Answers: 0
edited January 2010 in General
Dear Allan,

First and foremost, compliments on a well-written, well-documented, and easy to use plug-in! Thank you so so much.

Please pardon me if this question has been already asked and answered before, but when I try to add event listeners for my rows with pagination enabled, only the first page gets the event handler added. For example, in the fnInitComplete callback function, I added the following code:

[code]
$("tbody>tr").click(function() { alert("hi there!"); });
[/code]

I can understand why this is happening because only the first page of rows are in the DOM after the initialization is completed.

So to overcome my issue, I used the following code:

[code]
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {

$(nRow).unbind("click"); // otherwise, it keeps adding to the click handler!
$(nRow).click(function(){
var aPos = oTable.fnGetPosition(nRow);
cID = oTable.fnGetData(aPos)[0]; // just an ID in my returned data
sTitle = oTable.fnGetData(aPos)[1]; // the title label in my returned data
alert(cID + " " + sTitle);
});

return nRow;
}
[/code]

So I guess my question is if this is the correct way to do it? Is there a more efficient method to accomplish what I'm trying here?

Once again, thanks so much for an amazing plugin. Keep up the great work.

Cheers,

Ken

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Ken,

    There are a number of ways to overcome this:

    Before initialising DataTables: http://datatables.net/examples/advanced_init/events_pre_init.html
    After initialising DataTables: http://datatables.net/examples/advanced_init/events_post_init.html
    Or use jQuery live events.

    Regards,
    Allan
  • bigsilkbigsilk Posts: 7Questions: 0Answers: 0
    I used live and it worked great. I have a table where each record can be edited by clicking, oddly enough, an 'Edit' button. Pages after first weren't working, so:

    [code]
    $( ".edit_button" ).live(
    "click",
    function(){
    $(foo);
    });
    [/code]
This discussion has been closed.