fnInitComplete - exists or just a myth?
fnInitComplete - exists or just a myth?
mnovakovic
Posts: 5Questions: 0Answers: 0
So what about this callback? Did not do much investigation, but i do not see this documented on api page.
Does this option exist?
I am having problems in my application, because when i have table and i apply datatables plugin, i am seeing it "transforming" because it takes some time for datatables to apply itself to html table. So this callback would be a great way to be sure when to display html.
Does this option exist?
I am having problems in my application, because when i have table and i apply datatables plugin, i am seeing it "transforming" because it takes some time for datatables to apply itself to html table. So this callback would be a great way to be sure when to display html.
This discussion has been closed.
Replies
I have actually just discovered a bug with fnInitComplete in 1.4.1 - it is called too early when sAjaxSource is used to populate the table (in that case it is called before the Ajax retrieved data is put into the table). Is this what you are seeing? If you are using data from the DOM, it most certainly should work.
Allan
I am not using sAjaxSource, i am getting all content in html from server and just applying datatables on that.
I will try this tomorrow on work and will give feedback. Thanks a lot :)
On another note, the simple, well written and highly organized nature of this plug-in deserves the highest praise. That it fits like a glove in to the jQuery environment is also commendable.
Have a look at http://datatables.net/usage#fnInitComplete (the usage page is linked from the top of all pages). With the 1.5 release I'm going to tidy up the documentation on this site.
Thank you for your kind words about this software - it's very much appreciated!
Allan
Allan
[code]
oTable = $('#compounds-list').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": compoundsUrl,
"fnServerData": getCompounds,
"fnInitComplete":addClicks,
});
[/code]
and my addClicks function:
[code]
function addClicks() {
alert(oTable);
//var nodes = oTable.fnGetNodes();
}
[/code]
gets called, and the alert says the oTable is undefined. I am able to use the oTable variable for other sorting/filtering tasks. So does that mean the fnInitComplete is getting called too early?
Thanks,
Craig
PS. I've just been using datatables for two days, and it is perfect for my project (or will be perfect once I figure out this).
Craig
I am having issues with it as well.
Allan
[code]
"fnInitComplete": function() {
alert(this.fnGetNodes().length);
}
[/code]
send me "0"...though the table has 2 row....
Allan
[code]
oTable = $('#channels').dataTable(
{
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": url,
"fnServerData": getJson,
"fnRowCallback": fnRow,
"sDom": 'tr',
"bAutoWidth": false,
"bSortClasses": false,
"aoColumns":
[
/* SELECT */ { "bSortable": false,
"bSearchable": false},
/* CHAN_NAME */ { "bSortable": false,
"bSearchable": false}
]
})
[/code]
I need to know number of rows(all rows, without filtering... i don`t use filtering in this table) after table initialization...but fnInitComplete is getting called too early and gives out incorrect results...
I have found while such decision:
one page has 2 tables - oTable and oTable2(all tables use server-side processing)...oTable2 has url with '&table=2'...
[code]
var oTable_init;
var IntervalID;
function getJson (sSource, aoData, fnCallback)
{
if (this.url.match(/table=2/))
{
if (typeof(oTable_init) != 'undefined') {
clearInterval(IntervalID);
}
else
{
IntervalID = setInterval('oTable2.fnDraw(false)', 1000);
return false;
}
}
$.ajax({
dataType: "json",
type: "POST",
url: sSource,
data: aoData,
success: function (data) {
if (! this.url.match(/table=2/))
oTable_init = data.iTotalDisplayRecords;
fnCallback(data);
},
error: function() {
alert ("Get JSON error");
}
});
}
[/code]
I.e. I should know number of rows in the first table before XHR of the second table...
I have this code: [code] var cTable = $('#matches').dataTable({
//other properties
"sAjaxSource": //valid reference to a WCF service
"sAjaxDataProp": "ResponseData",
"aoColumns": [
//column-related stuff
],
"fnInitComplete": function () {
var cNodes = cTable.fnGetNodes();
//loop through the nodes and do stuff
}
});
[/code] which worked fine. Then, we had to do some user validation between the time we pulled the data and the time we put up the table, so this wouldn't work anymore. I had to use an ajax call and pass the result to the datatable instead, so: [code]
var cTable = $('#matches').dataTable({
//other properties
"aaData": myJson.ResponseData,
"aoColumns": [
//column-related stuff
],
"fnInitComplete": function () {
var cNodes = cTable.fnGetNodes();
//loop through the nodes and do stuff
} //function
}); //datatable
[/code]This wouldn't work. I kept getting a "cTable is undefined" error and no data. Once I pulled the fnInitComplete out and ran it afterwards, everything was fine. So, what I'm getting is that you can reference the pointer to the datatable once the initialization is complete, if you are using a direct call to a data source, but if you are passing the result of an ajax call to the datatable you have to wait until the entire datatable sequence executes.
This might be why capeck was having problems.
[code]
var cTable = $('#matches').dataTable({
"fnInitComplete": function () {
var cNodes = cTable.fnGetNodes();
}
});
[/code]
would fail.
To "fix" this (its not a bug, because it is simply how Javascript works) then you can use the 'this' keyword in the callback function. In all DataTables callbacks 'this' is the DataTables instance - so:
[code]
var cTable = $('#matches').dataTable({
"fnInitComplete": function () {
var cNodes = this.fnGetNodes();
}
});
[/code]
would work.
Allan