Skip to content

Commit 1f5c83a

Browse files
committed
Added length menu options
1 parent 6bc9d48 commit 1f5c83a

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131
//.FilterOn("Number").CheckBoxes(Enum.GetNames(typeof(Numbers)));
3232
vm.StateSave = true;
3333
//vm.DrawCallback = "drawCallback";
34-
34+
35+
//you can change the page length options...
36+
vm.LengthMenu = LengthMenuVm.Default();
37+
vm.LengthMenu.RemoveAll(t => t.Item2 == 5);
38+
vm.PageLength = 25; //... and set a default
39+
3540
if (Request.QueryString["lang"] == "de")
3641
{
3742
//vm.Language = "{ 'sUrl': '" + Url.Content("~/Content/jquery.dataTables.lang.de-DE.txt") + "' }";

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</Reference>
4343
<Reference Include="System" />
4444
<Reference Include="System.Core" />
45+
<Reference Include="System.Web" />
4546
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
4647
<SpecificVersion>False</SpecificVersion>
4748
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.1.0\lib\net45\System.Web.Helpers.dll</HintPath>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@
4545
"bServerSide": true,
4646
"bFilter": @Model.ShowSearch.ToString().ToLower(),
4747
"sDom": '@Html.Raw(Model.Dom)',
48-
"aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
48+
@if (Model.LengthMenu != null) { <text>"lengthMenu": @Html.Raw(Model.LengthMenu),</text> }
49+
@if (Model.PageLength.HasValue)
50+
{
51+
<text>"pageLength": @Html.Raw(Model.PageLength),</text>
52+
}
4953
"bAutoWidth": @Model.AutoWidth.ToString().ToLowerInvariant(),
5054
"sAjaxSource": "@Html.Raw(Model.AjaxUrl)", @Html.Raw(Model.TableTools ? "\"oTableTools\" : { \"sSwfPath\": \"//cdn.datatables.net/tabletools/2.2.1/swf/copy_csv_xls_pdf.swf\" }," : "")
5155
"fnServerData": function(sSource, aoData, fnCallback) {

Mvc.JQuery.Datatables/DataTableConfigVm.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public string ColumnSortingString
106106
public string Language { get; set; }
107107

108108
public string DrawCallback { get; set; }
109-
109+
public LengthMenuVm LengthMenu { get; set; }
110+
public int? PageLength { get; set; }
110111

111112
private bool _columnFilter;
112113

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Mvc.JQuery.Datatables
6+
{
7+
public class LengthMenuVm : List<Tuple<string, int>>
8+
{
9+
public LengthMenuVm()
10+
{
11+
12+
13+
}
14+
/// <summary>
15+
/// Create a lengthmenuvm with options for 5,10,25,50,All
16+
/// </summary>
17+
/// <returns></returns>
18+
public static LengthMenuVm Default()
19+
{
20+
return new LengthMenuVm {Tuple.Create("5", 5), Tuple.Create("10", 10), Tuple.Create("25", 25), Tuple.Create("50", 50), Tuple.Create("All", -1)};
21+
}
22+
23+
public override string ToString()
24+
{
25+
return "[[" + string.Join(", ", this.Select(pair => pair.Item2)) + "],[\"" + string.Join("\", \"", this.Select(pair => pair.Item1.Replace("\"", "\"\""))) + "\"]]";
26+
27+
}
28+
}
29+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<ItemGroup>
8181
<Compile Include="DataTablesFiltering.cs" />
8282
<Compile Include="DataTablesFilterAttribute.cs" />
83+
<Compile Include="LengthMenuVm.cs" />
8384
<Compile Include="Models\ColDef.cs" />
8485
<Compile Include="ColumnFilterSettingsVm.cs" />
8586
<Compile Include="DataTablesAttribute.cs" />

0 commit comments

Comments
 (0)