【发布时间】:2015-04-13 16:30:22
【问题描述】:
您好,我们目前有一个 MVC4 应用程序,它将带有缩略图的大量记录带入一个 jQuery Datatables 驱动的视图中。当它执行 GET 加载所有缩略图时,这当然加载非常缓慢。为了解决这个问题,我试图让它使用服务器端处理并且一次只能得到 10 个结果。
为此,我遵循了 Datatables 文档并像这样更改了 Javascript:
<script>
$(document).ready(function () {
//Create an array with the values of all the checkboxes in a column */
$.fn.dataTable.ext.order['dom-checkbox'] = function (settings, col) {
return this.api().column(col, { order: 'index' }).nodes().map(function (td, i) {
return $('input', td).prop('checked') ? '1' : '0';
});
}
/* Initialise the table with the required column ordering data types */
$(document).ready(function () {
$('.passes').dataTable({
"serverSide": true,
"processing": true,
"ajax": "/Pass/Datatable",
"columns": [
{ "orderDataType": "dom-text-numeric "},
{ "orderDataType": "dom-text-numeric" },
{ "orderDataType": "dom-text-numeric" },
{ "orderDataType": "dom-text-numeric" },
{ "orderDataType": "dom-text-numeric" },
{ "orderDataType": "dom-checkbox" },
{ "orderDataType": "dom-text-numeric" }
],
"order": [[5, "desc"]]
});
});
});
这是从我返回 JSON 结果的控制器中的方法获取 AJAX 源。我从我的 SQL 表中获取信息,然后使用我在 Github 上找到的 Datatable 解析器来解析我进入 JSON 的信息,如下所示:
public JsonResult Datatable(int passesPerPage, HttpRequest Request)
{
//default number of passes will be set to 10
passesPerPage = 10;
var passes = db.Cards.OrderBy(c => c.Vendor.Name); // fetch the data from data model
passes.Take(passesPerPage); //Takes the number of passes requested per page so as not to overload the server
var parser = new DataTablesParser<Card>(Request, passes); //pass the parser the data to parse it into JSON
return Json(parser.Parse()); // have the parser parse the request parameters and return the Json Result
}
当我测试网页时,它仍然会加载所有记录并且根本没有分页功能。我想知道我到底做错了什么,因为除了意外行为之外,我没有收到任何类型的错误。如果我能得到关于如何调试它的任何帮助,或者如果有人能看到我做错了什么,我将不胜感激。
【问题讨论】:
-
当我检查服务器错误日志时,这是我从数据表返回的 ASP.NET 错误。值不能为空。参数名称:String 导致错误的行是这一行: 第 46 行:return Json(parser.Parse()); // 让解析器解析请求参数并返回 Json Result
-
如果您收到 ajax 错误,那么可能是因为 ajax 返回的结果不是您的插件所期望的格式。您需要深入研究 dataTables tutotial 以了解如何使用它所期望的格式进行数据...
标签: javascript jquery json asp.net-mvc-4 datatable