-
Notifications
You must be signed in to change notification settings - Fork 3
/
PersonDataTable.cs
177 lines (167 loc) · 8.06 KB
/
PersonDataTable.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using DataTables.NetStandard.Sample.DataTables.ViewModels;
using DataTables.NetStandard.Sample.Models;
using Microsoft.EntityFrameworkCore;
namespace DataTables.NetStandard.Sample.DataTables
{
public class PersonDataTable : BaseDataTable<Person, PersonViewModel>
{
protected SampleDbContext _dbContext;
public PersonDataTable(IMapper mapper, SampleDbContext dbContext) : base(mapper)
{
_dbContext = dbContext;
}
public override IList<DataTablesColumn<Person, PersonViewModel>> Columns()
{
var columns = new List<DataTablesColumn<Person, PersonViewModel>>
{
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "id",
DisplayName = "ID",
PublicPropertyName = nameof(PersonViewModel.Id),
PrivatePropertyName = nameof(Person.Id),
IsOrderable = true,
IsSearchable = true,
SearchPredicate = (p, s) => false, // The fallback predicate will never match, but since we declared a provider, it is not used anyway.
//SearchPredicateProvider = (s) => (p, s) => true, // The provider will return a predicate matching all entities (used for global search). This is just for illustration, it makes no sense.
ColumnSearchPredicateProvider = (s) => // The column provider will return a predicate matching entities in a numeric range if the search term is properly formatted.
{
var minMax = s.Split("-delim-", System.StringSplitOptions.RemoveEmptyEntries);
if (minMax.Length >= 2)
{
var min = long.Parse(minMax[0]);
var max = long.Parse(minMax[1]);
return (p, s) => p.Id >= min && p.Id <= max;
}
return (p, s) => false;
}
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "name",
DisplayName = "Name",
PublicPropertyName = nameof(PersonViewModel.Name),
PrivatePropertyName = nameof(Person.Name),
IsOrderable = true,
IsSearchable = true
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "email",
DisplayName = "Email",
PublicPropertyName = nameof(PersonViewModel.Email),
PrivatePropertyName = nameof(Person.Email),
IsOrderable = true,
IsSearchable = true
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "otherEmails",
DisplayName = "Other Emails",
PublicPropertyName = nameof(PersonViewModel.OtherEmails),
PrivatePropertyName = null,
IsOrderable = false,
IsSearchable = true,
SearchPredicate = (p, s) => p.EmailAddresses.Any(e => e.Address.ToUpper().Contains(s.ToUpper()))
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "dateOfBirth",
DisplayName = "Date of Birth",
PublicPropertyName = nameof(PersonViewModel.DateOfBirth),
PrivatePropertyName = nameof(Person.DateOfBirth),
IsOrderable = true,
IsSearchable = false
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "address",
DisplayName = "Address",
PublicPropertyName = nameof(PersonViewModel.Address),
PrivatePropertyName = $"{nameof(Person.Location)}.{nameof(Location.Street)}",
IsOrderable = true,
IsSearchable = true,
SearchPredicate = (p, s) => (p.Location.Street + " " + p.Location.HouseNumber).ToLower().Contains(s.ToLower()),
ColumnOrderingExpression = (p) => (p.Location.Street + " " + (p.Location.HouseNumber ?? "")).Trim().ToLower()
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "postCode",
DisplayName = "Post Code",
PublicPropertyName = nameof(PersonViewModel.PostCode),
PrivatePropertyName = $"{nameof(Person.Location)}.{nameof(Location.PostCode)}",
IsOrderable = true,
IsSearchable = true,
OrderingIndex = 0,
OrderingDirection = System.ComponentModel.ListSortDirection.Ascending
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "city",
DisplayName = "City",
PublicPropertyName = nameof(PersonViewModel.City),
PrivatePropertyName = $"{nameof(Person.Location)}.{nameof(Location.City)}",
IsOrderable = true,
IsSearchable = true
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "country",
DisplayName = "Country",
PublicPropertyName = nameof(PersonViewModel.Country),
PrivatePropertyName = $"{nameof(Person.Location)}.{nameof(Location.Country)}",
IsOrderable = true,
IsSearchable = true,
OrderingIndex = 1,
OrderingDirection = System.ComponentModel.ListSortDirection.Descending
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "action",
DisplayName = "Action",
PublicPropertyName = nameof(PersonViewModel.Action),
PrivatePropertyName = null,
IsOrderable = false,
IsSearchable = false
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "action2",
DisplayName = "Action 2",
PublicPropertyName = nameof(PersonViewModel.Action2),
PrivatePropertyName = null,
IsOrderable = false,
IsSearchable = false
},
new DataTablesColumn<Person, PersonViewModel>
{
PublicName = "action3",
DisplayName = "Action 3",
PublicPropertyName = nameof(PersonViewModel.Action3),
PrivatePropertyName = null,
IsOrderable = false,
IsSearchable = false
}
};
// We can also add additional options to a column
columns.Last().AdditionalOptions.Add("className", "text-center");
return columns;
}
public override IQueryable<Person> Query()
{
return _dbContext.Persons
.Include(p => p.EmailAddresses)
.Include(p => p.Location);
}
protected override void ConfigureAdditionalOptions(DataTablesConfiguration configuration, IList<DataTablesColumn<Person, PersonViewModel>> columns)
{
base.ConfigureAdditionalOptions(configuration, columns);
configuration.AdditionalOptions["stateSave"] = true;
configuration.AdditionalOptions["pagingType"] = "full_numbers";
configuration.AdditionalOptions["search"] = new { smart = true };
}
}
}