Skip to content

Commit 43f06f1

Browse files
committed
Added support for binding DataTablesParam to new-style Datatables query parameters (e.g. columns[#][orderable]) as well as still supporting the old-style (e.g. bSortable_#). As part of this, added support for searching columns by name (columns[#]data]) instead of just by index.
1 parent 6467111 commit 43f06f1

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

Mvc.JQuery.Datatables/DataTablesFiltering.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ public IQueryable<T> ApplyFiltersAndSort<T>(DataTablesParam dtParameters, IQuery
3131
var values = parts.Where(p => p != null);
3232
data = data.Where(string.Join(" or ", values), parameters.ToArray());
3333
}
34-
for (int i = 0; i < dtParameters.sSearchColumns.Count; i++)
34+
for (int i = 0; i < dtParameters.sSearchValues.Count; i++)
3535
{
3636
if (dtParameters.bSearchable[i])
3737
{
38-
var searchColumn = dtParameters.sSearchColumns[i];
38+
var searchColumn = dtParameters.sSearchValues[i];
3939
if (!string.IsNullOrWhiteSpace(searchColumn))
4040
{
41+
DataTablesPropertyInfo column = FindColumn(dtParameters, columns, i);
4142
var parameters = new List<object>();
42-
var filterClause = GetFilterClause(dtParameters.sSearchColumns[i], columns[i], parameters);
43+
var filterClause = GetFilterClause(searchColumn, column, parameters);
4344
if (string.IsNullOrWhiteSpace(filterClause) == false)
4445
{
4546
data = data.Where(filterClause, parameters.ToArray());
@@ -50,9 +51,9 @@ public IQueryable<T> ApplyFiltersAndSort<T>(DataTablesParam dtParameters, IQuery
5051
string sortString = "";
5152
for (int i = 0; i < dtParameters.iSortingCols; i++)
5253
{
53-
5454
int columnNumber = dtParameters.iSortCol[i];
55-
string columnName = columns[columnNumber].PropertyInfo.Name;
55+
DataTablesPropertyInfo column = FindColumn(dtParameters, columns, i);
56+
string columnName = column.PropertyInfo.Name;
5657
string sortDir = dtParameters.sSortDir[i];
5758
if (i != 0)
5859
sortString += ", ";
@@ -68,6 +69,18 @@ public IQueryable<T> ApplyFiltersAndSort<T>(DataTablesParam dtParameters, IQuery
6869
return data;
6970
}
7071

72+
private DataTablesPropertyInfo FindColumn(DataTablesParam dtParameters, DataTablesPropertyInfo[] columns, int i)
73+
{
74+
if (dtParameters.sColumnNames.Any())
75+
{
76+
return columns.First(x => x.PropertyInfo.Name == dtParameters.sColumnNames[i]);
77+
}
78+
else
79+
{
80+
return columns[i];
81+
}
82+
}
83+
7184
public delegate string ReturnedFilteredQueryForType(
7285
string query, string columnName, DataTablesPropertyInfo columnType, List<object> parametersForLinqQuery);
7386

Mvc.JQuery.Datatables/DataTablesModelBinder.cs

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Web.Mvc;
34

45
namespace Mvc.JQuery.Datatables
@@ -11,26 +12,83 @@ public class DataTablesModelBinder : IModelBinder
1112
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
1213
{
1314
var valueProvider = bindingContext.ValueProvider;
15+
int columns = GetValue<int>(valueProvider, "iColumns");
16+
if (columns == 0)
17+
{
18+
return BindV10Model(valueProvider);
19+
}
20+
else
21+
{
22+
return BindLegacyModel(valueProvider, columns);
23+
}
24+
}
1425

15-
DataTablesParam obj = new DataTablesParam(GetValue<int>(valueProvider, "iColumns"));
26+
private object BindV10Model(IValueProvider valueProvider)
27+
{
28+
DataTablesParam obj = new DataTablesParam();
29+
obj.iDisplayStart = GetValue<int>(valueProvider, "start");
30+
obj.iDisplayLength = GetValue<int>(valueProvider, "length");
31+
obj.sSearch = GetValue<string>(valueProvider, "search[value]");
32+
obj.bEscapeRegex = GetValue<bool>(valueProvider, "search[regex]");
33+
obj.sEcho = GetValue<int>(valueProvider, "draw");
34+
35+
int colIdx = 0;
36+
while (true)
37+
{
38+
string colPrefix = String.Format("columns[{0}]", colIdx);
39+
string colName = GetValue<string>(valueProvider, colPrefix+"[data]");
40+
if (String.IsNullOrWhiteSpace(colName)) {
41+
break;
42+
}
43+
obj.sColumnNames.Add(colName);
44+
obj.bSortable.Add(GetValue<bool>(valueProvider, colPrefix+"[orderable]"));
45+
obj.bSearchable.Add(GetValue<bool>(valueProvider, colPrefix+"[searchable]"));
46+
obj.sSearchValues.Add(GetValue<string>(valueProvider, colPrefix+"[search][value]"));
47+
obj.bEscapeRegexColumns.Add(GetValue<bool>(valueProvider, colPrefix+"[searchable][regex]"));
48+
colIdx++;
49+
}
50+
obj.iColumns = colIdx;
51+
colIdx = 0;
52+
while (true)
53+
{
54+
string colPrefix = String.Format("order[{0}]", colIdx);
55+
int? orderColumn = GetValue<int?>(valueProvider, colPrefix+"[column]");
56+
if (orderColumn.HasValue)
57+
{
58+
obj.iSortCol.Add(orderColumn.Value);
59+
obj.sSortDir.Add(GetValue<string>(valueProvider, colPrefix+"[dir]"));
60+
colIdx++;
61+
}
62+
else
63+
{
64+
break;
65+
}
66+
}
67+
obj.iSortingCols = colIdx;
68+
return obj;
69+
}
70+
71+
private DataTablesParam BindLegacyModel(IValueProvider valueProvider, int columns)
72+
{
73+
DataTablesParam obj = new DataTablesParam(columns);
1674

1775
obj.iDisplayStart = GetValue<int>(valueProvider, "iDisplayStart");
1876
obj.iDisplayLength = GetValue<int>(valueProvider, "iDisplayLength");
1977
obj.sSearch = GetValue<string>(valueProvider, "sSearch");
2078
obj.bEscapeRegex = GetValue<bool>(valueProvider, "bEscapeRegex");
2179
obj.iSortingCols = GetValue<int>(valueProvider, "iSortingCols");
2280
obj.sEcho = GetValue<int>(valueProvider, "sEcho");
23-
81+
2482
for (int i = 0; i < obj.iColumns; i++)
2583
{
2684
obj.bSortable.Add(GetValue<bool>(valueProvider, "bSortable_" + i));
2785
obj.bSearchable.Add(GetValue<bool>(valueProvider, "bSearchable_" + i));
28-
obj.sSearchColumns.Add(GetValue<string>(valueProvider, "sSearch_" + i));
86+
obj.sSearchValues.Add(GetValue<string>(valueProvider, "sSearch_" + i));
2987
obj.bEscapeRegexColumns.Add(GetValue<bool>(valueProvider, "bEscapeRegex_" + i));
3088
obj.iSortCol.Add(GetValue<int>(valueProvider, "iSortCol_" + i));
3189
obj.sSortDir.Add(GetValue<string>(valueProvider, "sSortDir_" + i));
3290
}
33-
return obj;
91+
return obj;
3492
}
3593

3694
private static T GetValue<T>(IValueProvider valueProvider, string key)

Mvc.JQuery.Datatables/DataTablesParam.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ public class DataTablesParam
1515
public bool bEscapeRegex { get; set; }
1616
public int iSortingCols { get; set; }
1717
public int sEcho { get; set; }
18+
public List<string> sColumnNames { get; set; }
1819
public List<bool> bSortable { get; set; }
1920
public List<bool> bSearchable { get; set; }
20-
public List<string> sSearchColumns { get; set; }
21+
public List<string> sSearchValues { get; set; }
2122
public List<int> iSortCol { get; set; }
2223
public List<string> sSortDir { get; set; }
2324
public List<bool> bEscapeRegexColumns { get; set; }
2425

2526
public DataTablesParam()
2627
{
28+
sColumnNames = new List<string>();
2729
bSortable = new List<bool>();
2830
bSearchable = new List<bool>();
29-
sSearchColumns = new List<string>();
31+
sSearchValues = new List<string>();
3032
iSortCol = new List<int>();
3133
sSortDir = new List<string>();
3234
bEscapeRegexColumns = new List<bool>();
@@ -35,9 +37,10 @@ public DataTablesParam()
3537
public DataTablesParam(int iColumns)
3638
{
3739
this.iColumns = iColumns;
40+
sColumnNames = new List<string>(iColumns);
3841
bSortable = new List<bool>(iColumns);
3942
bSearchable = new List<bool>(iColumns);
40-
sSearchColumns = new List<string>(iColumns);
43+
sSearchValues = new List<string>(iColumns);
4144
iSortCol = new List<int>(iColumns);
4245
sSortDir = new List<string>(iColumns);
4346
bEscapeRegexColumns = new List<bool>(iColumns);

0 commit comments

Comments
 (0)