[英]Datatable for server side processing with paging, filtering and search
I need to use the jquery datatable server-side processing ( http://datatables.net ) for my asp.net (C#) Web-Site. 我需要对asp.net(C#)网站使用jquery数据表服务器端处理( http://datatables.net )。
My Web-Site has thousands of records to show in the table as list. 我的网站有成千上万的记录以列表形式显示在表中。 I am using jquery datatable server-side to enable paging, filtering and search. 我在服务器端使用jquery datatable启用分页,过滤和搜索。
Is there any good reference/articles for jquery datatable server-side processing to use with asp.net (C#)? 对于与asp.net(C#)一起使用的jquery数据表服务器端处理,是否有任何好的参考资料/文章?
The parameters sent to your controller and the values you need to return are defined in the docs here http://datatables.net/manual/server-side 发送到控制器的参数和需要返回的值在此处http://datatables.net/manual/server-side的文档中定义
Set up your .net endpoint with a signature like so: 使用如下签名设置您的.net端点:
public JsonResult GetResultDtData(
int draw,
int start,
int length,
Dictionary<string, string> search,
List<Dictionary<string,string>> order,
List<Dictionary<string, string>> )
Then you can use those parameters to decide on what data you need to send back. 然后,您可以使用这些参数来确定需要发送回哪些数据。 Paging can be done with Skip() and Take() 可以使用Skip()和Take()完成分页
IEnumerable theDataToReturn = GetMyDataFromDB();
DataTablesReturnData dtReturn = new DataTablesReturnData()
{
draw = draw,
recordsTotal = theDataToReturn.Count,
recordsFiltered = theDataToReturn.Count,
data = getData().Skip(start).Take(length).ToList()
};
return dtReturn;
Filtering and ordering are a bit more complex, but all the info you need is stored in the initial parameters (search, order and columns). 过滤和排序比较复杂,但是所需的所有信息都存储在初始参数(搜索,排序和列)中。
I will refer this article jQuery Datatable server side pagination and sorting in ASP.NET MVC 我将在ASP.NET MVC中引用本文jQuery Datatable服务器端分页和排序
jQuery code for setup jQuery Datables 设置jQuery Datables的jQuery代码
<script>
$(document).ready(function () {
$("#myTable").DataTable({
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": false, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"ajax": {
"url": "/home/LoadData",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "ContactName", "name": "ContactName", "autoWidth": true },
{ "data": "CompanyName", "name": "CompanyName", "autoWidth": true },
{ "data": "Phone", "name": "Phone", "autoWidth": true },
{ "data": "Country", "name": "Country", "autoWidth": true },
{ "data": "City", "name": "City", "autoWidth": true },
{ "data": "PostalCode", "name": "PostalCode", "autoWidth": true }
]
});
});
</script>
ASP.NET C# Code (MVC) ASP.NET C#代码(MVC)
[HttpPost]
public ActionResult LoadData()
{
var draw = Request.Form.GetValues("draw").FirstOrDefault();
var start = Request.Form.GetValues("start").FirstOrDefault();
var length = Request.Form.GetValues("length").FirstOrDefault();
//Find Order Column
var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
int pageSize = length != null? Convert.ToInt32(length) : 0;
int skip = start != null ? Convert.ToInt32(start) : 0;
int recordsTotal = 0;
using (MyDatatableEntities dc = new MyDatatableEntities())
{
var v = (from a in dc.Customers select a);
//SORT
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{
v = v.OrderBy(sortColumn + " " + sortColumnDir);
}
recordsTotal = v.Count();
var data = v.Skip(skip).Take(pageSize).ToList();
return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data }, JsonRequestBehavior.AllowGet);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.