Tôi mới sử dụng plugin jquery DataTables. Sau khi phát hiện ra rằng IE 8 có vấn đề về hiệu suất với Javascript, tôi quyết định thay đổi cách tôi sử dụng DataTables để xử lý phía máy chủ. Tôi nhận được thông báo lỗi này khi tải JSP của tôi (Tôi đang sử dụng Spring 3):Lỗi DataTables: "Đã yêu cầu thông số không xác định"
DataTables warning (table id = 'results_table'): Requested unknown parameter '0' from the data source for row 0
Tôi đã google xung quanh và phát hiện ra rằng nhiều nguyên nhân đó thông báo lỗi đi xuống JSON bị thay đổi vì vậy tôi tìm thấy một cách để xuất JSON của tôi từ hàm điều khiển Spring 3 của tôi để xem JSON mà nó tạo ra và tôi đã thay đổi mã của mình để làm cho nó khá gần với trang DataTables nói nó trông giống như thế.
Vẫn không có niềm vui, vẫn nhận được thông báo lỗi đó.
Ví dụ về xử lý phía máy chủ mà tôi tìm thấy cho DataTables không bao gồm mã để chỉ định các cột được sử dụng ở phía máy khách, vì vậy tôi cho rằng tôi không cần nó. Tôi có?
Dưới đây là những phần liên quan của results.jsp tôi:
<!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>
Đây là phản ứng JSON Tôi đã gửi cho nó:
{
"sEcho" : 1,
"iTotalRecords" : 1,
"iTotalDisplayRecords" : 1,
"aaData" : [ {
"person_id" : "888888",
"ID" : "999999",
"no_print" : " ",
"fullname" : "Obama, Willard",
"email_address" : "<a href = \"mailto:[email protected]\">[email protected]</a>",
"current_phone_number" : "303-867-5309",
"title" : " ",
"office" : " ",
"position" : "Contractor",
"empl_code" : "CONT"
} ]
}
Đây là mùa xuân của tôi chức năng điều khiển Tôi đang sử dụng để gửi phản hồi JSON qua Jackson. Điều này bao gồm mã để xuất JSON của tôi để tôi có thể thấy nó trông như thế nào. JSON có thể xuất ra để stdout và những gì tôi gửi lại cho DataTables khác nhau không?
@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
Thông tin tại đây có thể giúp bạn .. http://stackoverflow.com/a/12985883/661584 Hy vọng bạn sẽ làm việc đó. Chrs – MemeDeveloper