将 json 数组转换为 aadata 格式

发布于 2024-10-19 04:46:04 字数 569 浏览 2 评论 0原文

我当前的数组格式没有被数据表 aaData 格式解释为传递列值:

{
    "aaData": [
        {
            "startDate": "09/08/2010 12:00:00 AM",
            "endDate": "13/08/2010 12:00:00 AM",
            "runDate": "16/08/2010 12:00:00 AM",
            "clientId": "40272",
            "clientType": "C",
            "plannerName": "Adrian Mcfly",
            "plannerRegion": "s1",
            "contact": "Vera chaniqua",
            "email": " ",
            "interviewDate": "09/08/2010 12:00:00 AM"
        },
    ]
}

如何删除列 id 并仅显示值,以便数据表可以将其作为 ajax 调用读取?

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:00 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2024-10-26 04:46:04

编辑 2012 年 8 月 29 日

从 1.9 开始,您可以禁止必须拥有根 JSON 属性。

"sAjaxDataProp": "",

这可能是大多数 JSON 序列化程序所得到的结果。

或者自定义它

"sAjaxDataProp": "myData",

在datatables 1.8中你可以像这样格式化你的json:

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

        ]
    }

并在你的datatable属性中添加这个

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

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"
        }
        ],    
你与清晨阳光 2024-10-26 04:46:04

好吧,基本上你所得到的是对象数组的JSON表示(具有属性startDateendDate,...) ,但您需要的是一个由字符串组成的数组数组。

我假设您正在进行服务器端处理,因此如果您不想更改服务器代码,您可以在获取过程的中间修改数据,然后再将其提供给数据表的回调。

接下来我要做的只是遍历获取的数据中的每个对象并创建值数组:

$('#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)
        } );
    }
} );

} );

希望它有效!

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!

月棠 2024-10-26 04:46:04

尝试使用方括号而不是左/右大括号 { 和 }。有关详细信息,请参阅我对此问题的回答:datatables and jsonformatting error with php

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文