Skip to content

Commit c2a1ac6

Browse files
committed
Merge branch 'master' of github.com:mcintyre321/mvc.jquery.datatables
2 parents 463aada + 235e258 commit c2a1ac6

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,42 @@
3030
</tbody>
3131
</table>
3232
<script type="text/javascript">
33-
$(document).ready(function() {
34-
var $table = $('#@Model.Id');
35-
var dt = $table.dataTable({
36-
"bProcessing": true,
37-
"bStateSave": true,
38-
"bServerSide": true,
39-
"bFilter": @Model.ShowSearch.ToString().ToLower(),
40-
"sDom": '@Html.Raw(Model.Dom)',
41-
"aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
42-
"bAutoWidth": @Model.AutoWidth.ToString().ToLowerInvariant(),
43-
"sAjaxSource": "@Html.Raw(Model.AjaxUrl)", @Html.Raw(Model.TableTools ? "\"oTableTools\" : { \"sSwfPath\": \"/content/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf\" }," : "")
44-
"fnServerData": function(sSource, aoData, fnCallback) {
45-
$.ajax({
46-
"dataType": 'json',
47-
"type": "POST",
48-
"url": sSource,
49-
"data": aoData,
50-
"success": fnCallback
33+
(function setDataTable() {
34+
if(!window.jQuery) {
35+
setTimeout(setDataTable, 20);
36+
return;
37+
}
38+
$(document).ready(function() {
39+
var $table = $('#@Model.Id');
40+
var dt = $table.dataTable({
41+
"bProcessing": true,
42+
"bStateSave": true,
43+
"bServerSide": true,
44+
"bFilter": @Model.ShowSearch.ToString().ToLower(),
45+
"sDom": '@Html.Raw(Model.Dom)',
46+
"aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
47+
"bAutoWidth": @Model.AutoWidth.ToString().ToLowerInvariant(),
48+
"sAjaxSource": "@Html.Raw(Model.AjaxUrl)", @Html.Raw(Model.TableTools ? "\"oTableTools\" : { \"sSwfPath\": \"/content/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf\" }," : "")
49+
"fnServerData": function(sSource, aoData, fnCallback) {
50+
$.ajax({
51+
"dataType": 'json',
52+
"type": "POST",
53+
"url": sSource,
54+
"data": aoData,
55+
"success": fnCallback
56+
});
57+
}
58+
@Html.Raw(!string.IsNullOrWhiteSpace(Model.JsOptionsString) ? ", " + Model.JsOptionsString : "")
59+
});
60+
@if (Model.ColumnFilter)
61+
{
62+
<text>
63+
dt.columnFilter({
64+
sPlaceHolder: "head:before",
65+
aoColumns: [@Html.Raw(Model.ColumnFiltersString)]
5166
});
67+
</text>
5268
}
5369
});
54-
@if (Model.ColumnFilter)
55-
{
56-
<text>
57-
dt.columnFilter({
58-
sPlaceHolder: "head:before",
59-
aoColumns: [@Html.Raw(Model.ColumnFiltersString)]
60-
});
61-
</text>
62-
}
63-
});
70+
})();
6471
</script>

Mvc.JQuery.Datatables/DataTableVm.cs

Lines changed: 13 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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" />

README renamed to README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ The code here is based on code from http://code.google.com/p/datatables-asp-mvc/
66

77
If you have a feature request, bug, or a patch, please could you add an example page on a fork demonstrating the problem or feature. Thanks!
88

9+
[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=mcintyre321&url=https://github.com/mcintyre321/mvc.jquery.datatables&title=Mvc.JQuery.DataTables&language=&tags=github&category=software)

0 commit comments

Comments
 (0)