Skip to content

Commit

Permalink
#33 Using IsoDateTimeConverter for properly formatting date in Kendo …
Browse files Browse the repository at this point in the history
…grids
  • Loading branch information
RomanovM committed Mar 1, 2017
1 parent 9f0fcad commit 2fa14b7
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 48 deletions.
11 changes: 11 additions & 0 deletions src/Libraries/Nop.Core/Domain/Common/AdminAreaSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,40 @@ public class AdminAreaSettings : ISettings
/// Default grid page size
/// </summary>
public int DefaultGridPageSize { get; set; }

/// <summary>
/// Popup grid page size (for popup pages)
/// </summary>
public int PopupGridPageSize { get; set; }

/// <summary>
/// A comma-separated list of available grid page sizes
/// </summary>
public string GridPageSizes { get; set; }

/// <summary>
/// Additional settings for rich editor
/// </summary>
public string RichEditorAdditionalSettings { get; set; }

/// <summary>
///A value indicating whether to javascript is supported in rcih editor
/// </summary>
public bool RichEditorAllowJavaScript { get; set; }

/// <summary>
/// Gets or sets a value indicating whether advertisements (news) should be hidden
/// </summary>
public bool HideAdvertisementsOnAdminArea { get; set; }

/// <summary>
/// Gets or sets title of last news (admin area)
/// </summary>
public string LastNewsTitleAdminArea { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to use IsoDateTimeConverter in Json results (used for avoiding issue with dates in KendoUI grids)
/// </summary>
public bool UseIsoDateTimeConverterInJson { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5934,7 +5934,8 @@ protected virtual void InstallSettings()
PopupGridPageSize = 10,
GridPageSizes = "10, 15, 20, 50, 100",
RichEditorAdditionalSettings = null,
RichEditorAllowJavaScript = false
RichEditorAllowJavaScript = false,
UseIsoDateTimeConverterInJson = true
});


Expand Down
53 changes: 53 additions & 0 deletions src/Presentation/Nop.Web.Framework/Mvc/ConverterJsonResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Web.Mvc;
using Newtonsoft.Json;
using Nop.Core;

namespace Nop.Web.Framework.Mvc
{
/// <summary>
/// Represents custom JsonResult with using Json converters
/// </summary>
public class ConverterJsonResult : JsonResult
{
#region Fields

private readonly JsonConverter[] _converters;

#endregion

#region Ctor

public ConverterJsonResult(params JsonConverter[] converters)
{
_converters = converters;
}

#endregion

#region Methods

/// <summary>
/// Enables processing of the result of an action method
/// </summary>
/// <param name="context">The context within which the result is executed</param>
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");

if (context.HttpContext == null || context.HttpContext.Response == null)
return;

context.HttpContext.Response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : MimeTypes.ApplicationJson;
if (ContentEncoding != null)
context.HttpContext.Response.ContentEncoding = ContentEncoding;

//serialize data with any converters
if (Data != null)
context.HttpContext.Response.Write(JsonConvert.SerializeObject(Data, _converters));
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<Compile Include="Menu\SiteMapNode.cs" />
<Compile Include="Menu\XmlSiteMap.cs" />
<Compile Include="Mvc\ActionConfirmationModel.cs" />
<Compile Include="Mvc\ConverterJsonResult.cs" />
<Compile Include="Mvc\NoTrimAttribute.cs" />
<Compile Include="Mvc\DependencyRegistrarExtensions.cs" />
<Compile Include="Mvc\NullJsonResult.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json.Converters;
using Nop.Core;
using Nop.Core.Domain.Common;
using Nop.Core.Infrastructure;
using Nop.Services.Localization;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Mvc;
using Nop.Web.Framework.Security;

namespace Nop.Admin.Controllers
Expand Down Expand Up @@ -100,18 +103,23 @@ protected virtual void SaveSelectedTabName(string tabName = "", bool persistForT
/// <param name="behavior">The JSON request behavior</param>
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
//Json fix issue with dates in KendoUI grid
//use json with IsoDateTimeConverter
var result = EngineContext.Current.Resolve<AdminAreaSettings>().UseIsoDateTimeConverterInJson
? new ConverterJsonResult(new IsoDateTimeConverter()) : new JsonResult();

result.Data = data;
result.ContentType = contentType;
result.ContentEncoding = contentEncoding;
result.JsonRequestBehavior = behavior;

//Json fix for admin area
//sometime our entities have big text values returned (e.g. product desriptions)
//of course, we can set and return them as "empty" (we already do it so). Furthermore, it's a perfoemance optimization
//but it's better to avoid exceptions for other entities and allow maximum JSON length
return new JsonResult()
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior,
MaxJsonLength = int.MaxValue
};
result.MaxJsonLength = int.MaxValue;

return result;
//return base.Json(data, contentType, contentEncoding, behavior);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ public virtual ActionResult List(DataSourceRequest command)
}),
Total = blogPosts.TotalCount
};
return new JsonResult
{
Data = gridModel
};

return Json(gridModel);
}

public virtual ActionResult Create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ public virtual ActionResult List(DataSourceRequest command)
Data = customerRoles.Select(PrepareCustomerRoleModel),
Total = customerRoles.Count()
};
return new JsonResult
{
Data = gridModel
};
}

return Json(gridModel);
}

public virtual ActionResult Create()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ public virtual ActionResult List(DataSourceRequest command)
Total = emailAccountModels.Count()
};

return new JsonResult
{
Data = gridModel
};
}
return Json(gridModel);
}

public virtual ActionResult MarkAsDefaultEmail(int id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ public virtual ActionResult List(DataSourceRequest command)
Data = languages.Select(x => x.ToModel()),
Total = languages.Count()
};
return new JsonResult
{
Data = gridModel
};

return Json(gridModel);
}

public virtual ActionResult Create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1209,11 +1209,9 @@ public virtual ActionResult OrderList(DataSourceRequest command, OrderListModel
aggregatortax = _priceFormatter.FormatPrice(reportSummary.SumTax, true, false),
aggregatortotal = _priceFormatter.FormatPrice(reportSummary.SumOrders, true, false)
};
return new JsonResult
{
Data = gridModel
};
}

return Json(gridModel);
}

[HttpPost, ActionName("List")]
[FormValueRequired("go-to-order-by-number")]
Expand Down Expand Up @@ -2993,11 +2991,9 @@ public virtual ActionResult ShipmentListSelect(DataSourceRequest command, Shipme
Data = shipments.Select(shipment => PrepareShipmentModel(shipment, false)),
Total = shipments.TotalCount
};
return new JsonResult
{
Data = gridModel
};
}

return Json(gridModel);
}

[HttpPost]
public virtual ActionResult ShipmentsByOrder(int orderId, DataSourceRequest command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ public virtual ActionResult QueuedEmailList(DataSourceRequest command, QueuedEma
}),
Total = queuedEmails.TotalCount
};
return new JsonResult
{
Data = gridModel
};
}

return Json(gridModel);
}

[HttpPost, ActionName("List")]
[FormValueRequired("go-to-email-by-number")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,13 @@ public virtual ActionResult ProductTemplates(DataSourceRequest command)
var templatesModel = _productTemplateService.GetAllProductTemplates()
.Select(x => x.ToModel())
.ToList();
var model = new DataSourceResult
var gridModel = new DataSourceResult
{
Data = templatesModel,
Total = templatesModel.Count
};

return new JsonResult
{
Data = model
};
return Json(gridModel);
}

[HttpPost]
Expand Down
10 changes: 9 additions & 1 deletion upgradescripts/3.80-the next version/upgrade.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4662,4 +4662,12 @@ END

CLOSE cursor_defaultGroup
DEALLOCATE cursor_defaultGroup
GO
GO

--new setting
IF NOT EXISTS (SELECT 1 FROM [Setting] WHERE [name] = N'adminareasettings.useisodatetimeconverterinjson')
BEGIN
INSERT [Setting] ([Name], [Value], [StoreId])
VALUES (N'adminareasettings.useisodatetimeconverterinjson', N'True', 0)
END
GO

0 comments on commit 2fa14b7

Please sign in to comment.