2011-02-24 119 views
3

我currrent阵列格式不被数据表aaData格式解释为IM传递列的值:转换JSON数组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调用?

+0

'jsonobject.aaData [0]'? – Nishant 2011-02-24 14:13:23

+0

如果这是一个JSON对象(看起来像它),那么你可以做一些像'删除aaData [0] [clientId]'在js – Val 2011-02-24 14:15:41

+2

btw你在末尾丢失']' – Val 2011-02-24 14:17:07

回答

1

好了,基本上你有什么有对象(包含属性的startDate结束日期 ...)的数组的JSON表示,但你需要的是数组的数组字符串的

我假设你正在做服务器端的处理,所以如果你不想改变服务器代码,你可以在获取过程中获取数据,并在将数据提供给数据表的回调。

我下一步做仅仅是通过每个对象要在取出的数据,并创建值的阵列:

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

你不需要1.8中的字符串数组。 Allan现在允许对象。检查我的帖子进行更新。 – 2011-07-07 12:28:26

7

编辑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" 
     } 
     ],