using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace jQuery.DataTables.Mvc
{
///
/// Represents the jQuery DataTables request that is sent for server
/// side processing.
/// http://datatables.net/usage/server-side
///
public class JQueryDataTablesModel
{
///
/// Gets or sets the information for DataTables to use for rendering.
///
public int sEcho { get; set; }
///
/// Gets or sets the display start point.
///
public int iDisplayStart { get; set; }
///
/// Gets or sets the number of records to display.
///
public int iDisplayLength { get; set; }
///
/// Gets or sets the Global search field.
///
public string sSearch { get; set; }
///
/// Gets or sets if the Global search is regex or not.
///
public bool bRegex { get; set; }
///
/// Gets or sets the number of columns being display (useful for getting individual column search info).
///
public int iColumns { get; set; }
///
/// Gets or sets indicator for if a column is flagged as sortable or not on the client-side.
///
public ReadOnlyCollection bSortable_ { get; set; }
///
/// Gets or sets indicator for if a column is flagged as searchable or not on the client-side.
///
public ReadOnlyCollection bSearchable_ { get; set; }
///
/// Gets or sets individual column filter.
///
public ReadOnlyCollection sSearch_ { get; set; }
///
/// Gets or sets if individual column filter is regex or not.
///
public ReadOnlyCollection bRegex_ { get; set; }
///
/// Gets or sets the number of columns to sort on.
///
public int? iSortingCols { get; set; }
///
/// Gets or sets column being sorted on (you will need to decode this number for your database).
///
public ReadOnlyCollection iSortCol_ { get; set; }
///
/// Gets or sets the direction to be sorted - "desc" or "asc".
///
public ReadOnlyCollection sSortDir_ { get; set; }
///
/// Gets or sets the value specified by mDataProp for each column.
/// This can be useful for ensuring that the processing of data is independent
/// from the order of the columns.
///
public ReadOnlyCollection mDataProp_ { get; set; }
public ReadOnlyCollection GetSortedColumns()
{
if (!iSortingCols.HasValue)
{
// Return an empty collection since it's easier to work with when verifying against
return new ReadOnlyCollection(new List());
}
var sortedColumns = new List();
for (int i = 0; i < iSortingCols.Value; i++)
{
sortedColumns.Add(new SortedColumn(mDataProp_[iSortCol_[i]], sSortDir_[i]));
}
return sortedColumns.AsReadOnly();
}
}
///
/// Represents a sorted column from DataTables.
///
public class SortedColumn
{
private const string Ascending = "asc";
public SortedColumn(string propertyName, string sortingDirection)
{
PropertyName = propertyName;
Direction = sortingDirection.Equals(Ascending) ? SortingDirection.Ascending : SortingDirection.Descending;
}
///
/// Gets the name of the Property on the class to sort on.
///
public string PropertyName { get; private set; }
public SortingDirection Direction { get; private set; }
public override int GetHashCode()
{
var directionHashCode = Direction.GetHashCode();
return PropertyName != null ? PropertyName.GetHashCode() + directionHashCode : directionHashCode;
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (GetType() != obj.GetType())
{
return false;
}
var other = (SortedColumn)obj;
if (other.Direction != Direction)
{
return false;
}
return other.PropertyName == PropertyName;
}
}
///
/// Represents the direction of sorting for a column.
///
public enum SortingDirection
{
Ascending,
Descending
}
}