EnVisageOnline/Main-RMO/Source/jQuery.DataTables.Mvc/JQueryDataTablesModel.cs

160 lines
5.0 KiB
C#

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