2012-10-04 38 views
0

填充數據表的標頭值我有JSON如下所示從JSON

{ 
    "aaData": [ 

     [ 
     "Name", 
     "Description", 
     "Date" 
     ], 
     [ 
      { 
       "displayValue": "Home Page", 
       "link": "http://somelink.com" 
      }, 
      "London", 
      "1983" 
     ], 
     [ 
      { 
       "displayValue": "Backlog", 
       "link": "http://BacklogApp.com" 
      }, 
      "Paris", 
      "1999" 
     ] 
    ] 
} 

現在,在JS,我填充使用sAjaxSource表如下所示

$(document).ready(function() { 
     $('#genericTable').dataTable({ 
      "bProcessing": true, 
      "sAjaxSource": "resources/json/" + key + ".json", 
      "sPaginationType" : "full_numbers", 
      "bJQueryUI"   : true, 
      "bRetrieve"   : true, 
      "bPaginate"   : true, 
      "bSort"    : true, 
      "aaSorting" : [[ 3, "desc" ]], 
      "iDisplayLength" : 50, 
      "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { 
       if(typeof aData[0] != 'string'){ 
        $('td:eq(0)', nRow).html('<a href="' + aData[0]['link'] +'" >' + 
          aData[0]['displayValue'] + '</a>'); 
       } 
      } 
     }).columnFilter({ sPlaceHolder: "head:after", 
      aoColumns: [ { type: "text" }, 
         { type: "text" }, 
         null 
         ] 
     }); 
    }); 

我能夠填充在數據在jsp中使用硬編碼標題名稱的表,但我想從json填充標題名稱。 (名稱,說明,日期)。我怎樣才能做到這一點。

任何想法???

在此先感謝!

回答

2

可能可以在數據表初始化之前執行調用,以便您已經有了列和數據。根據我的經驗,您必須在「aoColumns」選項中定義列。

這裏就是我的列標題的例子

"aoColumns" : [ 
        { "sTitle" : "Status" }, 
        { "sTitle" : "Name" }, 
        { "sTitle" : "Url" }, 
        { "sTitle" : "Page Views" }, 
        { "sTitle" : "Leads" }, 
        { "sTitle" : "Conv. Rate" }, 
        { "sTitle" : "Actions" } 
       ], 

所以MB寫你的JSON像這樣,然後將其傳遞到intialization在Ajax回調

json = { 
    "aoColumns": [ 
        {"sTitle" : "Column Number 1"}, 
        .... 
       ], 
    "aaData": [ 
       .... 
       ..... 
       ] 
}; 

columns = json['aoColumns']; 
data = json['aaData']; 
+0

「列」:[ { 「sTitle」:「標題名稱」, } ]。 sTitle做了詭計,謝謝 –