开发者

convert json array to aadata format

my currrent array format is not being interpreted by datatables aaData format as im passing column values:

{
    "aaData": [
        {
            "startDate": "09/08/2010 12:00:00 AM",
            "endDate": "13/08/2010 12:00:00 AM",
            "runDate": "16/08/2010 12:00:0开发者_运维技巧0 AM",
            "clientId": "40272",
            "clientType": "C",
            "plannerName": "Adrian Mcfly",
            "plannerRegion": "s1",
            "contact": "Vera chaniqua",
            "email": " ",
            "interviewDate": "09/08/2010 12:00:00 AM"
        },
    ]
}

how do i remove the column id and display just the values so that i can be read by datatables as a ajax call?


EDIT 8/29/2012

As of 1.9 you can disable having to have a root JSON property.

"sAjaxDataProp": "",

This is likely what you will get with most JSON serializers.

Or customize it

"sAjaxDataProp": "myData",

In datatables 1.8 you can format your json like this:

{
        "aaData": [
            {
                "DT_RowClass": "",
                "description": "",             
                "pkgLineTree": {
                    "treeId": {
                        "name": "Jacksonville"
                    }
                }              
            },
            {
                "DT_RowClass": "",
                "description": "",       
                "pkgLineTree": {
                    "treeId": {
                        "name": "Jacksonville"
                    }
                }         
            }

        ]
    }

And in your datatable properties add this

"aoColumns": [    
        {
            "mDataProp": "pkgLineTree.treeId.name"
        },  
        {
            "mDataProp": "shortname"
        },
        {
            "mDataProp": "description"
        }, 
        {
            "mDataProp": "lineStatus"
        }
        ],    


Well, basically what you've got there is a JSON representation of an array of objects (with properties startDate, endDate, ...), but what you need is an array of arrays of strings.

I'm assuming you are doing server-side processing, so if you don't want to change the server code, you can get just in the middle of the fetching process and modify the data right before giving it to the callback of datatables.

What I do next is just going through each object in the fetched data and create the array of values:

$('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/server_processing.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
        $.getJSON( sSource, aoData, function (json) { 
            /* --- Here is where we massage the data --- */
            /* if the variable "json" is just a string (I forgot) then evaluate it first into an object (download a json library)*/
            var aaData=[];
            $.each(json, function(index, object) {
                var aData=[];
                $.each(object, function(key, value) { 
                    aData.push(value); //be careful here, you might put things in the wrong column 
                });
                aaData.push(aData);
            }); 
            /* --- And after we're done, we give the correctly formatted data to datatables --- */  /* --- if "json" was a string and not an object, then stringify aaData first (object to json notation, download a library) --- */
            fnCallback(aaData)
        } );
    }
} );

} );

Hope it works!


Try using square brackets instead of opening/closing curly braces { and }. For details, see my answer to this question: datatables and json formatting error with php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜