-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mesut Talebi
committed
Feb 3, 2018
1 parent
387aaed
commit 0e221d1
Showing
7 changed files
with
149 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
megridview v0.3.3 | ||
megridview v0.4.0 | ||
Developed By Mesut Talebi ([email protected]) | ||
Open Source And no licence :) free to use | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
/* | ||
megridview v0.3.1 | ||
megridview v0.4.0 | ||
Developed By Mesut Talebi ([email protected]) | ||
Open Source And no licence :) free to use | ||
*/ | ||
|
||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace MT.GridView.Models | ||
{ | ||
|
@@ -22,7 +25,7 @@ public class PagingInfo | |
|
||
public int TotalPages | ||
{ | ||
get { return (int)Math.Ceiling((decimal)TotalItems / (ItemsPerPage != 0 ? ItemsPerPage : 1)); } | ||
get { return (int)Math.Ceiling((decimal)TotalItems / ( ItemsPerPage != 0 ? ItemsPerPage : 1 )); } | ||
} | ||
|
||
public SortObject Sort { get; set; } | ||
|
@@ -41,7 +44,7 @@ public class SortObject | |
|
||
public class FilterObject | ||
{ | ||
public string Column { get; set; } | ||
public string Column { get; set; } | ||
|
||
public string Value { get; set; } | ||
|
||
|
@@ -78,60 +81,156 @@ public enum FilterConjunction | |
Or | ||
} | ||
|
||
public class Extensions | ||
public static class Extensions | ||
{ | ||
private static LambdaExpression GetExpression<T>(string propertyName, string methodName, out Type type) | ||
{ | ||
string[] props = propertyName.Split('.'); | ||
type = typeof(T); | ||
|
||
ParameterExpression arg = Expression.Parameter(type, "x"); | ||
Expression expr = arg; | ||
|
||
foreach (string prop in props) | ||
{ | ||
// use reflection (not ComponentModel) to mirror LINQ | ||
PropertyInfo pi = type.GetProperty(prop); | ||
expr = Expression.Property(expr, pi); | ||
type = pi.PropertyType; | ||
} | ||
|
||
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type); | ||
|
||
return Expression.Lambda(delegateType, expr, arg); | ||
} | ||
|
||
private static IOrderedEnumerable<T> ApplyOrder<T>( | ||
IEnumerable<T> source, | ||
string propertyName, | ||
string methodName) | ||
{ | ||
Type type = typeof(T); | ||
LambdaExpression lambda = GetExpression<T>(propertyName, methodName, out type); | ||
|
||
object result = typeof(Queryable).GetMethods().Single( | ||
method => method.Name == methodName | ||
&& method.IsGenericMethodDefinition | ||
&& method.GetGenericArguments().Length == 2 | ||
&& method.GetParameters().Length == 2) | ||
.MakeGenericMethod(typeof(T), type) | ||
.Invoke(null, new object[] { source, lambda }); | ||
|
||
return (IOrderedEnumerable<T>)result; | ||
} | ||
|
||
|
||
private static IOrderedQueryable<T> ApplyOrder<T>( | ||
IQueryable<T> source, | ||
string propertyName, string methodName) | ||
{ | ||
Type type = typeof(T); | ||
LambdaExpression lambda = GetExpression<T>(propertyName, methodName, out type); | ||
|
||
object result = typeof(Queryable).GetMethods().Single( | ||
method => method.Name == methodName | ||
&& method.IsGenericMethodDefinition | ||
&& method.GetGenericArguments().Length == 2 | ||
&& method.GetParameters().Length == 2) | ||
.MakeGenericMethod(typeof(T), type) | ||
.Invoke(null, new object[] { source, lambda }); | ||
|
||
return (IOrderedQueryable<T>)result; | ||
} | ||
|
||
// IQueryable Sort Extention | ||
public static IOrderedQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, SortObject sortObject) | ||
{ | ||
if (sortObject != null) | ||
{ | ||
switch (sortObject.Direction) | ||
{ | ||
case SortDirection.Ascending: | ||
return ApplyOrder<TSource>(source, sortObject.SortColumn, "OrderBy"); | ||
case SortDirection.Descending: | ||
return ApplyOrder<TSource>(source, sortObject.SortColumn, "OrderByDescending"); | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return source as IOrderedQueryable<TSource>; | ||
} | ||
|
||
public static IOrderedEnumerable<TSource> Sort<TSource>(this IEnumerable<TSource> source, SortObject sortObject) | ||
{ | ||
if (sortObject != null) | ||
{ | ||
switch (sortObject.Direction) | ||
{ | ||
case SortDirection.Ascending: | ||
return ApplyOrder<TSource>(source, sortObject.SortColumn, "OrderBy"); | ||
case SortDirection.Descending: | ||
return ApplyOrder<TSource>(source, sortObject.SortColumn, "OrderByDescending"); | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return source as IOrderedEnumerable<TSource>; | ||
} | ||
|
||
public static string GetWhereClause(FilterObject filterObj, Type valueType) | ||
{ | ||
{ | ||
string whereClause = "true"; | ||
if (valueType != typeof (DateTime)) | ||
if (valueType != typeof(DateTime)) | ||
{ | ||
switch (filterObj.Operator) | ||
{ | ||
case FilterOperator.Contains: | ||
if (valueType == typeof (string)) | ||
if (valueType == typeof(string)) | ||
whereClause += string.Format(" {0} {1}.Contains(\"{2}\")", filterObj.Conjunction, | ||
filterObj.Column, filterObj.Value); | ||
break; | ||
case FilterOperator.GreaterThan: | ||
if (valueType != typeof (string)) | ||
if (valueType != typeof(string)) | ||
whereClause += string.Format(" {0} {1} > {2}", filterObj.Conjunction, filterObj.Column, | ||
filterObj.Value); | ||
break; | ||
case FilterOperator.GreaterThanOrEqual: | ||
if (valueType != typeof (string)) | ||
if (valueType != typeof(string)) | ||
whereClause += string.Format(" {0} {1} >= {2}", filterObj.Conjunction, filterObj.Column, | ||
filterObj.Value); | ||
break; | ||
case FilterOperator.LessThan: | ||
if (valueType != typeof (string)) | ||
if (valueType != typeof(string)) | ||
whereClause += string.Format(" {0} {1} < {2}", filterObj.Conjunction, filterObj.Column, | ||
filterObj.Value); | ||
break; | ||
case FilterOperator.LessThanOrEqual: | ||
if (valueType != typeof (string)) | ||
if (valueType != typeof(string)) | ||
whereClause += string.Format(" {0} {1} <= {2}", filterObj.Conjunction, filterObj.Column, | ||
filterObj.Value); | ||
break; | ||
case FilterOperator.StartsWith: | ||
if (valueType != typeof (string)) | ||
if (valueType != typeof(string)) | ||
whereClause += string.Format(" {0} {1}.StartsWith(\"{2}\")", filterObj.Conjunction, | ||
filterObj.Column, filterObj.Value); | ||
break; | ||
case FilterOperator.EndsWith: | ||
if (valueType != typeof (string)) | ||
if (valueType != typeof(string)) | ||
whereClause += string.Format(" {0} {1}.EndsWith(\"{2}\")", filterObj.Conjunction, | ||
filterObj.Column, filterObj.Value); | ||
break; | ||
case FilterOperator.Equals: | ||
|
||
whereClause += | ||
string.Format(valueType == typeof (string) ? " {0} {1} == \"{2}\"" : " {0} {1} == {2}", | ||
string.Format(valueType == typeof(string) ? " {0} {1} == \"{2}\"" : " {0} {1} == {2}", | ||
filterObj.Conjunction, filterObj.Column, filterObj.Value); | ||
break; | ||
case FilterOperator.NotEqual: | ||
|
||
whereClause += | ||
string.Format(valueType == typeof (string) ? " {0} {1} != \"{2}\"" : " {0} {1} != {2}", | ||
string.Format(valueType == typeof(string) ? " {0} {1} != \"{2}\"" : " {0} {1} != {2}", | ||
filterObj.Conjunction, filterObj.Column, filterObj.Value); | ||
break; | ||
default: | ||
|
@@ -145,11 +244,11 @@ public static string GetWhereClause(FilterObject filterObj, Type valueType) | |
|
||
switch (filterObj.Operator) | ||
{ | ||
case FilterOperator.Contains: | ||
case FilterOperator.Contains: | ||
break; | ||
case FilterOperator.GreaterThan: | ||
whereClause += string.Format(" {0} {1} > DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); | ||
|
||
whereClause += string.Format(" {0} {1} > DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); | ||
break; | ||
case FilterOperator.GreaterThanOrEqual: | ||
|
||
|
@@ -162,9 +261,9 @@ public static string GetWhereClause(FilterObject filterObj, Type valueType) | |
case FilterOperator.LessThanOrEqual: | ||
whereClause += string.Format(" {0} {1} <= DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); | ||
break; | ||
case FilterOperator.StartsWith: | ||
case FilterOperator.StartsWith: | ||
break; | ||
case FilterOperator.EndsWith: | ||
case FilterOperator.EndsWith: | ||
break; | ||
case FilterOperator.Equals: | ||
whereClause += string.Format(" {0} {1} == DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
megridview v0.3.3 | ||
megridview v0.4.0 | ||
Developed By Mesut Talebi ([email protected]) | ||
Open Source And no licence :) free to use | ||
*/ | ||
|
@@ -348,7 +348,7 @@ | |
$('th.sortable > span', obj).removeClass('fa-chevron-up').removeClass('fa-chevron-down'); | ||
|
||
//find sorted column | ||
var sortableTh = $('th.sortable[data-sort=' + $gridviewObject.Sort.SortColumn + ']', obj); | ||
var sortableTh = $('th.sortable[data-sort="' + $gridviewObject.Sort.SortColumn + '"]', obj); | ||
console.log(sortableTh); | ||
|
||
$(sortableTh).addClass('sorted'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters