我需要为我的 asp.net mvc (C#) 应用程序使用 jquery 数据表服务器端处理 ( http://datatables.net )。
我的应用程序有数千条记录以列表形式显示在表中。我正在使用 jquery 数据表来启用分页、过滤和搜索。
是否有任何关于 jquery 数据表服务器端处理与 asp.net mvc (C#) 一起使用的好的参考/文章?
我需要为我的 asp.net mvc (C#) 应用程序使用 jquery 数据表服务器端处理 ( http://datatables.net )。
我的应用程序有数千条记录以列表形式显示在表中。我正在使用 jquery 数据表来启用分页、过滤和搜索。
是否有任何关于 jquery 数据表服务器端处理与 asp.net mvc (C#) 一起使用的好的参考/文章?
您好,此链接可能对您有帮助...
http://www.dotnetawesome.com/2015/11/jquery-datatable-server-side-pagination-sorting.html
这里是关于 jQuery Datatable server side pagination and sort in ASP.NET MVC 的文章,在 asp.net mvc (C#) as server dside 中一步一步解释我会参考这篇文章 [jQuery Datatable server side pagination and sort in ASP.NET MVC
用于设置 jQuery 数据表的 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# 代码 (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);
}
}
https://github.com/johannes-brunner/DataTables-ASP.NET-MVC
这是一个示例项目,您可以下载它并通过调试了解 DataTables 如何与 .NET MVC 一起使用。它帮助我在这个话题中找到立足点。
请遵循此代码非常简单
<script type="text/javascript">
$(document).ready(function () {
$("#AllUsers").DataTable({
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": true, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"ajax": {
"url": "@Url.Content("~/Users/GetAllUsers")",
"type": "POST",
"datatype": "json"
},
"columns": [
{ data: 'UserID', name: 'UserID', "autoWidth": true },
{ data: 'Name', name: 'Name', "autoWidth": true },
{ data: 'Email', name: 'Email', "autoWidth": true },
{ data: 'UserRole', name: 'UserRole', "autoWidth": true },
{ data: 'Status', name: 'Status', "autoWidth": true },
{
data: '', name: '', "autoWidth": true, "orderable": false, mRender: function (data, colo, row) {
return "<i class='fa fa-trash' style='cursor:pointer'></i>";
}
}
]
});
});
这是控制器方法
public JsonResult GetAllUsers()
{
JsonResult result = new JsonResult();
try
{
// Initialization.
string search = Request.Form.GetValues("search[value]")[0];
string draw = Request.Form.GetValues("draw")[0];
string order = Request.Form.GetValues("order[0][column]")[0];
string orderDir = Request.Form.GetValues("order[0][dir]")[0];
int startRec = Convert.ToInt32(Request.Form.GetValues("start")[0]);
int pageSize = Convert.ToInt32(Request.Form.GetValues("length")[0]);
// Loading.
List<User> data = _userReps.AllUsers().ToList();
// Total record count.
int totalRecords = data.Count;
// Verification.
if (!string.IsNullOrEmpty(search) &&
!string.IsNullOrWhiteSpace(search))
{
// Apply search
data = data.Where(p => p.FirstName.ToString().ToLower().Contains(search.ToLower()) ||
p.LastName.ToLower().Contains(search.ToLower()) ||
p.EmailID.ToString().ToLower().Contains(search.ToLower()) ||
p.UserRole.UserRoleName.ToLower().Contains(search.ToLower()) ||
p.UserStatus.Name.ToLower().Contains(search.ToLower())
).ToList();
}
// Sorting.
if (!(string.IsNullOrEmpty(order) && string.IsNullOrEmpty(orderDir)))
{
data = data.OrderBy(order + " " + orderDir).ToList();
}
int recFilter = data.Count;
data = data.Skip(startRec).Take(pageSize).ToList();
var modifiedData = data.Select(d =>
new {
UserID= d.UserID,
Name= d.FirstName + " "+ d.LastName,
Email= d.EmailID,
Status= d.UserStatus.Name,
UserRole= d.UserRole.UserRoleName
}
);
// Loading drop down lists.
result = this.Json(new
{
draw = Convert.ToInt32(draw),
recordsTotal = totalRecords,
recordsFiltered = recFilter,
data = modifiedData
}, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
// Info
Console.Write(ex);
}
// Return info.
return result;
}
我会建议ALMMa 数据表
在你提到的官方网站上,有一个完整的参考这里。
我希望它能有所帮助。