Skip to content

Commit b645423

Browse files
committed
Added facet example
1 parent a406ea0 commit b645423

17 files changed

+372
-164
lines changed

Mvc.JQuery.Datatables.Core/DataTableConfigVm.cs

+32-12
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ public DataTableConfigVm(string id, string ajaxUrl, IEnumerable<ColDef> columns)
2929
AjaxUrl = ajaxUrl;
3030
this.Id = id;
3131
this.Columns = columns;
32-
this.ShowSearch = true;
32+
this.Filter = true;
33+
3334
this.ShowPageSizes = true;
3435
this.TableTools = true;
3536
ColumnFilterVm = new ColumnFilterSettingsVm(this);
3637
AjaxErrorHandler =
3738
"function(jqXHR, textStatus, errorThrown)" +
3839
"{ " +
39-
"window.alert('error loading data: ' + textStatus + ' - ' + errorThrown); " +
40+
"console.log('error loading data: ' + textStatus + ' - ' + errorThrown); " +
4041
"console.log(arguments);" +
4142
"}";
4243
}
4344

44-
public bool ShowSearch { get; set; }
45+
/// <summary>
46+
/// Sets the bFilter properyt
47+
/// </summary>
48+
public bool Filter { get; set; }
4549

4650
public string Id { get; private set; }
4751

@@ -90,20 +94,36 @@ public string Dom
9094
return _dom;
9195

9296
string str = "";
93-
if (this.ColVis)
97+
if (this.ShowVisibleColumnPicker)
9498
str += "C";
9599
if (this.TableTools)
96100
str += "T<\"clear\">";
97101
if (this.ShowPageSizes)
98102
str += "l";
99-
if (this.ShowSearch)
103+
if (this.ShowFilterInput)
100104
str += "f";
101105
return str + "tipr";
102106
}
103107

104108
set { _dom = value; }
105109
}
106110

111+
112+
public bool ShowVisibleColumnPicker { get; set; }
113+
114+
public bool ShowFilterInput { get; set; }
115+
116+
[Obsolete("Use .Filter and .ShowFilterInput")]
117+
public bool ShowSearch
118+
{
119+
get { return ShowFilterInput && Filter; }
120+
set
121+
{
122+
ShowFilterInput = value;
123+
Filter = value;
124+
}
125+
}
126+
107127
public bool ColVis { get; set; }
108128

109129
public string ColumnSortingString
@@ -202,9 +222,9 @@ public _FilterOn<DataTableConfigVm> FilterOn<T>(object jsOptions)
202222
IDictionary<string, object> optionsDict = DataTableConfigVm.ConvertObjectToDictionary(jsOptions);
203223
return FilterOn<T>(optionsDict);
204224
}
205-
////public _FilterOn<DataTableConfigVm> FilterOn<T>(IDictionary<string, object> jsOptions)
225+
////public _FilterOn<DataTableConfigVm> FilterOn<T>(IDictionary<string, object> filterOptions)
206226
////{
207-
//// return new _FilterOn<DataTableConfigVm>(this, this.FilterTypeRules, (c, t) => t == typeof(T), jsOptions);
227+
//// return new _FilterOn<DataTableConfigVm>(this, this.FilterTypeRules, (c, t) => t == typeof(T), filterOptions);
208228
////}
209229
public _FilterOn<DataTableConfigVm> FilterOn(string columnName)
210230
{
@@ -221,16 +241,16 @@ public _FilterOn<DataTableConfigVm> FilterOn(string columnName, object jsOptions
221241
IDictionary<string, object> initialSearchColsDict = ConvertObjectToDictionary(jsInitialSearchCols);
222242
return FilterOn(columnName, optionsDict, initialSearchColsDict);
223243
}
224-
public _FilterOn<DataTableConfigVm> FilterOn(string columnName, IDictionary<string, object> jsOptions)
244+
public _FilterOn<DataTableConfigVm> FilterOn(string columnName, IDictionary<string, object> filterOptions)
225245
{
226-
return FilterOn(columnName, jsOptions, null);
246+
return FilterOn(columnName, filterOptions, null);
227247
}
228-
public _FilterOn<DataTableConfigVm> FilterOn(string columnName, IDictionary<string, object> jsOptions, IDictionary<string, object> jsInitialSearchCols)
248+
public _FilterOn<DataTableConfigVm> FilterOn(string columnName, IDictionary<string, object> filterOptions, IDictionary<string, object> jsInitialSearchCols)
229249
{
230250
var colDef = this.Columns.Single(c => c.Name == columnName);
231-
if (jsOptions != null)
251+
if (filterOptions != null)
232252
{
233-
foreach (var jsOption in jsOptions)
253+
foreach (var jsOption in filterOptions)
234254
{
235255
colDef.Filter[jsOption.Key] = jsOption.Value;
236256
}

Mvc.JQuery.Datatables.Core/DataTablesFilterAttribute.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,38 @@ public class DataTablesFilterAttribute : DataTablesAttributeBase
99
{
1010
private readonly string filterType;
1111
private readonly object[] options;
12-
1312
public DataTablesFilterAttribute()
1413
{
1514
}
1615

17-
public DataTablesFilterAttribute(DataTablesFilterType filterType, params object[] options)
18-
:this(GetFilterTypeString(filterType), options)
16+
public DataTablesFilterAttribute(DataTablesFilterType filterType)
17+
:this(GetFilterTypeString(filterType))
1918
{
2019

2120
}
2221

22+
/// <summary>
23+
/// Sets sSelector on the column (i.e. selector for custom positioning)
24+
/// </summary>
25+
public string Selector { get; set; }
26+
2327
private static string GetFilterTypeString(DataTablesFilterType filterType)
2428
{
2529
return filterType.ToString().ToLower().Replace("range", "-range");
2630
}
2731

28-
public DataTablesFilterAttribute(string filterType, params object[] options)
32+
public DataTablesFilterAttribute(string filterType)
2933
{
3034
this.filterType = filterType;
31-
this.options = options;
3235
}
3336

3437
public override void ApplyTo(ColDef colDef, System.Reflection.PropertyInfo pi)
3538
{
36-
colDef.Filter = new FilterDef(pi.GetType());
39+
colDef.Filter = new FilterDef(pi.PropertyType);
40+
if (Selector != null)
41+
{
42+
colDef.Filter["sSelector"] = Selector;
43+
}
3744
if (filterType == "none")
3845
{
3946
colDef.Filter = null;
@@ -43,6 +50,7 @@ public override void ApplyTo(ColDef colDef, System.Reflection.PropertyInfo pi)
4350
if (filterType != null) colDef.Filter.type = filterType;
4451
if (options != null && options.Any()) colDef.Filter.values = options;
4552
}
53+
4654
}
4755

4856
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Reflection;
3+
using Mvc.JQuery.DataTables.Models;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace Mvc.JQuery.DataTables.Example.Controllers
7+
{
8+
public class DefaultToStartOf2014Attribute : DataTablesAttributeBase
9+
{
10+
public override void ApplyTo(ColDef colDef, PropertyInfo pi)
11+
{
12+
colDef.SearchCols = colDef.SearchCols ?? new JObject();
13+
colDef.SearchCols["sSearch"] = new DateTime(2014, 1, 1).ToString("g") + "~" + DateTimeOffset.Now.Date.AddDays(1).ToString("g");
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Linq;
3+
using System.Web.Mvc;
4+
using Mvc.JQuery.DataTables.Example.Domain;
5+
using Mvc.JQuery.DataTables.Models;
6+
7+
namespace Mvc.JQuery.DataTables.Example.Controllers
8+
{
9+
public class FacetController : Controller
10+
{
11+
12+
public ActionResult Index()
13+
{
14+
return View();
15+
}
16+
17+
public DataTablesResult<UserFacetRowViewModel> GetFacetedUsers(DataTablesParam dataTableParam)
18+
{
19+
return DataTablesResult.Create(FakeDatabase.Users.Select(user => new UserFacetRowViewModel()
20+
{
21+
Email = user.Email,
22+
Position = user.Position == null ? "" : user.Position.ToString(),
23+
Hired = user.Hired,
24+
IsAdmin = user.IsAdmin,
25+
Content = "https://randomuser.me/api/portraits/thumb/men/" + user.Id + ".jpg"
26+
}), dataTableParam,
27+
rowViewModel => new
28+
{
29+
Content = "<div>" +
30+
" <div>Email: " + rowViewModel.Email + (rowViewModel.IsAdmin ? " (admin)" : "") + "</div>" +
31+
" <div>Hired: " + rowViewModel.Hired + "</div>" +
32+
" <img src='" + rowViewModel.Content + "' />" +
33+
"</div>"
34+
});
35+
}
36+
37+
38+
public class UserFacetRowViewModel
39+
{
40+
[DataTables(DisplayName = "E-Mail", Searchable = true, Visible = false)]
41+
[DataTablesFilter(Selector = "#" + nameof(Email) + "Filter")]
42+
public string Email { get; set; }
43+
44+
[DataTables(Width = "70px", Visible = false)]
45+
[DataTablesFilter(Selector = "#" +nameof(IsAdmin) + "Filter")]
46+
public bool IsAdmin { get; set; }
47+
48+
49+
[DataTables(Width = "70px", Visible = false)]
50+
[DataTablesFilter(Selector = "#" + nameof(Position) + "Filter")]
51+
public string Position { get; set; }
52+
53+
[DataTablesFilter(DataTablesFilterType.DateTimeRange, Selector = "#" + nameof(Hired) + "Filter")]
54+
[DefaultToStartOf2014]
55+
[DataTables(Visible = false)]
56+
public DateTime? Hired { get; set; }
57+
58+
[DataTables(Sortable = false, Searchable = false)]
59+
[DataTablesFilter(DataTablesFilterType.None)]
60+
public string Content { get; set; }
61+
}
62+
}
63+
}

Mvc.JQuery.Datatables.Example/Controllers/FakeDatabase.cs

-69
This file was deleted.

0 commit comments

Comments
 (0)