Defining Columns from data sources

Defining Columns from data sources

brightblueskybrightbluesky Posts: 3Questions: 0Answers: 0
edited September 2013 in DataTables 1.9
Hi,

I am new to DataTables and will like to ask if it is possible to create a table and using the datasource to create the columns definition. Tried searching but can't find any discussions


var aDataSet = [{
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"version": "4",
"grade": "X"
},
{
"engine": "Trident",
"browser": "Internet Explorer 5.0",
"platform": "Win 95+",
"version": "5",
"grade": "C"
},
{
"engine": "Trident",
"browser": "Internet Explorer 5.5",
"platform": "Win 95+",
"version": "5.5",
"grade": "A"
}];




$(document).ready(function() {
$('#dynamic').html('');');
$('#example').dataTable({
"aaData": aDataSet ,
"aoColumns": [
{"mData": "engine"},
{"mData": "browser"},
{"mData": "platform"},
{"mData": "version"},
{"mData": "grade"}

]

});

});

so rather than defining the aoColumns, is it possible to get the object keys to define the columns

Replies

  • brightblueskybrightbluesky Posts: 3Questions: 0Answers: 0
    I got the answer

    var aDataSet = [{
    "engine": "Trident",
    "browser": "Internet Explorer 4.0",
    "platform": "Win 95+",
    "version": "4",
    "grade": "X"
    },
    {
    "engine": "Trident",
    "browser": "Internet Explorer 5.0",
    "platform": "Win 95+",
    "version": "5",
    "grade": "C"
    },
    {
    "engine": "Trident",
    "browser": "Internet Explorer 5.5",
    "platform": "Win 95+",
    "version": "5.5",
    "grade": "A"
    }];




    var mDataArray = new Array();
    var keys = Object.keys(aDataSet[0]);
    for (var i = 0; i < keys.length; i++)
    {
    var temp = new Object();
    temp["mData"] = keys[i].toString();
    mDataArray.push(temp);
    }




    $(document).ready(function() {

    $('#example').dataTable({
    "aaData": aDataSet,
    "aoColumns": mDataArray
    });


    });
This discussion has been closed.