Skip to content

Commit 213804d

Browse files
author
dave27stars
committed
add basic attribute support for visible and sortable - possible fix for #40
1 parent f4dc8ba commit 213804d

File tree

6 files changed

+100
-17
lines changed

6 files changed

+100
-17
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public DataTablesResult<UserView> GetUsers(DataTablesParam dataTableParam)
3838
Position = user.Position == null ? "" : user.Position.ToString(),
3939
Number = user.Number,
4040
Hired = user.Hired,
41-
IsAdmin = user.IsAdmin
41+
IsAdmin = user.IsAdmin,
42+
Salary = user.Salary
4243
});
4344
}
4445

@@ -65,7 +66,8 @@ private static List<User> Users()
6566
Position = positions[i%positions.Count],
6667
IsAdmin = i % 11 == 0,
6768
Number = (Numbers) r.Next(4),
68-
Hired = DateTime.UtcNow.AddDays(-1 * 365 * 3 * r.NextDouble())
69+
Hired = DateTime.UtcNow.AddDays(-1 * 365 * 3 * r.NextDouble()),
70+
Salary = 10000 + (DateTime.UtcNow.Minute * 1000) + (DateTime.UtcNow.Second * 100) + DateTime.UtcNow.Millisecond
6971
})
7072
));
7173
}
@@ -93,6 +95,8 @@ public class User
9395
public Numbers Number { get; set; }
9496

9597
public bool IsAdmin { get; set; }
98+
99+
public decimal Salary { get; set; }
96100
}
97101

98102
public class UserView
@@ -103,11 +107,16 @@ public class UserView
103107
public MvcHtmlString Name { get; set; }
104108

105109
public string Email { get; set; }
110+
111+
[DataTablesSortable(false)]
106112
public bool IsAdmin { get; set; }
107113
public string Position { get; set; }
108114
public DateTime Hired { get; set; }
109115

110116
public Numbers Number { get; set; }
117+
118+
[DataTablesVisible(false)]
119+
public decimal Salary { get; set; }
111120
}
112121

113122

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,20 @@
4040
"bStateSave": true,
4141
"bServerSide": true,
4242
"bFilter": @Model.ShowSearch.ToString().ToLower(),
43-
"sDom": '@Html.Raw(Model.Dom)',
44-
"aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
45-
"bAutoWidth": @Model.AutoWidth.ToString().ToLowerInvariant(),
46-
"sAjaxSource": "@Html.Raw(Model.AjaxUrl)", @Html.Raw(Model.TableTools ? "\"oTableTools\" : { \"sSwfPath\": \"/content/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf\" }," : "")
47-
"fnServerData": function(sSource, aoData, fnCallback) {
48-
$.ajax({
49-
"dataType": 'json',
50-
"type": "POST",
51-
"url": sSource,
52-
"data": aoData,
53-
"success": fnCallback
54-
});
55-
}
43+
"sDom": '@Html.Raw(Model.Dom)',
44+
"aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
45+
"bAutoWidth": @Model.AutoWidth.ToString().ToLowerInvariant(),
46+
"sAjaxSource": "@Html.Raw(Model.AjaxUrl)", @Html.Raw(Model.TableTools ? "\"oTableTools\" : { \"sSwfPath\": \"/content/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf\" }," : "")
47+
"fnServerData": function(sSource, aoData, fnCallback) {
48+
$.ajax({
49+
"dataType": 'json',
50+
"type": "POST",
51+
"url": sSource,
52+
"data": aoData,
53+
"success": fnCallback
54+
});
55+
},
56+
"aoColumnDefs" : @Html.Raw(Model.ColumnDefsString)
5657
@Html.Raw(!string.IsNullOrWhiteSpace(Model.JsOptionsString) ? ", " + Model.JsOptionsString : "")
5758
});
5859
@if (Model.ColumnFilter)

Mvc.JQuery.Datatables/DataTableConfigVm.cs

+30-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ public class ColDef
1010
{
1111
public string Name { get; set; }
1212
public string DisplayName { get; set; }
13+
public bool Visible { get; set; }
14+
public bool Sortable { get; set; }
1315
public Type Type { get; set; }
1416

15-
public static ColDef Create(string name, string p1, Type propertyType)
17+
public static ColDef Create(string name, string p1, Type propertyType, bool visible = true, bool sortable = true)
1618
{
17-
return new ColDef() {Name = name, DisplayName = p1, Type = propertyType};
19+
return new ColDef() {Name = name, DisplayName = p1, Type = propertyType, Visible = visible, Sortable = sortable};
1820
}
1921
}
2022
public class DataTableConfigVm
@@ -59,6 +61,14 @@ public string JsOptionsString
5961
}
6062
}
6163

64+
public string ColumnDefsString
65+
{
66+
get
67+
{
68+
return convertColumnDefsToJson(Columns);
69+
}
70+
}
71+
6272
public bool ColumnFilter { get; set; }
6373

6474
public bool TableTools { get; set; }
@@ -210,6 +220,24 @@ private static string convertDictionaryToJsonBody(IDictionary<string, object> di
210220
return (new JavaScriptSerializer()).Serialize((object)dictSystem).TrimStart('{').TrimEnd('}');
211221
}
212222

223+
private static string convertColumnDefsToJson(IEnumerable<ColDef> columns)
224+
{
225+
var nonSortableColumns = columns.Select((x, idx) => x.Sortable ? -1 : idx).Where( x => x > -1).ToArray();
226+
var nonVisibleColumns = columns.Select((x, idx) => x.Visible ? -1 : idx).Where( x => x > -1).ToArray();
227+
228+
var defs = new List<dynamic>();
229+
230+
if (nonSortableColumns.Any())
231+
defs.Add(new { bSortable = false, aTargets = nonSortableColumns });
232+
if (nonVisibleColumns.Any())
233+
defs.Add(new { bVisible = false, aTargets = nonVisibleColumns });
234+
235+
if (defs.Count > 0)
236+
return new JavaScriptSerializer().Serialize(defs);
237+
238+
return "[]";
239+
}
240+
213241
private static IDictionary<string, object> convertObjectToDictionary(object obj)
214242
{
215243
// Doing this way because RouteValueDictionary converts to Json in wrong format

Mvc.JQuery.Datatables/DataTablesHelper.cs

+9
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ public static DataTableConfigVm DataTableVm<TController, TResult>(this HtmlHelpe
4242
{
4343
var displayNameAttribute = (DisplayNameAttribute)propertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault();
4444
var displayName = displayNameAttribute == null ? propertyInfo.Name : displayNameAttribute.DisplayName;
45+
46+
var sortableAttribute = (DataTablesSortableAttribute)propertyInfo.GetCustomAttributes(typeof(DataTablesSortableAttribute), false).FirstOrDefault();
47+
var sortable = sortableAttribute == null ? true : sortableAttribute.Sortable;
48+
49+
var visibleAttribute = (DataTablesVisibleAttribute)propertyInfo.GetCustomAttributes(typeof(DataTablesVisibleAttribute), false).FirstOrDefault();
50+
var visible = visibleAttribute == null ? true : visibleAttribute.Visible;
51+
4552
columnList.Add(new ColDef()
4653
{
4754
Name = propertyInfo.Name,
4855
DisplayName = displayName,
56+
Sortable = sortable,
57+
Visible = visible,
4958
Type = propertyInfo.PropertyType
5059
});
5160
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Mvc.JQuery.Datatables
7+
{
8+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
9+
public class DataTablesSortableAttribute : Attribute
10+
{
11+
public DataTablesSortableAttribute() : this(true)
12+
{ }
13+
public DataTablesSortableAttribute(bool sortable)
14+
{
15+
this.Sortable = sortable;
16+
}
17+
18+
public bool Sortable { get; set; }
19+
}
20+
21+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
22+
public class DataTablesVisibleAttribute : Attribute
23+
{
24+
public DataTablesVisibleAttribute()
25+
: this(true)
26+
{ }
27+
public DataTablesVisibleAttribute(bool visible)
28+
{
29+
this.Visible = visible;
30+
}
31+
32+
public bool Visible { get; set; }
33+
}
34+
35+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</Reference>
5454
</ItemGroup>
5555
<ItemGroup>
56+
<Compile Include="DatatablesAtrtributes.cs" />
5657
<Compile Include="DataTablesData.cs" />
5758
<Compile Include="DataTablesFilter.cs" />
5859
<Compile Include="DataTablesHelper.cs" />

0 commit comments

Comments
 (0)