From 213804de10353204d5ddcc00f5fbe8569cb8ea88 Mon Sep 17 00:00:00 2001 From: dave27stars Date: Fri, 20 Sep 2013 16:12:06 +0100 Subject: [PATCH] add basic attribute support for visible and sortable - possible fix for #40 --- .../Controllers/HomeController.cs | 13 +++++-- .../Views/Shared/DataTable.cshtml | 27 +++++++------- Mvc.JQuery.Datatables/DataTableConfigVm.cs | 32 +++++++++++++++-- Mvc.JQuery.Datatables/DataTablesHelper.cs | 9 +++++ .../DatatablesAtrtributes.cs | 35 +++++++++++++++++++ .../Mvc.JQuery.Datatables.csproj | 1 + 6 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 Mvc.JQuery.Datatables/DatatablesAtrtributes.cs diff --git a/Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs b/Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs index 4eff16d..1f332df 100644 --- a/Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs +++ b/Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs @@ -38,7 +38,8 @@ public DataTablesResult GetUsers(DataTablesParam dataTableParam) Position = user.Position == null ? "" : user.Position.ToString(), Number = user.Number, Hired = user.Hired, - IsAdmin = user.IsAdmin + IsAdmin = user.IsAdmin, + Salary = user.Salary }); } @@ -65,7 +66,8 @@ private static List Users() Position = positions[i%positions.Count], IsAdmin = i % 11 == 0, Number = (Numbers) r.Next(4), - Hired = DateTime.UtcNow.AddDays(-1 * 365 * 3 * r.NextDouble()) + Hired = DateTime.UtcNow.AddDays(-1 * 365 * 3 * r.NextDouble()), + Salary = 10000 + (DateTime.UtcNow.Minute * 1000) + (DateTime.UtcNow.Second * 100) + DateTime.UtcNow.Millisecond }) )); } @@ -93,6 +95,8 @@ public class User public Numbers Number { get; set; } public bool IsAdmin { get; set; } + + public decimal Salary { get; set; } } public class UserView @@ -103,11 +107,16 @@ public class UserView public MvcHtmlString Name { get; set; } public string Email { get; set; } + + [DataTablesSortable(false)] public bool IsAdmin { get; set; } public string Position { get; set; } public DateTime Hired { get; set; } public Numbers Number { get; set; } + + [DataTablesVisible(false)] + public decimal Salary { get; set; } } diff --git a/Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml b/Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml index 1189309..2af0a01 100644 --- a/Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml +++ b/Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml @@ -40,19 +40,20 @@ "bStateSave": true, "bServerSide": true, "bFilter": @Model.ShowSearch.ToString().ToLower(), - "sDom": '@Html.Raw(Model.Dom)', - "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]], - "bAutoWidth": @Model.AutoWidth.ToString().ToLowerInvariant(), - "sAjaxSource": "@Html.Raw(Model.AjaxUrl)", @Html.Raw(Model.TableTools ? "\"oTableTools\" : { \"sSwfPath\": \"/content/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf\" }," : "") - "fnServerData": function(sSource, aoData, fnCallback) { - $.ajax({ - "dataType": 'json', - "type": "POST", - "url": sSource, - "data": aoData, - "success": fnCallback - }); - } + "sDom": '@Html.Raw(Model.Dom)', + "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]], + "bAutoWidth": @Model.AutoWidth.ToString().ToLowerInvariant(), + "sAjaxSource": "@Html.Raw(Model.AjaxUrl)", @Html.Raw(Model.TableTools ? "\"oTableTools\" : { \"sSwfPath\": \"/content/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf\" }," : "") + "fnServerData": function(sSource, aoData, fnCallback) { + $.ajax({ + "dataType": 'json', + "type": "POST", + "url": sSource, + "data": aoData, + "success": fnCallback + }); + }, + "aoColumnDefs" : @Html.Raw(Model.ColumnDefsString) @Html.Raw(!string.IsNullOrWhiteSpace(Model.JsOptionsString) ? ", " + Model.JsOptionsString : "") }); @if (Model.ColumnFilter) diff --git a/Mvc.JQuery.Datatables/DataTableConfigVm.cs b/Mvc.JQuery.Datatables/DataTableConfigVm.cs index dfc1e45..f4e52bf 100644 --- a/Mvc.JQuery.Datatables/DataTableConfigVm.cs +++ b/Mvc.JQuery.Datatables/DataTableConfigVm.cs @@ -10,11 +10,13 @@ public class ColDef { public string Name { get; set; } public string DisplayName { get; set; } + public bool Visible { get; set; } + public bool Sortable { get; set; } public Type Type { get; set; } - public static ColDef Create(string name, string p1, Type propertyType) + public static ColDef Create(string name, string p1, Type propertyType, bool visible = true, bool sortable = true) { - return new ColDef() {Name = name, DisplayName = p1, Type = propertyType}; + return new ColDef() {Name = name, DisplayName = p1, Type = propertyType, Visible = visible, Sortable = sortable}; } } public class DataTableConfigVm @@ -59,6 +61,14 @@ public string JsOptionsString } } + public string ColumnDefsString + { + get + { + return convertColumnDefsToJson(Columns); + } + } + public bool ColumnFilter { get; set; } public bool TableTools { get; set; } @@ -210,6 +220,24 @@ private static string convertDictionaryToJsonBody(IDictionary di return (new JavaScriptSerializer()).Serialize((object)dictSystem).TrimStart('{').TrimEnd('}'); } + private static string convertColumnDefsToJson(IEnumerable columns) + { + var nonSortableColumns = columns.Select((x, idx) => x.Sortable ? -1 : idx).Where( x => x > -1).ToArray(); + var nonVisibleColumns = columns.Select((x, idx) => x.Visible ? -1 : idx).Where( x => x > -1).ToArray(); + + var defs = new List(); + + if (nonSortableColumns.Any()) + defs.Add(new { bSortable = false, aTargets = nonSortableColumns }); + if (nonVisibleColumns.Any()) + defs.Add(new { bVisible = false, aTargets = nonVisibleColumns }); + + if (defs.Count > 0) + return new JavaScriptSerializer().Serialize(defs); + + return "[]"; + } + private static IDictionary convertObjectToDictionary(object obj) { // Doing this way because RouteValueDictionary converts to Json in wrong format diff --git a/Mvc.JQuery.Datatables/DataTablesHelper.cs b/Mvc.JQuery.Datatables/DataTablesHelper.cs index ed9080a..c639631 100644 --- a/Mvc.JQuery.Datatables/DataTablesHelper.cs +++ b/Mvc.JQuery.Datatables/DataTablesHelper.cs @@ -42,10 +42,19 @@ public static DataTableConfigVm DataTableVm(this HtmlHelpe { var displayNameAttribute = (DisplayNameAttribute)propertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault(); var displayName = displayNameAttribute == null ? propertyInfo.Name : displayNameAttribute.DisplayName; + + var sortableAttribute = (DataTablesSortableAttribute)propertyInfo.GetCustomAttributes(typeof(DataTablesSortableAttribute), false).FirstOrDefault(); + var sortable = sortableAttribute == null ? true : sortableAttribute.Sortable; + + var visibleAttribute = (DataTablesVisibleAttribute)propertyInfo.GetCustomAttributes(typeof(DataTablesVisibleAttribute), false).FirstOrDefault(); + var visible = visibleAttribute == null ? true : visibleAttribute.Visible; + columnList.Add(new ColDef() { Name = propertyInfo.Name, DisplayName = displayName, + Sortable = sortable, + Visible = visible, Type = propertyInfo.PropertyType }); } diff --git a/Mvc.JQuery.Datatables/DatatablesAtrtributes.cs b/Mvc.JQuery.Datatables/DatatablesAtrtributes.cs new file mode 100644 index 0000000..f924eb7 --- /dev/null +++ b/Mvc.JQuery.Datatables/DatatablesAtrtributes.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Mvc.JQuery.Datatables +{ + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] + public class DataTablesSortableAttribute : Attribute + { + public DataTablesSortableAttribute() : this(true) + { } + public DataTablesSortableAttribute(bool sortable) + { + this.Sortable = sortable; + } + + public bool Sortable { get; set; } + } + + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] + public class DataTablesVisibleAttribute : Attribute + { + public DataTablesVisibleAttribute() + : this(true) + { } + public DataTablesVisibleAttribute(bool visible) + { + this.Visible = visible; + } + + public bool Visible { get; set; } + } + +} diff --git a/Mvc.JQuery.Datatables/Mvc.JQuery.Datatables.csproj b/Mvc.JQuery.Datatables/Mvc.JQuery.Datatables.csproj index 7c493de..a65c2cb 100644 --- a/Mvc.JQuery.Datatables/Mvc.JQuery.Datatables.csproj +++ b/Mvc.JQuery.Datatables/Mvc.JQuery.Datatables.csproj @@ -53,6 +53,7 @@ +