使用jquery datatable进行分页、过滤和搜索的服务器端处理[已关闭]

amrnrhlw  于 2022-12-18  发布在  jQuery
关注(0)|答案(5)|浏览(176)

**已关闭。**此问题不符合Stack Overflow guidelines。当前不接受答案。

我们不允许问题寻求有关书籍、工具、软件库等的推荐。你可以编辑问题,以便可以使用事实和引用来回答问题。
昨天关门了。
Improve this question
我需要为我的www.example.com mvc(C#)应用程序使用jquery数据表服务器端处理(http://datatables.net)asp.net。
我的应用程序有数千条记录要在表中显示为列表。我正在使用jquery datatable来启用分页、过滤和搜索。
有没有关于jquery数据表服务器端处理的好的参考/文章可以与asp.net mvc(C#)一起使用?

ccrfmcuu

ccrfmcuu1#

此链接可能对您有帮助...
http://www.dotnetawesome.com/2015/11/jquery-datatable-server-side-pagination-sorting.html
NET MVC中jQuery Datatable服务器端分页和排序的文章,在asp.net mvc(C#)中作为服务器端逐步讲解,我将参考本文[jQuery Datatable服务器端分页和排序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);
        }
    }
azpvetkf

azpvetkf2#

https://github.com/johannes-brunner/DataTables-ASP.NET-MVC
这是一个示例项目,您可以下载它,并通过调试来感受DataTables如何与. NETMVC一起工作。它帮助我找到了这个主题的立足点。

pcrecxhr

pcrecxhr3#

请遵循此代码它非常简单

<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;  
    }
omqzjyyz

omqzjyyz4#

在你提到的官方网站上,有一个完整的参考Here
我希望它能有所帮助。

相关问题