pagination and click events
pagination and click events
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
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
This discussion has been closed.
Replies
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
[code]
$( ".edit_button" ).live(
"click",
function(){
$(foo);
});
[/code]