forked from gustavnavar/Grid.Blazor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridExtensions.cs
More file actions
87 lines (78 loc) · 3.21 KB
/
GridExtensions.cs
File metadata and controls
87 lines (78 loc) · 3.21 KB
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
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;
namespace GridMvc
{
public static class GridExtensions
{
internal const string DefaultPartialViewName = "_Grid";
public static HtmlGrid<T> Grid<T>(this IHtmlHelper helper, IEnumerable<T> items, IViewEngine viewEngine = null)
{
return Grid(helper, items, DefaultPartialViewName);
}
public static HtmlGrid<T> Grid<T>(this IHtmlHelper helper, IEnumerable<T> items, IQueryCollection query, IViewEngine viewEngine = null)
{
return Grid(helper, items, query, 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, 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);
}
}
}