【问题标题】:convert json array to aadata format将json数组转换为aadata格式
【发布时间】:2011-07-03 14:33:56
【问题描述】:

我当前的数组格式没有被数据表 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 调用读取?

【问题讨论】:

  • jsonobject.aaData[0] ?
  • 如果那是一个 json 对象(看起来像),那么你可以在 js 中执行类似 delete aaData[0][clientId] 的操作
  • 顺便说一句,你最后缺少] :)
  • 顺便说一句,您应该删除末尾的逗号

标签: javascript jquery json datatables


【解决方案1】:

2012 年 8 月 29 日编辑

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

"sAjaxDataProp": "",

这很可能是大多数 JSON 序列化程序会得到的。

或者自定义

"sAjaxDataProp": "myData",

在数据表 1.8 中,您可以像这样格式化您的 json:

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

        ]
    }

在你的数据表属性中添加这个

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

【讨论】:

    【解决方案2】:

    嗯,基本上你所拥有的是 objects 数组的 JSON 表示(具有属性 startDateendDate、. ..),但您需要的是一个 arrays 字符串数组。

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

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

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

    });

    希望它有效!

    【讨论】:

    • 您在 1.8 中不需要字符串数组。 Allan 现在允许对象。检查我的帖子以获取更新。
    【解决方案3】:

    尝试使用方括号而不是打开/关闭花括号 { 和 }。详情见我对这个问题的回答:datatables and json formatting error with php

    【讨论】:

      猜你喜欢
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多