Skip to content

Commit 786e5fb

Browse files
committed
Use jobject to build args
1 parent 1f5c83a commit 786e5fb

File tree

4 files changed

+48
-44
lines changed

4 files changed

+48
-44
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
<Private>True</Private>
4141
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
4242
</Reference>
43+
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
44+
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
45+
</Reference>
4346
<Reference Include="System" />
4447
<Reference Include="System.Core" />
4548
<Reference Include="System.Web" />
Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
@using System.Data.Odbc
22
@using System.Linq
33
@using Mvc.JQuery.Datatables
4+
@using Newtonsoft.Json
5+
@using Newtonsoft.Json.Linq
46
@model DataTableConfigVm
57
<table id="@Model.Id" class="display @(Model.TableClass ?? DataTableConfigVm.DefaultTableClass ?? "")" >
68
<thead>
@@ -38,50 +40,54 @@
3840
return;
3941
}
4042
var $table = $('#@Model.Id');
41-
var dt = $table.dataTable({
42-
"aaSorting": @Html.Raw(Model.ColumnSortingString),
43-
"bProcessing": true,
44-
"bStateSave": @Html.Raw(Model.StateSave ? "true" : "false"),
45-
"bServerSide": true,
46-
"bFilter": @Model.ShowSearch.ToString().ToLower(),
47-
"sDom": '@Html.Raw(Model.Dom)',
48-
@if (Model.LengthMenu != null) { <text>"lengthMenu": @Html.Raw(Model.LengthMenu),</text> }
49-
@if (Model.PageLength.HasValue)
43+
44+
@{
45+
var options = new JObject();
46+
options["aaSorting"] = new JRaw(Model.ColumnSortingString);
47+
options["bProcessing"] = true;
48+
options["bSaveState"] = Model.StateSave;
49+
options["bFilter"] = Model.ShowSearch;
50+
options["sDom"] = Model.Dom;
51+
52+
if (Model.LengthMenu != null)
5053
{
51-
<text>"pageLength": @Html.Raw(Model.PageLength),</text>
54+
options["lengthMenu"] = new JRaw(Model.LengthMenu);
5255
}
53-
"bAutoWidth": @Model.AutoWidth.ToString().ToLowerInvariant(),
54-
"sAjaxSource": "@Html.Raw(Model.AjaxUrl)", @Html.Raw(Model.TableTools ? "\"oTableTools\" : { \"sSwfPath\": \"//cdn.datatables.net/tabletools/2.2.1/swf/copy_csv_xls_pdf.swf\" }," : "")
55-
"fnServerData": function(sSource, aoData, fnCallback) {
56-
$.ajax({
57-
"dataType": 'json',
58-
"type": "POST",
59-
"url": sSource,
60-
"data": aoData,
61-
"success": fnCallback
62-
});
63-
},
64-
"aoColumnDefs" : @Html.Raw(Model.ColumnDefsString),
65-
"aoSearchCols": @Html.Raw(Model.ColumnInitialSearchString)
66-
@Html.Raw(!string.IsNullOrWhiteSpace(Model.JsOptionsString) ? ", " + Model.JsOptionsString : "")
67-
@if (!string.IsNullOrEmpty(Model.Language))
56+
if (Model.PageLength.HasValue)
6857
{
69-
<text>
70-
,"oLanguage": @Html.Raw(@Model.Language)
71-
</text>
58+
options["pageLength"] = Model.PageLength;
7259
}
73-
@if (!string.IsNullOrEmpty(Model.DrawCallback))
60+
options["bAutoWidth"] = Model.AutoWidth;
61+
options["sAjaxSource"] = Model.AjaxUrl;
62+
if (Model.TableTools)
7463
{
75-
<text>
76-
,"fnDrawCallback": @Html.Raw(Model.DrawCallback)
77-
</text>
64+
options["oTableTools"] = new JRaw("{ 'sSwfPath': '//cdn.datatables.net/tabletools/2.2.1/swf/copy_csv_xls_pdf.swf' }");
7865
}
79-
});
80-
@if (Model.ColumnFilter)
66+
options["fnServerData"] = new JRaw(" function(sSource, aoData, fnCallback) { $.ajax({ 'dataType': 'json', 'type': 'POST', 'url': sSource, 'data': aoData, 'success': fnCallback })}");
67+
options["aoColumnDefs"] = new JRaw(Model.ColumnDefsString);
68+
options["aoSearchCols"] = new JRaw(Model.ColumnInitialSearchString);
69+
if (Model.JsOptions.Any())
8170
{
82-
<text>
83-
dt.columnFilter(@Html.Raw(Model.ColumnFilterVm.ToString()));
84-
</text>
71+
foreach (var jsOption in Model.JsOptions)
72+
{
73+
options[jsOption.Key] = new JRaw(jsOption.Value);
74+
}
8575
}
76+
if (!string.IsNullOrWhiteSpace(Model.Language))
77+
{
78+
options["oLanguage"] = new JRaw(Model.Language);
79+
}
80+
if (!string.IsNullOrWhiteSpace(Model.DrawCallback))
81+
{
82+
options["fnDrawCallback"] = new JRaw(Model.DrawCallback);
83+
}
84+
85+
}
86+
87+
var dt = $table.dataTable(@Html.Raw(options.ToString(Formatting.Indented)));
88+
@if (Model.ColumnFilter)
89+
{
90+
@Html.Raw("dt.columnFilter(" + Model.ColumnFilterVm + ");")
91+
}
8692
})();
8793
</script>

Mvc.JQuery.Datatables.Templates/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<package id="Microsoft.AspNet.Razor" version="3.1.0" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebPages" version="3.1.0" targetFramework="net45" />
66
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
7+
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net45" />
78
</packages>

Mvc.JQuery.Datatables/DataTableConfigVm.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ public DataTableConfigVm(string id, string ajaxUrl, IEnumerable<ColDef> columns)
4343

4444
public IDictionary<string, object> JsOptions { get { return m_JsOptions; } }
4545

46-
public string JsOptionsString
47-
{
48-
get
49-
{
50-
return ConvertDictionaryToJsonBody(JsOptions);
51-
}
52-
}
46+
5347

5448
public string ColumnDefsString
5549
{

0 commit comments

Comments
 (0)