JQuery的数据表.NET服务器端分页问题分页、服务器端、数据表、问题

2023-09-02 10:38:05 作者:浮夸¢

我工作的一个bug修复,现在在工作的一个应用程序,事先开发(自去了)没有刻意去专门分页上市出的数据结果意味着页面上的数据结果。

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.

快进到我试图修复它,他们已经走了pretty的好。该网站是使用数据表中添加客户端的搜索/排序/分页功能开发的.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.

有一个快速的结果查看:

A quick results view:

结果知道有此查询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

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

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] }
        ]
    });

我失去了一些设置,将起价通过服务器端的数据检索正确生成分页p $ pvent数据表?

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

推荐答案

您iTotalDisplayRecords等于25,那么数据表认为,因为所有的人都已经表明,只有25不需要在服务器端和第二页的合同在当前页上。 这是COMON错误 - 如果你看一下在 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:

iTotalRecords = allCompanies.Count()重新presenting数据库中的所有条目(在你的案件2086) iTotalDisplayRecords = filteredCompanies.Count()重新presenting的匹配当前检索条件的记录的数量。如果您没有使用过滤这个数字应该是一样的iTotalRecords 2086但在yourcase是25。 result.Count - 这是25这个号码没有在JSON响应通过,因为数据表已经知道,应该有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,数据表将显示消息显示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.

约万