Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using jquery 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 mvc (C#) application.

My application has thousands of records to show in the table as list. I am using jquery datatable to enable paging, filtering and search.

Is there any good reference/articles for jquery datatable server-side processing to use with asp.net mvc (C#)?

like image 708
Prasad Avatar asked Jul 07 '10 10:07

Prasad


1 Answers

Hi this link may be helpful to you...

http://www.dotnetawesome.com/2015/11/jquery-datatable-server-side-pagination-sorting.html

Here the article about jQuery Datatable server side pagination and sorting in ASP.NET MVC , explained step by step in asp.net mvc (C#) as server dside I will refer this article [jQuery Datatable server side pagination and sorting in ASP.NET MVC

jQuery code for setup jQuery Datables

<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)

[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);
        }
    }
like image 62
Sourav Mondal Avatar answered Sep 17 '22 13:09

Sourav Mondal