Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Created August 24, 2015 20:59
Show Gist options
  • Select an option

  • Save christopherbauer/282363dd98e92a64b122 to your computer and use it in GitHub Desktop.

Select an option

Save christopherbauer/282363dd98e92a64b122 to your computer and use it in GitHub Desktop.
DataTables Api Stuff
public class column
{
/// <summary>
/// Columns datasource
/// </summary>
public string data { get; set; }
/// <summary>
/// Columns name
/// </summary>
public string name { get; set; }
public bool searchable { get; set; }
public bool orderable { get; set; }
/// <summary>
/// Search value which applies to this column
/// </summary>
public search search { get; set; }
}
public class DataTablesController : ApiController
{
private readonly ICustomerRepository _customerRepository;
public DataTablesController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
public DataTableResult Get([FromUri]DataTableParameters parameters)
{
IList<CustomerModel> customers;
customers = _customerRepository.GetCustomers();
var pageCustomers = customers.Skip(parameters.start).Take(parameters.length).ToArray();
return new DataTableResult
{
draw = Convert.ToInt32(parameters.draw),
recordsTotal = customers.Count,
recordsFiltered = customers.Count(), //not doing filtering yet
data = pageCustomers,
error = ""
};
}
}
public class DataTableParameters
{
/// <summary>
/// Draw counter which is used by data tables to ensure ajax requests are drawn in sequence
/// </summary>
public int draw { get; set; }
/// <summary>
/// Paging first record indicator, the start point of the current data
/// </summary>
public int start { get; set; }
/// <summary>
/// Number of records the table is set to display for the current draw
/// </summary>
public int length { get; set; }
/// <summary>
/// Columns to which ordering has been applied
/// </summary>
public order[] order { get; set; }
/// <summary>
/// List of columns and data sources
/// </summary>
public column[] columns { get; set; }
/// <summary>
/// Global search value, applied to all columns which are searchable: true
/// </summary>
public search search { get; set; }
}
public class DataTableResult
{
/// <summary>
/// Response to the draw from DataTablesParameters, always cast the parameter value to int before returning it to prevent XSS
/// </summary>
public int draw { get; set; }
/// <summary>
/// Total records prior to filtering (in DB)
/// </summary>
public int recordsTotal { get; set; }
/// <summary>
/// Total records after filtering (not just the number returned from this page of data)
/// </summary>
public int recordsFiltered { get; set; }
/// <summary>
/// Data to display in table
/// </summary>
public object[] data { get; set; }
/// <summary>
/// Optional, in the event an error was detected but was found to be recoverable
/// </summary>
public string error { get; set; }
}
public class order
{
/// <summary>
/// Column index to order
/// </summary>
public int column { get; set; }
/// <summary>
/// Ordering direction
/// </summary>
public string dir { get; set; }
}
public class search
{
public string value { get; set; }
public bool regex { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment