【发布时间】:2014-04-22 15:05:58
【问题描述】:
我正在使用 sAjaxSource 从我的 spring 控制器中获取数据,该控制器给出 json 响应。
像这样的
[
{
"id":20009,
"title":"1914 tran by H. Rackham",
"description":"\"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charmces ",
"username":"nessudi",
"datecreated":"Apr 22, 2014 10:39:24 AM"
},
{
"id":20008,
"title":"The standard Lorem Ipsum passage, used since the 1500s",
"description":"\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\"",
"username":"naai",
"datecreated":"Apr 22, 2014 10:38:46 AM"
},
{
"id":20065,
"title":"Where to get Lorem Ipsum",
"description":"Therpetition, injected humour, or non-characteristic words etc.",
"username":"nensi",
"datecreated":"Apr 22, 2014 10:37:34 AM"
},
{
"id":20056,
"title":"What is Lorem Ipsum",
"description":"Lorrinter took a galley of type and scrambled it to make a type specimen book. ",
"username":"xyz",
"datecreated":"Apr 22, 2014 10:28:39 AM"
}
]
虽然没有数据时数据表会显示“正在加载......”消息。
我阅读了其他建议使用“{aaData:[ ]}”发送空数组的帖子..但我正在使用 "sAjaxDataProp": "" ..so 我的数据属性为空.. 我该如何处理?我在 java 中使用 gson 库。
这是我的控制器
@RequestMapping(value="/theurl", method = RequestMethod.GET)
public @ResponseBody String somemethod(){
List<someobject> listobjs = this.someService.getobjsList();
if(listobjs != null){
Collections.sort(listobjs, Collections.reverseOrder());
}
return gson.toJson(listobjs);
}
这是我创建的数据表
$("#myTable").dataTable({
"bPaginate": true,
"sAjaxSource": "/theurl",
"sAjaxDataProp": "",
"oLanguage": {
"sEmptyTable": "My Custom Message On Empty Table"
},
"iDisplayLength": 5,
"bSort" : false,
"aoColumnDefs" : [
{"aTargets": [0],
"mRender" : function (data, type, full){
return '<input style="float:left; margin: 0 auto; width: 100%;" class="selected" type="checkbox" value="' + data + '">';
}
}
],
"aoColumns" : [{"mData" : "id"},
{"mData" : "title"},
{"mData" : "description"},
{"mData" : "datecreated"}],
"fnInitComplete": function(oSettings, json){
//I do something here after init.
}
});
当列表中有一些数据时,它工作得非常好,但是当列表为空时,数据表只显示“正在加载”消息并且我收到类型错误:无法处理浏览器控制台错误中的空异常。 我已经通过编写一些 jquery 代码来替换“正在加载...”消息来临时修复它。请指导。
【问题讨论】:
标签: jquery json spring datatables gson