Skip to content

Commit e89940d

Browse files
committed
Added error handling,
Fix bug with multiple requests being issued
1 parent 3d78133 commit e89940d

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

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

Lines changed: 13 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
@@ -24,6 +26,7 @@ public ActionResult JSUnitTests()
2426

2527
public DataTablesResult<UserView> GetUsers(DataTablesParam dataTableParam)
2628
{
29+
throw new Exception();
2730
return DataTablesResult.Create(FakeDatabase.Users.Select(user => new UserView()
2831
{
2932
Id = user.Id,
@@ -80,10 +83,20 @@ public class UserView
8083
public string Position { get; set; }
8184

8285
[DataTablesFilter(DataTablesFilterType.DateTimeRange)]
86+
[DefaultToStartOfYear]
8387
public DateTime? Hired { get; set; }
8488

8589
public Numbers Number { get; set; }
8690

8791

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

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

Lines changed: 6 additions & 1 deletion
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())

Mvc.JQuery.Datatables/DataTableConfigVm.cs

Lines changed: 7 additions & 0 deletions
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; }
@@ -110,6 +116,7 @@ public string ColumnSortingString
110116
private bool _columnFilter;
111117

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

114121
public class _FilterOn<TTarget>
115122
{

0 commit comments

Comments
 (0)