-
Notifications
You must be signed in to change notification settings - Fork 135
/
GridExtensions.cs
138 lines (125 loc) · 5.34 KB
/
GridExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using GridShared.Columns;
using GridMvc.Html;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Http;
using GridCore;
using Microsoft.Extensions.Primitives;
using GridShared.Utility;
using GridShared.Sorting;
using System.Linq;
namespace GridMvc
{
public static class GridExtensions
{
public static HtmlGrid<T> Grid<T>(this IHtmlHelper helper, IEnumerable<T> items, IViewEngine viewEngine = null)
{
return Grid(helper, items, GridRenderOptions.DefaultPartialViewName);
}
public static HtmlGrid<T> Grid<T>(this IHtmlHelper helper, IEnumerable<T> items, IQueryCollection query, IViewEngine viewEngine = null)
{
return Grid(helper, items, query, GridRenderOptions.DefaultPartialViewName);
}
public static HtmlGrid<T> Grid<T>(this IHtmlHelper helper, IEnumerable<T> items, string viewName, IViewEngine viewEngine = null)
{
var newGrid = new SGrid<T>(items, helper.ViewContext.HttpContext.Request.Query);
var htmlGrid = new HtmlGrid<T>(helper, newGrid, viewName);
return htmlGrid;
}
public static HtmlGrid<T> Grid<T>(this IHtmlHelper helper, IEnumerable<T> items, IQueryCollection query, string viewName, IViewEngine viewEngine = null)
{
var newGrid = new SGrid<T>(items, query);
var htmlGrid = new HtmlGrid<T>(helper, newGrid, viewName);
return htmlGrid;
}
public static HtmlGrid<T> Grid<T>(this IHtmlHelper helper, SGrid<T> sourceGrid, IViewEngine viewEngine = null)
{
//wrap source grid:
var htmlGrid = new HtmlGrid<T>(helper, sourceGrid, GridRenderOptions.DefaultPartialViewName);
return htmlGrid;
}
public static HtmlGrid<T> Grid<T>(this IHtmlHelper helper, SGrid<T> sourceGrid, string viewName, IViewEngine viewEngine)
{
//wrap source grid:
var htmlGrid = new HtmlGrid<T>(helper, sourceGrid, viewName);
return htmlGrid;
}
//support IHtmlString in RenderValueAs method
public static IGridColumn<T> RenderValueAs<T>(this IGridColumn<T> column,
Func<T, IHtmlContent> constraint)
{
Func<T, string> valueContraint = a =>
{
using (var sw = new StringWriter())
{
constraint(a).WriteTo(sw, HtmlEncoder.Default);
return sw.ToString();
}
};
return column.RenderValueAs(valueContraint);
}
//support WebPages inline helpers
public static IGridColumn<T> RenderValueAs<T>(this IGridColumn<T> column,
Func<T, Func<object, HelperResult>> constraint)
{
Func<T, string> valueContraint = a =>
{
using (var sw = new StringWriter())
{
constraint(a)(null).WriteTo(sw, HtmlEncoder.Default);
return sw.ToString();
}
};
return column.RenderValueAs(valueContraint);
}
public static QueryDictionary<StringValues> Convert(IQueryCollection collection)
{
QueryDictionary<StringValues> dictionary = new QueryDictionary<StringValues>();
foreach (var element in collection)
{
if (dictionary.ContainsKey(element.Key))
dictionary[element.Key] = StringValues.Concat(dictionary[element.Key], element.Value);
else
dictionary.Add(element.Key, element.Value);
}
// creare a consecutive sortValues
var sortings = dictionary.Get(ColumnOrderValue.DefaultSortingQueryParameter);
if (sortings.Count > 0)
{
// get sortValues from Query
var sortValues = new DefaultOrderColumnCollection();
foreach (string sorting in sortings)
{
ColumnOrderValue column = QueryDictionary<StringValues>.CreateColumnData(sorting);
if (column != ColumnOrderValue.Null)
sortValues.Add(column);
}
// creare a consecutive sortValues
var sortList = sortValues.OrderBy(r => r.Id).ToList();
int i = 0;
sortValues = new DefaultOrderColumnCollection();
foreach (var sortItem in sortList)
{
i++;
var column = new ColumnOrderValue
{
ColumnName = sortItem.ColumnName,
Direction = sortItem.Direction,
Id = i
};
sortValues.Add(column);
}
// update query with new sortValues
dictionary.Remove(ColumnOrderValue.DefaultSortingQueryParameter);
dictionary.Add(ColumnOrderValue.DefaultSortingQueryParameter, QueryDictionary<StringValues>.CreateStringValues(sortValues));
}
return dictionary;
}
}
}