数据表列和JSON格式

时间:2012-10-29 13:01:56

标签: json datatables

我已根据以下html

设置了数据表
  <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对象并选择我想在客户端的表上显示哪些列?

1 个答案:

答案 0 :(得分:3)

您可以在aoColumns中使用mData属性,如下所示:

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