0

我希望生成一个以 ajax 响应为源的 DataTable。我使用 GSON 库将员工的 ArrayList 转换为 JSON 字符串。我得到的 JSON 字符串是

{
"sEcho": 3,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
    {
        "id": 1,
        "firstName": "darsheet",
        "lastName": "shah",
        "city": "san jose",
        "state": "ca",
        "zip": 95112
    },
    {
        "id": 2,
        "firstName": "akshat",
        "lastName": "shah",
        "city": "ahmedabad",
        "state": "gj",
        "zip": 380061
    }
]

}

但是 sAjaxSource 属性需要以下格式的 JSON 字符串

{
"sEcho": 3,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
    {
        "0": 1,
        "1": "darsheet",
        "2": "shah",
        "3": "san jose",
        "4": "ca",
        "5": 95112
    },
    {
        "0": 2,
        "1": "akshat",
        "2": "shah",
        "3": "ahmedabad",
        "4": "gj",
        "5": 380061
    }
]

}

我使用的数据表代码是

$(document).ready(function(){
$('#refresh').click(function(){
    $('#emp').dataTable({
        "sAjaxSource":".../ExploreDatatable/loadTableAjax"  
    });

});
});

如何消除 JSON 结构中的这种不匹配?

谢谢。

4

1 回答 1

0

据我所知,您的数据表选项是错误的。它应该这样做:

$('#emp').dataTable({
        "sAjaxSource":".../ExploreDatatable/loadTableAjax",
        "aoColumns": [{"mDataProp": "id"},
                      {"mDataProp": "firstName"},
                         .....the other column names, that should be mapped to the json key......
                     ]
    });
于 2013-03-12T00:56:30.563 回答