SQL Column Names as Table Headers?

SQL Column Names as Table Headers?

ZaLiTHkAZaLiTHkA Posts: 10Questions: 1Answers: 0
edited August 2012 in DataTables 1.9
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.

Replies

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Perfectly possible, but it doesn't really have much to do with DataTables, but rather the server-side script that you are using to get the data :-). You can get the JSON like that (it is very unusual to have spaces in JSON keys, but it is possible) and then use mData ( http://datatables.net/blog/Extended_data_source_options_with_DataTables ) to tell DataTables what keys you want to use from the JSON.

    Allan
  • ZaLiTHkAZaLiTHkA Posts: 10Questions: 1Answers: 0
    edited August 2012
    Thanks Allan, I'm not sure I follow exactly what you mean... Just to check, currently I'm getting this back from the server:

    [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.
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    I think I see what you are looking for now:

    > 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
  • ZaLiTHkAZaLiTHkA Posts: 10Questions: 1Answers: 0
    Ah, I see.. I somehow doubt something like this would really be used by too many people, so if DT doesn't already handle it, it's probably not really worth your time to include it... :)

    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
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    As DataTables development progresses, I expect that being able to offer this kind of thing will actually become much easier (the ability to add / remove columns specifically). I'm not yet sure if that will make it into 1.10 - we'll see :-)

    Allan
This discussion has been closed.