Dynamic Column Names

Dynamic Column Names

n00b77n00b77 Posts: 6Questions: 0Answers: 0
edited October 2011 in General
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

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited October 2011
    DataTables doesn't support this natively, but you can do it yourself:

    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.
  • n00b77n00b77 Posts: 6Questions: 0Answers: 0
    Thanks fbas,
    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]
  • n00b77n00b77 Posts: 6Questions: 0Answers: 0
    Well I did this and it works but just doesn't seem like the right way.

    [code]$('#table_form thead th:eq('+col+')').html( data.aoColumns[col] );[/code]
  • repairman2003repairman2003 Posts: 2Questions: 0Answers: 0
    edited October 2011
    I was able to get it by using:

    [code]oTable.dataTableSettings[0].nTHead.children[thRow].children[col].innerHTML[/code]
  • n00b77n00b77 Posts: 6Questions: 0Answers: 0
    Thanks repairman that did work and good first post. I would rather use the datatable object rather than the jquery dom.

    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.
This discussion has been closed.