Created
July 15, 2019 21:48
-
-
Save davidsonsousa/99030880343e6616f549bac966a08c53 to your computer and use it in GitHub Desktop.
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
using ReportsWeb.Attributes; | |
using ReportsWeb.Helpers; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
namespace ReportsWeb.Models.DataTable | |
{ | |
public class PersonTableViewModel | |
{ | |
[ShowOnDataTable(0, "Full name"), Searchable, Orderable] | |
public string FullName { get; set; } | |
[ShowOnDataTable(1, "Preferred name"), Searchable, Orderable] | |
public string PreferredName { get; set; } | |
[ShowOnDataTable(2), Searchable, Orderable] | |
public string Phone { get; set; } | |
[ShowOnDataTable(3, "E-mail"), Searchable, Orderable] | |
public string Email { get; set; } | |
[ShowOnDataTable(4), Searchable, Orderable] | |
public string Age { get; set; } | |
[ShowOnDataTable(5), Searchable, Orderable] | |
public string Status { get; set; } | |
public static void Filter(ref IEnumerable<PersonTableViewModel> items, string searchTerm) | |
{ | |
if (!string.IsNullOrWhiteSpace(searchTerm)) | |
{ | |
var searchableProperties = typeof(PersonTableViewModel).GetProperties().Where(p => Attribute.IsDefined(p, typeof(Searchable))); | |
var lambda = PrepareFilter(searchTerm, searchableProperties); | |
items = items.Where(lambda.Compile()); | |
} | |
} | |
public static void Order(ref IEnumerable<PersonTableViewModel> items, int orderColumn, string orderDirection) | |
{ | |
try | |
{ | |
switch (orderColumn) | |
{ | |
case 0: | |
items = orderDirection == "asc" ? items.OrderBy(o => o.FullName) : items.OrderByDescending(o => o.FullName); | |
break; | |
case 1: | |
items = orderDirection == "asc" ? items.OrderBy(o => o.PreferredName) : items.OrderByDescending(o => o.PreferredName); | |
break; | |
case 2: | |
items = orderDirection == "asc" ? items.OrderBy(o => o.Phone) : items.OrderByDescending(o => o.Phone); | |
break; | |
case 3: | |
items = orderDirection == "asc" ? items.OrderBy(o => o.Email) : items.OrderByDescending(o => o.Email); | |
break; | |
case 4: | |
items = orderDirection == "asc" ? items.OrderBy(o => o.Age) : items.OrderByDescending(o => o.Age); | |
break; | |
case 5: | |
items = orderDirection == "asc" ? items.OrderBy(o => o.Status) : items.OrderByDescending(o => o.Status); | |
break; | |
default: | |
items = items.OrderBy(o => o.FullName); | |
break; | |
} | |
} | |
catch | |
{ | |
throw; | |
} | |
} | |
private static Expression<Func<PersonTableViewModel, bool>> PrepareFilter(string searchTerm, IEnumerable<PropertyInfo> searchableProperties) | |
{ | |
var expressionFilter = new List<ExpressionFilter>(); | |
foreach (var property in searchableProperties) | |
{ | |
expressionFilter.Add(new ExpressionFilter | |
{ | |
PropertyName = property.Name, | |
Operation = Operations.Contains, | |
Value = searchTerm | |
}); | |
} | |
return ExpressionBuilder.GetExpression<PersonTableViewModel>(expressionFilter, LogicalOperators.Or); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment