Skip to content Skip to sidebar Skip to footer

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. My Web-Site has thousands of records to show in the table as list. I

Solution 1:

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

Set up your .net endpoint with a signature like so:

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

IEnumerabletheDataToReturn= GetMyDataFromDB();
DataTablesReturnDatadtReturn=newDataTablesReturnData()
{
  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).

Solution 2:

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

            //SORTif (!(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);
        }
    }

Post a Comment for "Datatable For Server Side Processing With Paging, Filtering And Search"