SQL Column Names as Table Headers?
SQL Column Names as Table Headers?
ZaLiTHkA
Posts: 10Questions: 1Answers: 0
Hope this hasn't been covered a thousand times already... I couldn't find anything in the documentation about this and the search page doesn't seem to work for me (the results iFrame shows blank until I open it in a new tab, and then it won't let me scroll).
Anyway, the question at hand right now is regarding my datatable headers. I prefer to select data from SQL using the following basic syntax:
[code]SELECT
Table.FirstColumn AS [Pretty Column Name],
Table.SecondColumn AS [Another Pretty Column Name]
FROM [Table][/code]
While using AJAX for the table data source, I've noticed the names I specify in my model are pulled through into the data, now I'd like to use those names to generate the header for the datatable.
Is this possible with DataTables? If so, please could someone tell me how...? In case it makes a difference, I'm currently using 1.9.2.
Anyway, the question at hand right now is regarding my datatable headers. I prefer to select data from SQL using the following basic syntax:
[code]SELECT
Table.FirstColumn AS [Pretty Column Name],
Table.SecondColumn AS [Another Pretty Column Name]
FROM [Table][/code]
While using AJAX for the table data source, I've noticed the names I specify in my model are pulled through into the data, now I'd like to use those names to generate the header for the datatable.
Is this possible with DataTables? If so, please could someone tell me how...? In case it makes a difference, I'm currently using 1.9.2.
This discussion has been closed.
Replies
Allan
[code]aaData: [[5, 5, Alphapos, alphapos, 0, 9999],…]
iTotalDisplayRecords: 38
iTotalRecords: 38
sColumns: "[hidden_ID],[ID],[Company Name],[AEC Password],[Store ID Range - Start],[Store ID Range - End]"
sEcho: 0[/code]
So, as far as I understand your reply, I already have what I need in the response and I shouldn't need to alter the JSON structure then, correct?
I can't figure out how to get sColumns out of the data with mData though, I'm pretty comfortable with PHP, but still quite new to JavaScript/jQuery.. :(
Edit: Just noticed the time, I'm leaving work now, so I may not reply again this evening.. I'll dig into this again tomorrow morning and see what I can do.
> I can't figure out how to get sColumns out of the data with mData though
You don't... This isn't something that is supported by DataTables at the moment - in fact the sColumns parameter is deprecated and will be removed in 1.10 - its use is only for column ordering, which is now done with mData .
So basically you want to tell DataTables what you column names are on the Ajax load, right? That isn't actually an option that is directly built into DataTables at the moment (it might be in future, but not yet). There are two options off the top of my head:
1. Make an Ajax call to the server to discover the column names and then construct your table with that information and then initialise DataTables.
2. If you know how many columns the table is going to have, you can use fnInitComplete to access the information sent back in the JSON and use standard jQuery / DOM methods to update the column headers (there isn't an API method for that, although a plug-in could be made!).
Allan
I think I'll play around with constructing the table frame before loading data into it then, something tells me that will be the most practical option for this particular project.
Thanks again for the help, greatly appreciated. :D
Allan