2012-10-29 57 views
0

我已經安裝基於以下HTML數據表列和JSON格式

<script> 
     $('#tableListing').dataTable({ 
     "bAutoWidth" : true, 
     "bProcessing" : true, 
     "bServerSide" : true, 
     "sAjaxSource" : '${actionUrl}', 
     "aoColumns":[ 
         { "sTitle": "ID", "mData": "id" }, 
         { "sTitle": "Username", "mData": "userName" }, 
         { "sTitle": "Password", "mData": "password" } 
        ] 

      } 
    }); 
    </script> 

<table id="tableListing" class="display datatable"> 
      <thead> 
       <tr> 
        <th><spring:message code="columnLabel.id" /></th> 
        <th><spring:message code="columnLabel.username" /></th> 
        <th><spring:message code="columnLabel.enabled" /></th> 
       </tr> 
      </thead> 
</table> 

而且從服務器的JSON響應一個數據表是[1,"abiieez",true]和它工作得很好。不過,我已經改變了從服務器的響應成類似:

* 編輯(更新JSON響應)*

{ 
    "iTotalDisplayRecords": 11, 
    "iTotalRecords": 11, 
    "aaData": "[{\"creationTime\":0,\"enabled\":true,\"id\":1,\"loginDuration\":0,\"online\":false,\"password\":\"asdasd\",\"userName\":\"abiieez\"},{\"creationTime\":0,\"enabled\":false,\"id\":105,\"loginDuration\":0,\"online\":false,\"password\":\"asdasd\",\"userName\":\"asd1212123\"},{\"creationTime\":0,\"enabled\":false,\"id\":106,\"loginDuration\":0,\"online\":false,\"password\":\"asdsdasd\",\"userName\":\"asdasd23123\"},{\"creationTime\":0,\"enabled\":false,\"id\":107,\"loginDuration\":0,\"online\":false,\"password\":\"asdsdasd\",\"userName\":\"asdasd23123\"},{\"creationTime\":0,\"enabled\":false,\"id\":108,\"loginDuration\":0,\"online\":false,\"password\":\"anak jalanan\",\"userName\":\"alibaba90\"},{\"creationTime\":1351338218227,\"enabled\":false,\"id\":109,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338263527,\"enabled\":false,\"id\":110,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338265078,\"enabled\":false,\"id\":111,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338266329,\"enabled\":false,\"id\":112,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338267247,\"enabled\":false,\"id\":113,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"}]", 
    "sEcho": "1" 
} 

以上JSON是不是由數據表接受,我認爲,由於列數不匹配。

有沒有辦法讓我檢索這個json對象,並選擇我想在客戶端的表上顯示哪些列?

回答

3

可以使用MDATA財產aoColumns這樣的:

$(document).ready(function() { 
      $('#tableListing').dataTable({ 
       "aoColumns":[ 
          { "sTitle": "user", "mData": "userName" }, 
          { "sTitle": "pass", "mData": "password" }, 
          ... 
          ] 
       "bAutoWidth" : true, 
       "bProcessing" : true, 
       "bServerSide" : true, 
       "sAjaxSource" : '${actionUrl}' 
      }); 
    }); 
+0

難道我已經把sColumns從服務器的JSON響應?我還需要在html中指定3列嗎?我仍然有錯誤「請求來自行0的數據源的未知參數'id'」。 – abiieez

+0

您應該在表初始化期間在html中指定「aoColumns」。 – Smiter

+0

我仍然面臨同樣的錯誤。我更新了我的數據表初始化。 – abiieez