Skip to content

Commit f4dc8ba

Browse files
committed
Fix for search not working
1 parent b652247 commit f4dc8ba

File tree

7 files changed

+38
-28
lines changed

7 files changed

+38
-28
lines changed

Mvc.JQuery.Datatables.Example/App_Code/RegisterVirtualPathProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static void AppInitialize()
88
{
99
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedResourceVirtualPathProvider.Vpp()
1010
{
11-
{typeof(Mvc.JQuery.Datatables.DataTableVm).Assembly, @"..\Mvc.JQuery.Datatables"}
11+
{typeof(Mvc.JQuery.Datatables.DataTableConfigVm).Assembly, @"..\Mvc.JQuery.Datatables"}
1212
});
1313
}
1414
}

Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@using Mvc.JQuery.Datatables
2-
@model DataTableVm
2+
@model DataTableConfigVm
33
@{
44

55
}
6-
<table id="@Model.Id" class="display @(Model.TableClass ?? DataTableVm.DefaultTableClass ?? "")" >
6+
<table id="@Model.Id" class="display @(Model.TableClass ?? DataTableConfigVm.DefaultTableClass ?? "")" >
77
<thead>
88
<tr>
99
@foreach (var column in Model.Columns)

Mvc.JQuery.Datatables/DataTableVm.cs renamed to Mvc.JQuery.Datatables/DataTableConfigVm.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ public static ColDef Create(string name, string p1, Type propertyType)
1717
return new ColDef() {Name = name, DisplayName = p1, Type = propertyType};
1818
}
1919
}
20-
public class DataTableVm
20+
public class DataTableConfigVm
2121
{
2222
IDictionary<string, object> m_JsOptions = new Dictionary<string, object>();
2323

24-
static DataTableVm()
24+
static DataTableConfigVm()
2525
{
2626
DefaultTableClass = "table table-bordered table-striped";
2727
}
2828

2929
public static string DefaultTableClass { get; set; }
3030
public string TableClass { get; set; }
3131

32-
public DataTableVm(string id, string ajaxUrl, IEnumerable<ColDef> columns)
32+
public DataTableConfigVm(string id, string ajaxUrl, IEnumerable<ColDef> columns)
3333
{
3434
AjaxUrl = ajaxUrl;
3535
this.Id = id;
@@ -101,6 +101,7 @@ public string GetFilterType(string columnName, Type type)
101101
{
102102
(c, t) => (DateTypes.Contains(t)) ? "{type: 'date-range'}" : null,
103103
(c, t) => t == typeof(bool) ? "{type: 'checkbox', values : ['True', 'False']}" : null,
104+
(c, t) => t == typeof(bool?) ? "{type: 'checkbox', values : ['True', 'False', 'null']}" : null,
104105
(c, t) => t.IsEnum ? ("{type: 'checkbox', values : ['" + string.Join("','", Enum.GetNames(t)) + "']}") : null,
105106
(c, t) => "{type: 'text'}", //by default, text filter on everything
106107
};
@@ -169,37 +170,37 @@ private void AddRule(string result)
169170
{
170171
if (result != "null" && this._jsOptions != null && this._jsOptions.Count > 0)
171172
{
172-
var _jsOptionsAsJson = DataTableVm.convertDictionaryToJsonBody(this._jsOptions);
173+
var _jsOptionsAsJson = DataTableConfigVm.convertDictionaryToJsonBody(this._jsOptions);
173174
result = result.TrimEnd('}') + ", " + _jsOptionsAsJson + "}";
174175
}
175176
_list.Insert(0, (c, t) => _predicate(c, t) ? result : null);
176177
}
177178
}
178-
public _FilterOn<DataTableVm> FilterOn<T>()
179+
public _FilterOn<DataTableConfigVm> FilterOn<T>()
179180
{
180181
return FilterOn<T>(null);
181182
}
182-
public _FilterOn<DataTableVm> FilterOn<T>(object jsOptions)
183+
public _FilterOn<DataTableConfigVm> FilterOn<T>(object jsOptions)
183184
{
184-
IDictionary<string, object> optionsDict = DataTableVm.convertObjectToDictionary(jsOptions);
185+
IDictionary<string, object> optionsDict = DataTableConfigVm.convertObjectToDictionary(jsOptions);
185186
return FilterOn<T>(optionsDict);
186187
}
187-
public _FilterOn<DataTableVm> FilterOn<T>(IDictionary<string, object> jsOptions)
188+
public _FilterOn<DataTableConfigVm> FilterOn<T>(IDictionary<string, object> jsOptions)
188189
{
189-
return new _FilterOn<DataTableVm>(this, this.FilterTypeRules, (c, t) => t == typeof(T), jsOptions);
190+
return new _FilterOn<DataTableConfigVm>(this, this.FilterTypeRules, (c, t) => t == typeof(T), jsOptions);
190191
}
191-
public _FilterOn<DataTableVm> FilterOn(string columnName)
192+
public _FilterOn<DataTableConfigVm> FilterOn(string columnName)
192193
{
193194
return FilterOn(columnName, null);
194195
}
195-
public _FilterOn<DataTableVm> FilterOn(string columnName, object jsOptions)
196+
public _FilterOn<DataTableConfigVm> FilterOn(string columnName, object jsOptions)
196197
{
197-
IDictionary<string, object> optionsDict = DataTableVm.convertObjectToDictionary(jsOptions);
198+
IDictionary<string, object> optionsDict = DataTableConfigVm.convertObjectToDictionary(jsOptions);
198199
return FilterOn(columnName, optionsDict);
199200
}
200-
public _FilterOn<DataTableVm> FilterOn(string columnName, IDictionary<string, object> jsOptions)
201+
public _FilterOn<DataTableConfigVm> FilterOn(string columnName, IDictionary<string, object> jsOptions)
201202
{
202-
return new _FilterOn<DataTableVm>(this, this.FilterTypeRules, (c, t) => c == columnName, jsOptions);
203+
return new _FilterOn<DataTableConfigVm>(this, this.FilterTypeRules, (c, t) => c == columnName, jsOptions);
203204
}
204205

205206
private static string convertDictionaryToJsonBody(IDictionary<string, object> dict)

Mvc.JQuery.Datatables/DataTablesFilter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public IQueryable FilterPagingSortingSearch(DataTablesParam dtParameters, IQuery
2727
}
2828
}
2929
}
30-
data = data.Where(string.Join(" or ", parts), parameters.ToArray());
30+
var values = parts.Where(p => p != null);
31+
data = data.Where(string.Join(" or ", values), parameters.ToArray());
3132
}
3233
for (int i = 0; i < dtParameters.sSearchColumns.Count; i++)
3334
{
@@ -106,7 +107,7 @@ private static string GetFilterClause(string query, ColInfo column, List<object>
106107
Func<string, string> filterClause = (queryPart) =>
107108
Filters.Select(
108109
f => f(queryPart, column.Name, column.Type, parametersForLinqQuery))
109-
.First(filterPart => filterPart != null);
110+
.FirstOrDefault(filterPart => filterPart != null) ?? "";
110111

111112
var queryParts = query.Split('|').Select(filterClause).Where(fc => fc != "").ToArray();
112113
if (queryParts.Any())

Mvc.JQuery.Datatables/DataTablesHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static IHtmlString DataTableIncludes(this HtmlHelper helper, bool jqueryU
3131
return helper.Raw(output.ToString());
3232
}
3333

34-
public static DataTableVm DataTableVm<TController, TResult>(this HtmlHelper html, string id, Expression<Func<TController, DataTablesResult<TResult>>> exp, IEnumerable<ColDef> columns = null)
34+
public static DataTableConfigVm DataTableVm<TController, TResult>(this HtmlHelper html, string id, Expression<Func<TController, DataTablesResult<TResult>>> exp, IEnumerable<ColDef> columns = null)
3535
{
3636
if (columns == null || !columns.Any())
3737
{
@@ -57,12 +57,12 @@ public static DataTableVm DataTableVm<TController, TResult>(this HtmlHelper html
5757
controllerName = controllerName.Substring(0, controllerName.LastIndexOf("Controller"));
5858
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
5959
var ajaxUrl = urlHelper.Action(mi.Name, controllerName);
60-
return new DataTableVm(id, ajaxUrl, columns);
60+
return new DataTableConfigVm(id, ajaxUrl, columns);
6161
}
6262

63-
public static DataTableVm DataTableVm(this HtmlHelper html, string id, string ajaxUrl, params string[] columns)
63+
public static DataTableConfigVm DataTableVm(this HtmlHelper html, string id, string ajaxUrl, params string[] columns)
6464
{
65-
return new DataTableVm(id, ajaxUrl, columns.Select(c => ColDef.Create(c, (string)null, typeof(string))));
65+
return new DataTableConfigVm(id, ajaxUrl, columns.Select(c => ColDef.Create(c, (string)null, typeof(string))));
6666
}
6767
}
6868
}

Mvc.JQuery.Datatables/Mvc.JQuery.Datatables.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<Compile Include="DataTablesModelBinder.cs" />
6060
<Compile Include="DataTablesParam.cs" />
6161
<Compile Include="DataTablesResult.cs" />
62-
<Compile Include="DataTableVm.cs" />
62+
<Compile Include="DataTableConfigVm.cs" />
6363
<Compile Include="DynamicLinq\DynamicLinq.cs" />
6464
<Compile Include="Properties\AssemblyInfo.cs" />
6565
<Compile Include="StaticReflectionHelper.cs" />

Mvc.JQuery.Datatables/TypeFilters.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static string NumericFilter(string query, string columnname, Type columnT
8080
catch (FormatException)
8181
{
8282
}
83-
return "false";
83+
return null;
8484
}
8585
}
8686

@@ -127,9 +127,17 @@ public static string BoolFilter(string query, string columnname, Type columnType
127127
{
128128
if (query != null)
129129
query = query.TrimStart('^').TrimEnd('$');
130-
if (string.IsNullOrWhiteSpace(query)) return columnname + " == null";
131-
if (query.ToLower() == "true") return columnname + " == true";
132-
return columnname + " == false";
130+
var lowerCaseQuery = query.ToLowerInvariant();
131+
if (lowerCaseQuery == "false" || lowerCaseQuery == "true")
132+
{
133+
if (query.ToLower() == "true") return columnname + " == true";
134+
return columnname + " == false";
135+
}
136+
if (columnType == typeof (bool?))
137+
{
138+
if (lowerCaseQuery == "null") return columnname + " == null";
139+
}
140+
return null;
133141

134142
}
135143

0 commit comments

Comments
 (0)