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

查看:275
本文介绍了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.

快速解决我的尝试,并且它们已经很好了。该网站是使用DataTables开发的.NET MVC 2站点,以在客户端上添加搜索/排序/分页功能。我刚刚完成了一个类似的任务,使用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:

但不会生成页面链接。

我的动作方法是通过

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

要成为彻底,他是datatable初始化语句:

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通过服务器端数据检索正确生成分页的设置? p>

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

推荐答案

您的iTotalDisplayRecords等于25,因此数据表认为服务器端只有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 DataTables将显示分页。 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将显示消息显示1到25的25(总共iTotalRecords),并假设没有第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.

Jovan

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

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