Skip to content

Commit 1f7484d

Browse files
committed
2 parents bacfb46 + c54d55f commit 1f7484d

File tree

8 files changed

+49
-7
lines changed

8 files changed

+49
-7
lines changed

Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
using System.ComponentModel;
44
using System.Globalization;
55
using System.Linq;
6+
using System.Reflection;
67
using System.Web;
78
using System.Web.Mvc;
89
using Mvc.JQuery.Datatables.Models;
10+
using Newtonsoft.Json.Linq;
911
using Resources;
1012

1113
namespace Mvc.JQuery.Datatables.Example.Controllers
@@ -80,10 +82,23 @@ public class UserView
8082
public string Position { get; set; }
8183

8284
[DataTablesFilter(DataTablesFilterType.DateTimeRange)]
85+
[DefaultToStartOfYear]
8386
public DateTime? Hired { get; set; }
8487

8588
public Numbers Number { get; set; }
8689

90+
[DataTablesExclude]
91+
public string ThisColumnIsExcluded { get { return "asdf"; } }
92+
8793

8894
}
95+
96+
public class DefaultToStartOfYearAttribute : DataTablesAttributeBase
97+
{
98+
public override void ApplyTo(ColDef colDef, PropertyInfo pi)
99+
{
100+
colDef.SearchCols = colDef.SearchCols ?? new JObject();
101+
colDef.SearchCols["sSearch"] = new DateTime(DateTime.Now.Year, 1, 1).ToString("g") + "~" + DateTimeOffset.Now.Date.AddDays(1).ToString("g");
102+
}
103+
}
89104
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@
6363
{
6464
options["oTableTools"] = new JRaw("{ 'sSwfPath': '//cdn.datatables.net/tabletools/2.2.1/swf/copy_csv_xls_pdf.swf' }");
6565
}
66-
options["fnServerData"] = new JRaw(" function(sSource, aoData, fnCallback) { $.ajax({ 'dataType': 'json', 'type': 'POST', 'url': sSource, 'data': aoData, 'success': fnCallback })}");
66+
options["fnServerData"] = new JRaw(
67+
"function(sSource, aoData, fnCallback) { " +
68+
" var ajaxOptions = { 'dataType': 'json', 'type': 'POST', 'url': sSource, 'data': aoData, 'success': fnCallback }; " +
69+
(Model.AjaxErrorHandler == null ? "" : ("ajaxOptions['error'] = " + Model.AjaxErrorHandler) + "; ") +
70+
" $.ajax(ajaxOptions);" +
71+
"}");
6772
options["aoColumnDefs"] = new JRaw(Model.ColumnDefsString);
6873
options["aoSearchCols"] = Model.SearchCols;
6974
if (Model.JsOptions.Any())
@@ -92,7 +97,7 @@
9297
9398
@if (Model.GlobalJsVariableName != null)
9499
{
95-
@Html.Raw("window['@Model.GlobalJsVariableName'] = dt;")
100+
@Html.Raw("window['" + Model.GlobalJsVariableName + "'] = dt;")
96101
}
97102
})();
98103
</script>

Mvc.JQuery.Datatables/DataTableConfigVm.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public DataTableConfigVm(string id, string ajaxUrl, IEnumerable<ColDef> columns)
3232
this.ShowPageSizes = true;
3333
this.TableTools = true;
3434
ColumnFilterVm = new ColumnFilterSettingsVm(this);
35+
AjaxErrorHandler =
36+
"function(jqXHR, textStatus, errorThrown)" +
37+
"{ " +
38+
"window.alert('error loading data: ' + textStatus + ' - ' + errorThrown); " +
39+
"console.log(arguments);" +
40+
"}";
3541
}
3642

3743
public bool ShowSearch { get; set; }
@@ -105,11 +111,12 @@ public string ColumnSortingString
105111
public string DrawCallback { get; set; }
106112
public LengthMenuVm LengthMenu { get; set; }
107113
public int? PageLength { get; set; }
108-
public string GlobalJsVariableName { get; private set; }
114+
public string GlobalJsVariableName { get; set; }
109115

110116
private bool _columnFilter;
111117

112118
public bool FixedLayout { get; set; }
119+
public string AjaxErrorHandler { get; set; }
113120

114121
public class _FilterOn<TTarget>
115122
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace Mvc.JQuery.Datatables
4+
{
5+
/// <summary>
6+
/// Prevent a public property from being included as a column in a DataTable row model
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.Property)]
9+
public class DataTablesExcludeAttribute : Attribute
10+
{
11+
12+
}
13+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Reference Include="System.Xml" />
7979
</ItemGroup>
8080
<ItemGroup>
81+
<Compile Include="DataTablesExcludeAttribute.cs" />
8182
<Compile Include="DataTablesFiltering.cs" />
8283
<Compile Include="DataTablesFilterAttribute.cs" />
8384
<Compile Include="LengthMenuVm.cs" />

Mvc.JQuery.Datatables/Reflection/DataTablesTypeInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal static DataTablesPropertyInfo[] Properties(Type type)
1515
return propertiesCache.GetOrAdd(type, t =>
1616
{
1717
var infos = from pi in t.GetProperties()
18+
where pi.GetCustomAttribute<DataTablesExcludeAttribute>() == null
1819
let attributes = (pi.GetCustomAttributes()).OfType<DataTablesAttributeBase>().ToArray()
1920
orderby attributes.OfType<DataTablesAttribute>().Select(a => a.Order as int?).SingleOrDefault() ?? int.MaxValue
2021
select new DataTablesPropertyInfo(pi, attributes);

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ turn IQueryables into datagrids
66
> Install-Package Mvc.JQuery.Datatables.Templates *
77
88

9-
[Demo site](http://mvcjquerydatatables.apphb.com/)
9+
[Demo site](http://aspdatatables.azurewebsites.net/)
1010
![Example](http://snag.gy/FQFdn.jpg)
1111

1212
- turn any IQueryable into a live datagrid. Tested with:
1313
- Linq To Objects
1414
- Entity Framework
1515
- Lucene.Net.Linq
1616
- wraps the very comprehensive jquery datatables plugin. Supported features:
17-
- Filtering (text, date range (with datepicker), number ranges, choose from dropdown, multiple values using checkboxes)
17+
- Filtering (text, date or datetime range (with datepicker), number ranges, choose from dropdown, multiple values using checkboxes)
1818
- Sorting (configurable per column)
1919
- Paging (choose page size options, or fix them)
2020
- Customer column rendering
2121
- Custom positioning of filters (e.g. you could move them above the table)
2222
- Localization
2323
- TableTools support (PDF/Excel export)
24-
- Attribute based configuration (optional)
24+
- Attribute based configuration (optional)
2525
- Can be run from dll
2626

2727
*skip this if using EmbeddedResourceVirtualPathProvider

0 commit comments

Comments
 (0)