我正在使用jQuery数据表在我的Web应用程序上显示sms统计信息,数据表用于简化分页,搜索和排序功能正常。但是,数据表上的列的一个是日期,这是我的返回json对象的action方法
public ActionResult LoadStatistics(jQueryDataTableParamModel param)
{
//jQueryDataTableParamModel param = new jQueryDataTableParamModel();
var allStats = _dbManager.GetSMSStatitistics();
var filteredStats = allStats;
if (!string.IsNullOrWhiteSpace(param.sSearch))
{
filteredStats = filteredStats.Where(x => x.StaffNameF.Contains(param.sSearch, false)
|| x.StaffNameS.Contains(param.sSearch, false)
|| x.SiteID.Contains(param.sSearch, false)).ToList();
}
return Json(new
{
iTotalRecords = allStats.Count(),
iTotalDisplayRecords = filteredStats.Count(),
aaData = filteredStats
}, JsonRequestBehavior.AllowGet);
}
现在在视图中我有以下代码,将数据加载到我的表中,
@section scripts
{
<script type="text/javascript">
$(function () {
$('#statsTable').dataTable({
bProcessing: true,
bServerSide: false,
bPaginate: true,
sAjaxSource: '@Url.Action("LoadStatistics", "Statistics")',
"aoColumnDefs": [
{ "aTargets": [0], "mData": 'StaffNameF' },
{ "aTargets": [1], "mData": 'StaffNameS' },
{ "aTargets": [2], "mData": 'StaffEmail', "sWidth": "300px" },
{ "aTargets": [3], "mData": 'SiteID' },
{ "aTargets": [4], "mData": 'DateTimeIn', "sWidth": "300px", "sFormat": "dd/MM/YYYY" },
{ "aTargets": [5], "mData": 'CellNumber' }]
});
});
</script>
这对所有列都有效,除了显示类似这样的日期&#34; / Date(1488924000000)/&#34;。我假设这是日期,但它只是需要格式化,我怎么能实现这一点,我已经尝试过&#34; sFormat&#34;:&#34; dd / MM / YYYY&#34;但没有运气,我不知道如何在桌面内做到这一点。
答案 0 :(得分:2)
Json没有Dates的标准格式。 您可以将日期作为字符串从服务器返回,也可以使用datatables列呈现来解析和格式化日期客户端。
从服务器端将日期作为格式化字符串返回:
return Json(new
{
iTotalRecords = allStats.Count(),
iTotalDisplayRecords = filteredStats.Count(),
aaData = filteredStats.Select(x => new {
StaffNameF = x.StaffNameF,
StaffNameS = x.StaffNameS,
StaffEmail = x.StaffEmail,
SiteID = x.SiteID,
DateTimeIn = x.DateTimeIn.ToString("dd/MM/YYYY"),
CellNumber = x.CellNumber })
}, JsonRequestBehavior.AllowGet);
或在客户端进行格式化:
<script type="text/javascript">
$(function () {
$('#statsTable').dataTable({
bProcessing: true,
bServerSide: false,
bPaginate: true,
sAjaxSource: '@Url.Action("LoadStatistics", "Statistics")',
"aoColumnDefs": [
{ "aTargets": [0], "mData": 'StaffNameF' },
{ "aTargets": [1], "mData": 'StaffNameS' },
{ "aTargets": [2], "mData": 'StaffEmail', "sWidth": "300px" },
{ "aTargets": [3], "mData": 'SiteID' },
{ "aTargets": [4], "mData": 'DateTimeIn', "sWidth": "300px", "mRender": function ( data, type, full ) {
var d = new Date();
d.setTime(1245398693390);
return d.getDate() + "/"+ (d.getMonth() + 1) + "/" + d.getFullYear();
},
{ "aTargets": [5], "mData": 'CellNumber' }]
});
});
</script>