JQuery ServerTable DataTables

In my web application, I use the jQuery DataTables plugin to display data retrieved from the database.

I am currently using client side pagination, but the data in my tables is growing a lot and loading on ASP.NET pages is now getting a little slow. Therefore, I planned to switch to splitting the page to the server.

I know that the DataTables plugin supports it, but when I searched it, I did not find that I did not know how to implement it.

My main doubt is: if I implement pagination on the server side, do I also need to implement ordering or can I delegate it on the client side?

Have you ever experienced this?

NOTE I use Linq to SQL to connect to my database

+5
source share
3 answers

Existing answers may refer to the old version of dataTable, but current versions (I use 1.10+) pass the initial record and length, so any sentence pageNo * pageSizewill give incorrect results.

The first simple “manual” approach

, , , Http Request start length. search[value] order[0][column] order[0][dir] ..

, , :

, HTTP-:

int startRec = 0;
int.TryParse(Request["start"], out startRec);
int pageSize = 10;
int.TryParse(Request["length"], out pageSize);
var search = Request["search[value]"];
var order = Request["order[0][column]"];
var direction = Request["order[0][dir]"];

var query = this._dataStore.Records.AsQueryable();

( ) :

if (!string.IsNullOrWhiteSpace(search))
{
    query = query.Where(x => x.Label.ToLower().Contains(search.ToLower()));
}

:

switch (order)
{
    // My id column
    case "0":
        query = (direction == "desc") ? query.OrderByDescending(x => x.Id) : query.OrderBy(x => x.Id);
        break;
    // My label column
    case "1":
        query = (direction == "desc") ? query.OrderByDescending(x => x.Label) : query.OrderBy(x => x.Label);
        break;
}

, :

query = query.Skip(startRec).Take(pageSize);

.

( "Datatables.net MVC5" )

, /utils . MVC 5 Datatables.net MVC5 nuget.

  • NuGet

  • DataTablesBinder, IDataTablesRequest

.

 public JsonResult Table([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestmodel)
  1. :

.

if (!string.IsNullOrEmpty(requestmodel.Search.Value))
{
    query = query.Where(x => x.CompanyTypeName.Contains(requestmodel.Search.Value) || x.CompanyTypeDescription.Contains(requestmodel.Search.Value));
}
  1. :

.

foreach (var sort in requestmodel.Columns.GetSortedColumns())
{
    switch (sort.Name)
    {
        case "CompanyTypeDescription":
            query = sort.SortDirection == Column.OrderDirection.Ascendant ? query.OrderBy(x => x.CompanyTypeDescription) : query.OrderByDescending(x => x.CompanyTypeDescription);
            break;
        case "CompanyTypeName":
        default:
            query = sort.SortDirection == Column.OrderDirection.Ascendant ? query.OrderBy(x => x.CompanyTypeName) : query.OrderByDescending(x => x.CompanyTypeName);
            break;
    }
}
  1. , Skip Take, :

.

var result = query.Skip(requestmodel.Start).Take(requestmodel.Length).Select(x => new { x.CompanyTypeName, x.CompanyTypeDescription });
  1. , , JSON DataTablesResponse:

.

return Json(new DataTablesResponse(requestmodel.Draw, result, query.Count(), base.RefSureContext.CompanyType.Count()), JsonRequestBehavior.AllowGet);

, , .

.

+8

LINQ to SQL, paginate :

var c = new MyDataContext("your string");

c.Employees.Skip(pageIndex * pageSize).Take(pageSize);

jQuery DataTables, , AJAX ( MVC), ,

, , .

string, :

LINQ OrderBy IEnumerable <T>

+3

, :)

Employees
    .OrderBy(sortColumn + " " + sortOrder)
    .Skip(pageNo * pageSize)
    .Take(pageSize)
    .ToList();
0

All Articles