Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public DataTablesResult<UserView> 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
});
}

Expand All @@ -65,7 +66,8 @@ private static List<User> 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
})
));
}
Expand Down Expand Up @@ -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
Expand All @@ -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; }
}


Expand Down
27 changes: 14 additions & 13 deletions Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 30 additions & 2 deletions Mvc.JQuery.Datatables/DataTableConfigVm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,6 +61,14 @@ public string JsOptionsString
}
}

public string ColumnDefsString
{
get
{
return convertColumnDefsToJson(Columns);
}
}

public bool ColumnFilter { get; set; }

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

private static string convertColumnDefsToJson(IEnumerable<ColDef> 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<dynamic>();

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<string, object> convertObjectToDictionary(object obj)
{
// Doing this way because RouteValueDictionary converts to Json in wrong format
Expand Down
9 changes: 9 additions & 0 deletions Mvc.JQuery.Datatables/DataTablesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ public static DataTableConfigVm DataTableVm<TController, TResult>(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
});
}
Expand Down
35 changes: 35 additions & 0 deletions Mvc.JQuery.Datatables/DatatablesAtrtributes.cs
Original file line number Diff line number Diff line change
@@ -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; }
}

}
1 change: 1 addition & 0 deletions Mvc.JQuery.Datatables/Mvc.JQuery.Datatables.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DatatablesAtrtributes.cs" />
<Compile Include="DataTablesData.cs" />
<Compile Include="DataTablesFilter.cs" />
<Compile Include="DataTablesHelper.cs" />
Expand Down