Skip to content

Instantly share code, notes, and snippets.

@ryanohs
Created October 21, 2015 14:04
Show Gist options
  • Save ryanohs/55d65d35feac047f0371 to your computer and use it in GitHub Desktop.
Save ryanohs/55d65d35feac047f0371 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using PagedList;
public class DataTablesSearch
{
public string Value { get; set; }
public bool Regex { get; set; }
}
public class DataTablesOrder
{
public int Column { get; set; }
public string Dir { get; set; }
}
public class DataTablesColumn
{
public string Data { get; set; }
public string Name { get; set; }
public bool Searchable { get; set; }
public bool Orderable { get; set; }
public DataTablesSearch Search { get; set; }
}
public class DataTablesParam
{
public int Draw { get; set; }
public int Start { get; set; }
public int Length { get; set; }
public DataTablesSearch Search { get; set; }
public List<DataTablesOrder> Order { get; set; }
public DataTablesColumn[] Columns { get; set; }
public int PageNumber
{
get
{
if (Length == 0)
{
return 1;
}
return Start/Length + 1;
}
}
public int PageSize
{
get { return Length; }
}
public PagingCriteria ToPagingCriteria()
{
return new PagingCriteria()
{
Page = PageNumber,
PageSize = PageSize,
SortFields = Order.Select(x => new SortField(Columns[x.Column].Name ?? Columns[x.Column].Data, x.Dir)).ToArray()
};
}
}
public static class DataTablesExtensions
{
public static JsonResult ToDataTableResult<T>(this IPagedList<T> results, int draw)
{
return new JsonResult()
{
Data = new
{
recordsTotal = results.TotalItemCount,
recordsFiltered = results.TotalItemCount,
draw,
data = results.Cast<object>().ToArray()
}
};
}
}
public static class PagedListExtensions
{
/// <summary>
/// Map the items in a paged list
/// </summary>
public static IPagedList<TResult> Select<TSource, TResult>(this IPagedList<TSource> source,
Func<TSource, TResult> selector)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (selector == null)
{
throw new ArgumentNullException("selector");
}
return new StaticPagedList<TResult>(source.AsEnumerable().Select(selector), source.PageNumber, source.PageSize,
source.TotalItemCount);
}
/// <summary>
/// This function assumes we can trust the column names coming from the client. It would be more secure to reference a separate copy of
/// sortable column names on the server side, but there is a maintenance overhead as well.
/// </summary>
public static IPagedList<TSource> TrustedSortToPagedList<TSource>(this IEnumerable<TSource> source, PagingCriteria pagingCriteria)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
return source.AsQueryable().TrustedSortToPagedList(pagingCriteria);
}
/// <summary>
/// This function assumes we can trust the column names coming from the client. It would be more secure to reference a separate copy of
/// sortable column names on the server side, but there is a maintenance overhead as well.
/// </summary>
public static IPagedList<TSource> TrustedSortToPagedList<TSource>(this IQueryable<TSource> source, PagingCriteria pagingCriteria)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (pagingCriteria == null)
{
throw new ArgumentNullException("pagingCriteria");
}
var sortedSource = source.SortBySortField(pagingCriteria.SortFields.ToArray());
if (pagingCriteria.IsNotPaged)
{
var count = sortedSource.Count();
var pageSize = count == 0 ? 1 : count;
return sortedSource.ToPagedList(1, pageSize);
}
return sortedSource.ToPagedList(pagingCriteria.Page, pagingCriteria.PageSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment