2011-02-24 120 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" 
     } 
     ],