Skip to content

Commit db9d450

Browse files
abelozerovBrent McSharry
authored and
Brent McSharry
committed
Added ability to DataTableVm class to set custom JS options to JS DataTables constructor
1 parent fee44b2 commit db9d450

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

Mvc.JQuery.Datatables.Example/Views/Home/Index.cshtml

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ can also use
5353
@using Mvc.JQuery.Datatables.Example.Controllers
5454
@{
5555
var vm = Html.DataTableVm("table-id", (HomeController h) => h.GetUsers(null));
56+
//vm.JsOptions.Add("iDisplayLength", 25);
57+
//vm.JsOptions.Add("aLengthMenu", new object[] { new[] {5, 10, 25, 250, -1} , new object[] { 5, 10, 25, 250, "All"} });
5658
vm.ColumnFilter = true;
5759
vm.FilterOn("Position").Select("Engineer", "Tester", "Manager")
5860
.FilterOn("Id").NumberRange();

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

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"success": fnCallback
5656
});
5757
}
58+
@Html.Raw(!string.IsNullOrWhiteSpace(Model.JsOptionsString) ? ", " + Model.JsOptionsString : "")
5859
});
5960
@if (Model.ColumnFilter)
6061
{

Mvc.JQuery.Datatables/DataTableVm.cs

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Web.Script.Serialization;
45

56
namespace Mvc.JQuery.Datatables
67
{
@@ -17,6 +18,8 @@ public static ColDef Create(string name, string p1, Type propertyType)
1718
}
1819
public class DataTableVm
1920
{
21+
IDictionary<string, object> m_JsOptions = new Dictionary<string, object>();
22+
2023
static DataTableVm()
2124
{
2225
DefaultTableClass = "table table-bordered table-striped";
@@ -45,6 +48,16 @@ public DataTableVm(string id, string ajaxUrl, IEnumerable<ColDef> columns)
4548

4649
public IEnumerable<ColDef> Columns { get; private set; }
4750

51+
public IDictionary<string, object> JsOptions { get { return m_JsOptions; } }
52+
53+
public string JsOptionsString
54+
{
55+
get
56+
{
57+
return (new JavaScriptSerializer()).Serialize((object)JsOptions).TrimStart('{').TrimEnd('}');
58+
}
59+
}
60+
4861
public bool ColumnFilter { get; set; }
4962

5063
public bool TableTools { get; set; }

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

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<Reference Include="System.ComponentModel.DataAnnotations" />
4141
<Reference Include="System.Core" />
4242
<Reference Include="System.Web" />
43+
<Reference Include="System.Web.Extensions" />
4344
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
4445
<Reference Include="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
4546
<Reference Include="System.Xml.Linq" />

0 commit comments

Comments
 (0)