Getting column names from AJAX source
Getting column names from AJAX source
I had searched this forum for an answer for it without success.
My intention is getting aoColumns array (with bSortable, sType and sTitle values) from AJAX source.
This is my code:
[code]
function fnDataTablesPipeline ( sSource, aoData, fnCallback ) {
var iPipe = 5;
aoData.push({
"name":"colName",
"value":"COL1"
});
var bNeedServer = false;
var sEcho = fnGetKey(aoData, "sEcho");
var iRequestStart = fnGetKey(aoData, "iDisplayStart");
var iRequestLength = fnGetKey(aoData, "iDisplayLength");
var iRequestEnd = iRequestStart + iRequestLength;
oCache.iDisplayStart = iRequestStart;
/* outside pipeline? */
if ( oCache.iCacheLower < 0 || iRequestStart < oCache.iCacheLower || iRequestEnd > oCache.iCacheUpper )
{
bNeedServer = true;
}
/* sorting etc changed? */
if ( oCache.lastRequest && !bNeedServer )
{
for( var i=0, iLen=aoData.length ; i
My intention is getting aoColumns array (with bSortable, sType and sTitle values) from AJAX source.
This is my code:
[code]
function fnDataTablesPipeline ( sSource, aoData, fnCallback ) {
var iPipe = 5;
aoData.push({
"name":"colName",
"value":"COL1"
});
var bNeedServer = false;
var sEcho = fnGetKey(aoData, "sEcho");
var iRequestStart = fnGetKey(aoData, "iDisplayStart");
var iRequestLength = fnGetKey(aoData, "iDisplayLength");
var iRequestEnd = iRequestStart + iRequestLength;
oCache.iDisplayStart = iRequestStart;
/* outside pipeline? */
if ( oCache.iCacheLower < 0 || iRequestStart < oCache.iCacheLower || iRequestEnd > oCache.iCacheUpper )
{
bNeedServer = true;
}
/* sorting etc changed? */
if ( oCache.lastRequest && !bNeedServer )
{
for( var i=0, iLen=aoData.length ; i
This discussion has been closed.
Replies
It won't be perfect with server-side processing though as you'll need two calls to the server, one for aoColumns and one for aaData (the second is the one DataTables will make itself). It is possible to work around this I suspect, but it would involve tinkering with some of the internals of DataTables and could be rather messy.
Regards,
Allan
My problem doing that is that I need some variables in my PHP script sent by datatables like $_POST['sSearch'], $_POST['bSearchableX'], $_POST['iSortingCols']... (my script is very similar to server_processing.php from datatables examples).
When I do first AJAX call, these variables aren't sent and I retrieve errors from PHP script.
[code]
$.ajax({
"dataType": "json",
"type": "POST",
"url": sSource,
"data": aoData,
"success": function (json) {
/* Callback processing */
oCache.lastJson = jQuery.extend(true, {}, json);
if ( oCache.iCacheLower != oCache.iDisplayStart )
{
json.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
}
json.aaData.splice( oCache.iDisplayLength, json.aaData.length );
gTable.fnSettings().aoColumns[1].sTitle = 'Datatables Title 1';
fnCallback(json);
}
});
[/code]
If then, I write "console.log(gTable.fnSettings().aoColumns[1].sTitle)", firebug shows me the correct title (Datatables Title 1), but the column title in datatable remains empty.
I have tried to redraw the table with "gTable.fnDraw(false)" with no results.
It would be a good solution for me because, in this callback, I can access the json data returned by the server and this data contains the aoColumns array that I like use in my table.
Thanks for the clarification - I think I understand what you are getting at now. So the column titles are the key thing for you here, and it looks like you've got about 99% of it already done :-). The one this which is yet to be done, as you point out, is to write the title to the DOM. Javascript (at least 'mass consumption' Javascript) doesn't provide getters and setters yet, so DataTables can't write to the DOM for you when you do the setting of sTitle. However, it's a fairly simple line to add it in:
[code]
gTable.fnSettings().aoColumns[1].nTh.innerHTML = 'Datatables Title 1';
[/code]
Regards,
Allan
How can I change these other?
It's not generally possible to modify DataTables settings during run time - the sTitle option above makes us of the internal structure, so it is possible, but you need to know what to set and how. The way that I would typically recommend getting the full aoColumns object is shown in the thread I liked to - something like this (a bit simplified):
[code]
$(document).ready(function() {
$.ajax( {
"dataType": 'text',
"type": "GET",
"url": "dtJSON.txt",
"success": function (dataStr) {
var data = eval( '('+dataStr+')' );
$('#example').dataTable({
"aaData": data.aaData,
"aoColumns": data.aoColumns,
});
}
} );
});
[/code]
Allan