aoColumnDefs with dynamic column data

aoColumnDefs with dynamic column data

dnugrohoMCdnugrohoMC Posts: 5Questions: 0Answers: 0
edited September 2012 in General
I am trying to create a DataTable parameter aoColumnDefs with dynamic column data. I would like to create a JavaScript array (JSON format) for the dynamic column data.

But I get stuck how to create the value for property aTargets. Here the following codes that I have tried:

[code]
var aryJSONColTable = { columns: [] };

for (var i in aryColTableChecked) {
aryJSONColTable.columns.push({
"sTitle": aryColTableChecked[i],
"aTargets": [i]
});
};
[/code]

But I always get the value of aTargets is [1] for whole loop iteration.

I would like to create a DataTable like this:

[code]
oTable = $('#report').dataTable({
"aoColumnDefs": [
{ "sTitle": "Column1", "aTargets": [0]},
{ "sTitle": "Column2", "aTargets": [1]},
{ "sTitle": "Column3", "aTargets": [2]},
{ "sTitle": "Column4", "aTargets": [3]}
],
"bProcessing": true,
"bServerSide": true,
"bLengthChange": true,
"bFilter": true,
"aaSorting": [[ 1, "desc" ]],
"sScrollX": "100%",
"bScrollCollapse": true,
"bJQueryUI": true,
"sAjaxSource": "./getDataEA.php"
});
[/code]

and replace it with the following:

[code]
oTable = $('#report').dataTable({
"aoColumnDefs": aryJSONColTable.columns,
"bProcessing": true,
"bServerSide": true,
"bLengthChange": true,
"bFilter": true,
"aaSorting": [[ 1, "desc" ]],
"sScrollX": "100%",
"bScrollCollapse": true,
"bJQueryUI": true,
"sAjaxSource": "./getDataEA.php"
});
[/code]

Actually I am inspired with a discussion solution at http://www.datatables.net/forums/discussion/6260/dynamic-column-names/p1 , but in my case I need to use aoColumnDefs for my DataTables.

Please kindly assist me on this matter soon. Thank you in advance for your help.

Replies

  • dnugrohoMCdnugrohoMC Posts: 5Questions: 0Answers: 0
    With help of another forum, I have got the solution. Here it is the answer for my above query.

    [code]
    //create JSON array for aoColumnDefs
    var aryColTableChecked = ["column00", "column01", "column02", "column03"];
    var aryJSONColTable = [];

    for (var i=0; i < aryColTableChecked.length; i++ ) {
    aryJSONColTable.push({
    "sTitle": aryColTableChecked[i],
    "aTargets": [i]
    });
    };

    oTable = $('#report').dataTable({
    "aoColumnDefs": aryJSONColTable,
    "bProcessing": true,
    "bServerSide": true,
    "bLengthChange": true,
    "bFilter": true,
    "aaSorting": [[ 1, "desc" ]],
    "sScrollX": "100%",
    "bScrollCollapse": true,
    "bJQueryUI": true,
    "sAjaxSource": "./getDataEA.php"
    });
    [/code]
  • JugramJugram Posts: 10Questions: 0Answers: 0
    exactly what i was lokking for.. thumps up :)
This discussion has been closed.