How to map incoming data fields of an ajax response into data table columns

How to map incoming data fields of an ajax response into data table columns

rv_nathrv_nath Posts: 7Questions: 0Answers: 0
edited December 2013 in DataTables 1.9
HI,

From backend, I get a JSON response containing the following fields.

{
"aaData": [
{
"id": "94",
"category": "bats",
"rate": 1600,
"discnt_percent": 5.5,
"name": "MRF Bat",
"code": "91",
"catid": "1",
"cost": "1500",
"taxes": "14.5",
"units": "nos",
"qty" : "100",
"vat" : "5"
},
{ ...},
]

In my HTML table display, I want to show only code, name and qty columns, by ignoring other data.

I am trying to use the below code, but it doesn't work.

[code]
$("#item-list-table").dataTable({
"bProcessing" : false,
"sAjaxSource" : "responses/item-list.json",
"aoColumns" : [
{"mData" : "code"},
{"mData" : "name"},
{"mData" : "qty"}

]
});

[/code]

In fact, I don't understand how datatables maps the incoming source fields into respective html columns. Went through aoColumnDefs and aoColumns. But couldn't understand either of them for this usecase. Any help is greatly appreciated.

thanks and regards,
RV

Replies

  • rv_nathrv_nath Posts: 7Questions: 0Answers: 0
    edited December 2013
    Sorry, my bad. I realized that the incoming data is not in a format readily understood/accepted by Datatables.

    So, I wrote a transformation fn, by implementing fnServerData callback, which converts the data into

    "aaData" : [
    [col1_data, col2_data, col3_data].
    [col1_data, col2_data, col3_data].
    [...]
    ]

    sorry for the spam created.

    For any other dumb people like me, here is what I did.
    [code]
    $("#item-list-table").dataTable({
    "bProcessing" : false,
    "sAjaxSource" : "responses/item-list.json",
    "sDom": '<"top"i>rt<"bottom"flp><"clear">',
    "fnServerData" : function (sSource, aoData, fnCallback, oSettings) {

    oSettings.jqXHR = $.ajax({
    "dataType": "json",
    "url": sSource,
    "data": aoData,
    "success": function(data) {
    //do your stuff here
    transform2DataTableFormat(data);
    //finally call the original fn.
    fncallback(data);
    }
    });
    }
    });


    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Your incoming data looks just fine to me. Use mData as you were to get the data from the objects. If that isn't working, then there is something else going wrong. Can you link to the page please.

    See also: http://datatables.net/blog/Extended_data_source_options_with_DataTables

    Allan
This discussion has been closed.