Dynamic Column Names
Dynamic Column Names
Ok, I have searched like crazy here for this and tried everything I have seen but no luck. What I am trying to do is use the title of the columns to display the date of my data. So basically spreadsheet like with the left column being static names and data will paginate the columns not rows.
I am using server data via PHP so how do I need to format the return data and how would I use it in the datatables code.
Thanks
I am using server data via PHP so how do I need to format the return data and how would I use it in the datatables code.
Thanks
This discussion has been closed.
Replies
1) on the server side, return extra data in your json that defines the column titles
I talk about adding extra data to JSON responses in this thread (I recommend making a simple 0-indexed array of titles): http://www.datatables.net/forums/discussion/7102/badly-need-help-parsing-http-variable-to-server_processing.php/p1
2) when receiving the JSON, retrieve the data containing column titles (see above link) and I believe you can write those values into the settings's object's aoColumns "sTitle" fields
[code]for (col in oTable.fnSettings().aoColumns) {
col.sTitle = COL_FROM_JSON[col]; // ** col will be an index to the aoColumns array, so you might want your JSON to return an array of column titles
}[/code]
------[edit]---------
** oops. I do this often when switching between languages. "col", in javascript, would be the array key, not the value.. should have been
[code]oTable.fnSettings().aoColumns[col].sTitle[/code]
as you pointed out in your response
also seems from your response that writing to the aoColumns in the settings object doesn't affect the data in the table header.. it's basically a copy of the values, not controlling the values.
I think that is close but still not working. Here is what I tried, I can alert the current value of sTitle and its right for what I have in the template and I do have an array coming in through data and can alert those as well, but can not set them. Here is what I tried
[code]
"success": function(data, textStatus, XMLHttpRequest)
{
for (col in oTable.fnSettings().aoColumns)
{
//alert(oTable.fnSettings().aoColumns[col].sTitle);
//col.sTitle = COL_FROM_JSON[col]; // col will be an index to the aoColumns array
oTable.fnSettings().aoColumns[col].sTitle = data.aoColumns[col];
}
fnCallback(data, textStatus, XMLHttpRequest);
}
[/code]
[code]$('#table_form thead th:eq('+col+')').html( data.aoColumns[col] );[/code]
[code]oTable.dataTableSettings[0].nTHead.children[thRow].children[col].innerHTML[/code]
fbas, yea seems I can't use that as a setter so I am still looking for a shorter access then the one repairman gave.