JQuery DataTables .Net 服务器端分页问题 [英] JQuery DataTables .Net Server Side Pagination Issues

查看:30
本文介绍了JQuery DataTables .Net 服务器端分页问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在为工作中的应用程序修复错误,在该应用程序中,前任开发人员(自从离开以来)没有费心在专门用于列出数据结果的页面上对数据结果进行分页.

I'm working on a bug fix right now for an application at work where the prior developer (since gone) didn't bother to paginate the data results on a page meant specifically for listing out data results.

这当然引起了它的丑陋,因为用户开始在 IE 中看到长时间运行的脚本错误.再加上庞大的数据量,网页几乎毫无用处.

This of course has reared it's ugly head as users are starting to see long running script errors in IE. This, combined with the sheer data volume size, is making web pages nearly useless.

快进到我尝试修复它的过程中,它们进展顺利.该站点是一个 .NET MVC 2 站点,它是使用 DataTables 开发的,用于在客户端上添加搜索/排序/分页功能.我刚刚使用 jqGrid 完成了类似的任务,因此认为这相对简单.除了一个小问题.我一生都无法获得要生成的页面链接.

Fast forward to my attempts to fix it and they've gone pretty well. The site is a .NET MVC 2 site that was developed using DataTables to add search/sort/paging functionality on the client. I'd just completed a similar task using jqGrid so figured this would be relatively straight forward. And it has been except one small problem. I cannot for the life of me get page links to generate.

快速查看结果:

结果知道这个查询有2086条记录:

The results know that there are 2086 records in this query:

但是不会生成分页链接.

But paging links are not generated.

我的操作方法通过

return Json(new
              {
                 param.sEcho,
                 iTotalRecords = totalRecords,
                 iTotalDisplayRecords = filteredContracts.Count(),
                 aaData = result
              },
           JsonRequestBehavior.AllowGet);

哪里

param.sEcho = "1",iTotalRecords = 2086,iTotalDisplayRecords = 25,aaData是要显示的数据的数组结果

param.sEcho = "1", iTotalRecords = 2086, iTotalDisplayRecords = 25, and aaData is the array result of data to display

要彻底,他是数据表初始化语句:

To be thorough, he's the datatable initialize statement:

    $("#tblToDoItems").dataTable({
        'bServerSide': true,
        'bProcessing': true,
        'sAjaxSource': '/Home/GetContractList',
        "bJQueryUI": true,
        "bAutoWidth": false,
        "bPaginate": true,
        "sPaginationType": "full_numbers",
        "iDisplayLength": 25,
    /* make the first and last columns not sortable */
        "aoColumnDefs": [
            { "bSortable": false, "aTargets": [0, -1] }
        ]
    });

我是否遗漏了一些会阻止 DataTables 通过服务器端数据检索正确生成分页的设置?

Am I missing some setting that would prevent DataTables from properly generating pagination via server side data retrieval?

推荐答案

你的iTotalDisplayRecords等于25,所以datatables认为server端只有25个合约,第二页不需要,因为已经全部显示了在当前页面.这是常见的错误 - 如果您查看 JQuery MVC 教程部分服务器端分页你会看到有三个数字:

Your iTotalDisplayRecords is equal to 25, so datatables think that there are only 25 contracts on the server side and second page is not needed because all of them are already shown on the current page. This is comon mistake - if you take a look at the JQuery MVC tutorial section Implementation of server-side paging you will see that there are three numbers:

  1. iTotalRecords = allCompanies.Count() 表示数据库中的所有条目(在您的情况下为 2086)
  2. iTotalDisplayRecords = filteredCompanies.Count() 表示与当前搜索条件匹配的记录数.如果你没有使用过滤,这个数字应该与 iTotalRecords 2086 相同,但在你的情况下它是 25.
  3. result.Count - 这是 25.这个数字没有在 JSON 响应中传递,因为 DataTables 已经知道每页应该有 25 条记录.

如果将 all.Count 而不是 result.Count 放入 iTotalDisplayRecords 数据表中,将显示分页.iTotalDisplayRecords 和 iTotalRecords 用于显示消息显示 1 到 25 个 iTotalDisplayRecords(总共 iTotalRecords)"

If you put all.Count insteadof the result.Count into the iTotalDisplayRecords DataTables will show paging. iTotalDisplayRecords and iTotalRecords are used to show message "Showing 1 to 25 of iTotalDisplayRecords (iTotalRecords in total)"

如果 iTotalDisplayRecords 等于 25,DataTables 将显示信息Showing 1 to 25 of 25 (iTotalRecords in total)",并假设没有第 2 页;因此,分页将被禁用,如您的示例所示.

If iTotalDisplayRecords is equal to 25, DataTables will show message "Showing 1 to 25 of 25 (iTotalRecords in total)", and assume that there is no page 2; hence, paging will be disabled, as in your example.

乔万

这篇关于JQuery DataTables .Net 服务器端分页问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆