-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataTable.cs
373 lines (319 loc) · 13.5 KB
/
DataTable.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using DataTables.NetStandard.Extensions;
using DataTables.NetStandard.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace DataTables.NetStandard
{
/// <summary>
/// Base class for DataTables. Offers a default implementations for most required methods.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TEntityViewModel">The type of the entity view model.</typeparam>
public abstract class DataTable<TEntity, TEntityViewModel>
{
protected string _tableIdentifier;
protected DataTablesConfiguration _configuration;
protected bool _isConfigured = false;
/// <summary>
/// DataTable constructor. Gets and stores the table identifier.
/// </summary>
public DataTable()
{
SetTableIdentifier(BuildTableIdentifier());
_configuration = new DataTablesConfiguration();
}
/// <summary>
/// Column definitions for this DataTable.
/// </summary>
public abstract IList<DataTablesColumn<TEntity, TEntityViewModel>> Columns();
/// <summary>
/// Gets the query used to fetch data for the DataTable.
/// </summary>
public abstract IQueryable<TEntity> Query();
/// <summary>
/// A mapping function used to map query models to view models.
/// </summary>
public abstract Expression<Func<TEntity, TEntityViewModel>> MappingFunction();
/// <summary>
/// Gets the table identifier.
/// </summary>
public string GetTableIdentifier()
{
return _tableIdentifier;
}
/// <summary>
/// Sets the table identifier. Should only be used in the constructor.
/// </summary>
/// <param name="tableIdentifier">The table identifier.</param>
protected void SetTableIdentifier(string tableIdentifier)
{
_tableIdentifier = tableIdentifier;
}
/// <summary>
/// Renders the results based on the given <see cref="DataTablesRequest{TEntity, TEntityViewModel}"/>
/// and builds a response that can be returned immediately.
/// </summary>
/// <param name="query">The query.</param>
public virtual DataTablesResponse<TEntity, TEntityViewModel> RenderResponse(string query)
{
Configure();
var request = BuildRequest(query);
var data = RenderResults(request);
return new DataTablesResponse<TEntity, TEntityViewModel>(data, Columns(), request.Draw);
}
/// <summary>
/// Renders the results based on a <see cref="DataTablesRequest{TEntity, TEntityViewModel}"/>
/// built from the given <paramref name="query"/>.
/// </summary>
/// <param name="query">The query.</param>
public virtual IPagedList<TEntityViewModel> RenderResults(string query)
{
Configure();
return Query().ToPagedList(BuildRequest(query));
}
/// <summary>
/// Renders the results based on the given <see cref="DataTablesRequest{TEntity, TEntityViewModel}"/>.
/// </summary>
/// <param name="request">The request.</param>
public virtual IPagedList<TEntityViewModel> RenderResults(DataTablesRequest<TEntity, TEntityViewModel> request)
{
Configure();
return Query().ToPagedList(request);
}
/// <summary>
/// Renders the script.
/// </summary>
/// <param name="url">The url of the data endpoint for the DataTable</param>
/// <param name="method">The http method used for the data endpoint (get or post)</param>
public virtual string RenderScript(string url, string method = "get")
{
Configure();
return BuildConfigurationScript(GetTableIdentifier(), url, method);
}
/// <summary>
/// Renders the HTML.
/// </summary>
public virtual string RenderHtml()
{
Configure();
var sb = new StringBuilder();
sb.Append($"<table id=\"{GetTableIdentifier()}\">");
sb.Append(RenderTableHeader());
sb.Append(RenderTableBody());
sb.Append(RenderTableFooter());
sb.Append("</table>");
return sb.ToString();
}
/// <summary>
/// Returns a list of distinct column values that can be used for select filters.
/// </summary>
/// <param name="columnName">Name of the column.</param>
/// <exception cref="ArgumentException">No column with public name <paramref name="columnName"/> available.</exception>
public virtual IList<string> GetDistinctColumnValues(string columnName)
{
var column = Columns().FirstOrDefault(c => c.PublicName == columnName);
if (column == null)
{
throw new ArgumentException($"No column with public name {columnName} available.");
}
return GetDistinctColumnValues(column);
}
/// <summary>
/// Returns a list of distinct column values that can be used for select filters.
/// </summary>
/// <param name="column">The column.</param>
public virtual IList<string> GetDistinctColumnValues(DataTablesColumn<TEntity, TEntityViewModel> column)
{
var parameterExp = ExpressionHelper.BuildParameterExpression<TEntity>();
var propertyExp = ExpressionHelper.BuildPropertyExpression(parameterExp, column.PrivatePropertyName);
var stringExp = Expression.Call(propertyExp, ExpressionHelper.Object_ToString);
var lambda = Expression.Lambda<Func<TEntity, string>>(stringExp, parameterExp);
return GetDistinctColumnValues(lambda);
}
/// <summary>
/// Returns a list of distinct column values that can be used for select filters.
/// </summary>
/// <param name="property"></param>
public virtual IList<string> GetDistinctColumnValues(Expression<Func<TEntity, string>> property)
{
return Query().Select(property).Distinct().ToList();
}
/// <summary>
/// Builds the configuration script for the DataTable instance.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TEntityViewModel">The type of the entity view model.</typeparam>
/// <param name="tableName">Name of the table.</param>
/// <param name="url">The URL.</param>
/// <param name="method">The method.</param>
public string BuildConfigurationScript(string tableName, string url, string method)
{
var configuration = (DataTablesConfiguration)_configuration.Clone();
configuration.Ajax = new DataTablesConfiguration.AjaxConfiguration
{
Url = url,
Method = method
};
var config = JsonConvert.SerializeObject(configuration, GetSerializerSettings());
return $"var dt_{tableName} = $('#{tableName}').DataTable({config});";
}
/// <summary>
/// Allows to configure the DataTable instance.
/// </summary>
protected virtual void Configure()
{
if (_isConfigured)
{
return;
}
var columns = Columns();
ConfigureColumns(_configuration, columns);
ConfigureColumnOrdering(_configuration, columns);
ConfigureAdditionalOptions(_configuration, columns);
_isConfigured = true;
}
/// <summary>
/// Configures the columns in the DataTable settings which are serialized when rendering the table script.
/// </summary>
/// <param name="configuration"></param>
/// <param name="columns"></param>
protected virtual void ConfigureColumns(
DataTablesConfiguration configuration,
IList<DataTablesColumn<TEntity, TEntityViewModel>> columns)
{
foreach (var column in columns)
{
configuration.Columns.Add(new DataTablesConfiguration.DataTablesConfigurationColumn
{
Data = column.PublicName,
Name = column.PublicName,
Title = column.DisplayName ?? column.PublicName.FirstCharToUpper(),
Searchable = column.IsSearchable,
Orderable = column.IsOrderable,
AdditionalOptions = column.AdditionalOptions.DeepClone()
});
}
}
/// <summary>
/// Configures the column ordering in the DataTable settings which are serialized when rendering the table script.
/// </summary>
/// <param name="configuration"></param>
/// <param name="columns"></param>
protected virtual void ConfigureColumnOrdering(
DataTablesConfiguration configuration,
IList<DataTablesColumn<TEntity, TEntityViewModel>> columns)
{
var orderedColumns = columns.Where(c => c.OrderingIndex > -1).OrderBy(c => c.OrderingIndex);
foreach (var column in orderedColumns)
{
configuration.Order.Add(new List<string>
{
columns.IndexOf(column).ToString(),
column.OrderingDirection == System.ComponentModel.ListSortDirection.Descending ? "desc" : "asc"
});
}
}
/// <summary>
/// Allows to configure additional table options.
/// </summary>
/// <param name="configuration"></param>
/// <param name="columns"></param>
protected virtual void ConfigureAdditionalOptions(
DataTablesConfiguration configuration,
IList<DataTablesColumn<TEntity, TEntityViewModel>> columns)
{
// We don't configure anything here, but we provide a default implementation.
}
/// <summary>
/// Gets the serializer settings used to serialize the DataTables configuration.
/// </summary>
protected static JsonSerializerSettings GetSerializerSettings()
{
return new JsonSerializerSettings
{
Formatting = Formatting.None,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
}
/// <summary>
/// Renders the table header. Can be overwritten to change the rendering.
/// </summary>
protected virtual string RenderTableHeader()
{
var sb = new StringBuilder();
sb.Append("<thead>");
sb.Append("<tr>");
foreach (var column in Columns())
{
sb.Append(RenderTableHeaderColumn(column));
}
sb.Append("</tr>");
sb.Append("</thead>");
return sb.ToString();
}
/// <summary>
/// Renders a table header column. Can be overwritten to change the rendering.
/// It is also possible to return a different template based on the given column.
/// </summary>
/// <param name="column">The column.</param>
protected virtual string RenderTableHeaderColumn(DataTablesColumn<TEntity, TEntityViewModel> column)
{
return $"<th>{column.DisplayName}</th>";
}
/// <summary>
/// Renders the table body. Can be overwritten to change the rendering.
/// </summary>
protected virtual string RenderTableBody()
{
return "<td></td>";
}
/// <summary>
/// Renders the table footer. Can be overwritten to change the rendering.
/// </summary>
protected virtual string RenderTableFooter()
{
var sb = new StringBuilder();
sb.Append("<tfoot>");
sb.Append("<tr>");
foreach (var column in Columns())
{
sb.Append(RenderTableFooterColumn(column));
}
sb.Append("</tr>");
sb.Append("</tfoot>");
return sb.ToString();
}
/// <summary>
/// Renders the table footer column. Can be overwritten to change the rendering.
/// It is also possible to return a different template based on the given column.
/// </summary>
/// <param name="column">The column.</param>
protected virtual string RenderTableFooterColumn(DataTablesColumn<TEntity, TEntityViewModel> column)
{
return "<th></th>";
}
/// <summary>
/// Returns the identifier for the table. Can be something static or a generated
/// value that ensures uniqueness.
/// </summary>
protected virtual string BuildTableIdentifier()
{
return GetType().Name;
}
/// <summary>
/// Builds a <see cref="DataTablesRequest{TEntity, TEntityViewModel}"/> based on the given
/// <paramref name="query"/>, <see cref="Columns"/> and <see cref="MappingFunction"/>.
/// </summary>
/// <param name="query">The query.</param>
protected virtual DataTablesRequest<TEntity, TEntityViewModel> BuildRequest(string query)
{
return new DataTablesRequest<TEntity, TEntityViewModel>(query, Columns(), MappingFunction());
}
}
}