我是DataTables jquery插件的新手。在发现IE 8出现Javascript性能问题后,我决定改变使用DataTables进行服务器端处理的方式。当我的JSP加载时(我正在使用Spring 3),我收到此错误消息:
DataTables warning (table id = 'results_table'): Requested unknown parameter '0' from the data source for row 0
我用Google搜索并发现该错误消息的许多原因归结为格式错误的JSON,因此我找到了一种方法从我的Spring 3控制器函数输出我的JSON来查看它生成的JSON并将我的代码更改为让它非常接近DataTables网站应该看起来的样子。
仍然没有快乐,仍然收到错误消息。
我为DataTables找到的服务器端处理示例没有包含用于指定客户端使用的列的代码,所以我假设我不需要它。我吗?
以下是我的results.jsp的相关部分:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ACME: search results in a nice DataTables.net Plugin</title>
</head>
<body>
<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css" />
<script language = "JavaScript" type = "text/javascript" src = "../nsd/js/jquery-1.7.js"></script>
<script language = "JavaScript" type = "text/javascript" src = "../nsd/js/jquery.dataTables.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#results_table').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sScrollX": "600px",
"sServerMethod": "POST",
"sAjaxSource": "/acme/resultstable",
} );
} );
</script>
<form id="command" name="f" action="employee" method="post">
<div id = "results">
<table id = "results_table">
<thead>
<tr>
<th> </th>
<th>ID</th>
<th>NO_PRINT</th>
<th>Full Name</th>
<th>Email Address</th>
<th>Phone Number</th>
<th>Organization</th>
<th>Organization Code</th>
<th>Position</th>
<th>Employee Type</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
以下是我发送给它的JSON响应:
{
"sEcho" : 1,
"iTotalRecords" : 1,
"iTotalDisplayRecords" : 1,
"aaData" : [ {
"person_id" : "888888",
"ID" : "999999",
"no_print" : " ",
"fullname" : "Obama, Willard",
"email_address" : "<a href = \"mailto:barry@whitehouse.gov\">barry@whitehouse.gov</a>",
"current_phone_number" : "303-867-5309",
"title" : " ",
"office" : " ",
"position" : "Contractor",
"empl_code" : "CONT"
} ]
}
这是我用来通过Jackson发送JSON响应的Spring控制器功能。这包括输出我的JSON的代码,所以我可以看到它的样子。它输出到stdout的JSON和我发送回DataTables的内容会有所不同吗?
@RequestMapping(value = "/resultstable", method = RequestMethod.POST)
public @ResponseBody LinkedHashMap resultstable(ModelMap model,
HttpSession session,
@RequestParam (required=true) int sEcho,
@RequestParam (required=true) int iDisplayStart,
@RequestParam (required=true) int iDisplayLength,
@RequestParam (required=true) int iColumns,
@RequestParam (required=true) int iSortCol_0,
@RequestParam (required=false)String sSortDir_0,
@RequestParam (required=true) String sSearch ) {
/*
**********************************************************************
** These come from the DataTables.net Jquery plugin on results.jsp
**********************************************************************
** sEcho, - just send it back, used by DataTables for synching
** iDisplayStart - index of the record to start with, ie 3 for the 3rd of 100 records
** iDisplayLength - number of records to send back starting with iDisplayStart
** iColumns - number of columns to be displayed in the table
** iSortCol_0 - the number of thee column to be sorted on
** sSortDir_0 - direction of sorting: asc or desc
** sSearch - from the search box, filter results further on this term
**********************************************************************
*/
String nextView = "results";
String usertype = (String)session.getAttribute("usertype");
Search search = new Search(usertype);
List<LinkedHashMap> records = null;
String results = null;
int number_of_records = (Integer)session.getAttribute("number_of_records_found");
ResultsView rv = new ResultsView();
ResultsScreenTableHolder rstrh = null;
SearchScreenDataHolder ssdh2 = (SearchScreenDataHolder)session.getAttribute("search_screen_data_holder");
ObjectMapper mapper = new ObjectMapper();
logger.debug("started");
logger.debug("sEcho, == " + sEcho );
logger.debug("iDisplayStart == " + iDisplayStart );
logger.debug("iDisplayLength == " + iDisplayLength );
logger.debug("iColumns == " + iColumns );
logger.debug("iSortCol_0 == " + iSortCol_0 );
logger.debug("sSortDir_0 == " + sSortDir_0 );
logger.debug("sSearch == " + sSearch );
try {
records = search.searchForAnEmployee(ssdh2,usertype,sSearch,"asc",
iSortCol_0,iDisplayStart,
iDisplayLength);
LinkedHashMap lhm= new java.util.LinkedHashMap();
lhm.put("sEcho", sEcho);
lhm.put("iTotalRecords",number_of_records);
lhm.put("iTotalDisplayRecords",9);
lhm.put("aaData",records);
// convert user object to json string, and save to a file
mapper.writeValue(new File("c:\\Downloads\\rstrh.json.txt"), lhm);
// display to console
logger.debug("My JSON: " + mapper.defaultPrettyPrintingWriter().writeValueAsString(lhm));
}
catch (Exception e) {
logger.debug("\n",e);
}
return lhm;
}// end function
答案 0 :(得分:37)
我也遇到了同样的警告,但原因不同了。我的数据中有 null 值。 JSON格式是正确的,但DataTables不知道有显示 nulls 的默认规则。解决方案是使用 sDefaultContent 属性。
示例aaData:
aaData: [
{ "Field1": "Foo", "Field2":null },
{ "Field1": "Bar", "Field2":null },
]
然后在aoColumns上,您可以按如下方式使用该属性:
aoColumns: [
{ "mData": "Field1", sDefaultContent: "n/a" },
{ "mData": "Field2", sDefaultContent: "" }
]
这不是您当前的问题,但您将来可能会遇到此问题。
希望这有用。
答案 1 :(得分:13)
答案 2 :(得分:2)
sDefaultContent选项仅阻止显示警告框。数据表将找不到显示空值的规则。
更改表格中的空值将消除此警告..
答案 3 :(得分:1)
如果它有助于任何人我有类似的错误,因为我的mRender功能名称有句号:
My.Namespace.MyFunction(data, row);
这一行:
var a = _fnSplitObjNotation( src );
将其拆分为单独的对象,这显然会产生错误。
使用
My_Namespace_MyFunction(data, row);
另外,在传递字符串函数名而不是JavaScript函数对象时,我注意到了这个错误。
答案 4 :(得分:1)
为避免此错误,您的表号为&#34;&#34;列应该等于返回数据列(以数字表示),在上面的问题中它是aaData。
1/0
这是从服务器端语言返回数据的正确格式。 我以同样的方式解决了我的问题。