加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – JQuery DataTables服务器端分页

发布时间:2020-12-15 08:48:46 所属栏目:百科 来源:网络整理
导读:在我的网络应用程序中,我使用 JQuery DataTables插件来显示从数据库中检索的数据. 我目前正在使用客户端分页,但我的表中的数据正在增长很多,并且在ASP.NET页面中加载现在变得有点慢.所以我打算切换到服务器端分页. 我知道DataTables插件支持它,但搜索我没有
在我的网络应用程序中,我使用 JQuery DataTables插件来显示从数据库中检索的数据.

我目前正在使用客户端分页,但我的表中的数据正在增长很多,并且在ASP.NET页面中加载现在变得有点慢.所以我打算切换到服务器端分页.

我知道DataTables插件支持它,但搜索我没有发现没有明确实现它.

我的主要疑问是:如果我在服务器端实现分页,我还必须实现排序,或者我可以将它委托给客户端?

你有没有经历过这个?

注意我使用Linq to SQL连接到我的数据库

解决方法

现有的答案可能适用于旧版本的dataTable,但是当前版本(我使用的是1.10)会传递开始记录和长度,因此任何建议pageNo * pageSize都会给出不正确的结果.

第一个简单的“手动”方法

对于我想要做的事情,接受的答案也非常复杂,经过一些调试后,我发现页面大小和开始记录只是作为名为start和length的Http Request值传递.文本搜索作为search [value]传递.排序顺序在名为order [0] [column]的成员中传递,排序方向按顺序[0] [dir]等传递.

我用来排序和过滤的基本代码如下所示:

从HTTP Request对象获取分页,排序和过滤值:

int startRec = 0;
int.TryParse(Request["start"],out startRec);
int pageSize = 10;
int.TryParse(Request["length"],out pageSize);
var search = Request["search[value]"];
var order = Request["order[0][column]"];
var direction = Request["order[0][dir]"];

var query = this._dataStore.Records.AsQueryable();

首先应用(不区分大小写)搜索:

if (!string.IsNullOrWhiteSpace(search))
{
    query = query.Where(x => x.Label.ToLower().Contains(search.ToLower()));
}

然后应用任何排序:

switch (order)
{
    // My id column
    case "0":
        query = (direction == "desc") ? query.OrderByDescending(x => x.Id) : query.OrderBy(x => x.Id);
        break;
    // My label column
    case "1":
        query = (direction == "desc") ? query.OrderByDescending(x => x.Label) : query.OrderBy(x => x.Label);
        break;
}

最后应用分页:

query = query.Skip(startRec).Take(pageSize);

正确的记录现在可以返回了.

更新(使用“Datatables.net for MVC5”)

一旦我理解了服务器端dataTable的基础知识,就该开始寻找现有的插件/工具来简化这段代码了.到目前为止,对于MVC 5,我发现的最合适的是Datatables.net for MVC5 nuget包.

>安装NuGet包
>更改控制器操作以使用DataTablesBinder提供IDataTablesRequest接口

例如

public JsonResult Table([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestmodel)

>首先应用任何搜索过滤器:

例如

if (!string.IsNullOrEmpty(requestmodel.Search.Value))
{
    query = query.Where(x => x.CompanyTypeName.Contains(requestmodel.Search.Value) || x.CompanyTypeDescription.Contains(requestmodel.Search.Value));
}

>应用任何排序:

例如

foreach (var sort in requestmodel.Columns.GetSortedColumns())
{
    switch (sort.Name)
    {
        case "CompanyTypeDescription":
            query = sort.SortDirection == Column.OrderDirection.Ascendant ? query.OrderBy(x => x.CompanyTypeDescription) : query.OrderByDescending(x => x.CompanyTypeDescription);
            break;
        case "CompanyTypeName":
        default:
            query = sort.SortDirection == Column.OrderDirection.Ascendant ? query.OrderBy(x => x.CompanyTypeName) : query.OrderByDescending(x => x.CompanyTypeName);
            break;
    }
}

>然后像以前一样使用Skip and Take应用分页:

例如

var result = query.Skip(requestmodel.Start).Take(requestmodel.Length).Select(x => new { x.CompanyTypeName,x.CompanyTypeDescription });

>最后使用DataTablesResponse对象返回JSON结果:

例如

return Json(new DataTablesResponse(requestmodel.Draw,result,query.Count(),base.RefSureContext.CompanyType.Count()),JsonRequestBehavior.AllowGet);

这简化了所有搜索,排序和排序.分页成一个很容易重复的模式.

documentation for the addin is here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读