jqGrid显示13位数字而不是格式化日期

发布于 2024-12-01 08:42:45 字数 3748 浏览 0 评论 0原文

我的 jqGrid 在初始网格负载上显示 (1303153262000)。但是,当对列进行排序时,日期格式正确为(8 月 18 日上午 11:43)。有没有办法让网格在加载时自动进行这种格式化?

这正是从服务器返回的日期的样子 (2011-04-18 19:01:02)。

这是我正在使用的 jqGrid 列模型:

jQuery("#list4").jqGrid({
datatype: "local",
height: 250,
colNames:['Original ID','Date Created', 'Type', 'Title'],
colModel:[  
{name:'Id',index:'Id'},
{name:'cDate',index:'cDate', width:120, sorttype:'date', formatter:'date',     formatoptions: {srcformat:'M d h:i', newformat:'M d g:i A'}},  
{name:'xData',index:'xData', width:120}, 
{name:'zData',index:'zData', width:140}
],
rowNum: 1000,
multiselect: true,
toolbar: [true, "top"]
});

任何帮助将不胜感激!谢谢。

编辑:

@Oleg 这是数据在服务器端的样子:

2011-04-18 19:01:02
2011-04-18 19:01:28
2011-01-07 01:00:20
2011-04-18 19:01:02
2011-04-18 19:01:28
2011-01-07 01:00:20
2011-06-29 22:20:29
2011-06-29 21:47:55

这是返回到 jqGrid 时的样子:

Test: 1303153262000
Test: 1303153288000
Test: 1294362020000
Test: 1303153262000
Test: 1303153288000
Test: 1294362020000

我是否必须使用返回的结果创建一个新数组,并使用一个函数来更改纪元日期到我想要的日期格式?必须这样做是没有意义的,因为 jqGrid 显然具有该功能,但仅当用户单击排序时才有效。有没有办法让 jqGrid 在页面加载时执行此操作?

我把它从日食中拉出来。我正在使用一个名为 NotesArray 的列表/数组。这是从服务器返回的数组:

NotesArray:{Original_Created_Date__c=2011-07-01 01:06:55, Title__c=Title of message 1 00001073: 06/30/2011 18:07:07, Type__c=Task, Id=a0D8000000Na0Q0EAJ, Originating_Id__c=00T8000001nuEzPEAU}
NotesArray:{Original_Created_Date__c=2011-06-30 22:14:48, Title__c=Title of message 2 00001072: 06/30/2011 15:15:00, Type__c=Task, Id=a0D8000000Na0Q1EAJ, Originating_Id__c=00T8000001nuCccEAE}
NotesArray:{Original_Created_Date__c=2011-06-30 21:24:47, Title__c=Title of message 3 00001071: 06/30/2011 14:25:00, Type__c=Task, Id=a0D8000000Na0Q2EAJ, Originating_Id__c=00T8000001nuB5cEAE}
NotesArray:{Original_Created_Date__c=2011-06-30 21:19:48, Title__c=Title of message 4 00001070: 06/30/2011 14:20:00, Type__c=Task, Id=a0D8000000Na0Q3EAJ, Originating_Id__c=00T8000001nuBFiEAM}
NotesArray:{Original_Created_Date__c=2011-06-30 21:14:47, Title__c=Title of message 5 00001069: 06/30/2011 14:15:00, Type__c=Task, Id=a0D8000000Na0Q4EAJ, Originating_Id__c=00T8000001nuAzDEAU}
NotesArray:{Original_Created_Date__c=2011-06-30 21:09:47, Title__c=Title of message 6 00001068: 06/30/2011 14:10:00, Type__c=Task, Id=a0D8000000Na0Q5EAJ, Originating_Id__c=00T8000001nuASSEA2}

将返回更改为 JSON:

{"rows": [{"ObjectType": "Event","CreatedDate": "2011-01-03 09:16:51","ActivityType": "Call","OriginalId": "00U8000000Elxb4EAB","Title": "TEST EVENT"},{"ObjectType": "Event","CreatedDate": "2011-01-03 08:53:22","ActivityType": "Meeting","OriginalId": "00U8000000ElxY9EAJ","Title": "sadfasdf"},{"ObjectType": "Event","CreatedDate": "2011-01-03 08:51:04","ActivityType": "Meeting","OriginalId": "00U8000000ElxXkEAJ","Title": "werwrasdf"},{"ObjectType": "Task","CreatedDate": "2011-01-06 14:42:43","ActivityType": "Call","OriginalId": "00T8000001bce9VEAQ","Title": "test"},{"ObjectType": "Task","CreatedDate": "2011-01-03 08:50:41","ActivityType": "Meeting","OriginalId": "00T8000001aiqAXEAY","Title": "asdfasdfasf"},{"ObjectType": "Task","CreatedDate": "2011-01-03 08:50:29","ActivityType": "Call","OriginalId": "00T8000001aiqAcEAI","Title": "asdfsaf"},{"ObjectType": "Task","CreatedDate": "2011-01-03 08:52:55","ActivityType": "Call","OriginalId": "00T8000001aiqBuEAI","Title": "asdfsaf"},{"ObjectType": "Note","CreatedDate": "2011-08-22 15:17:11","ActivityType": "Note","OriginalId": "0028000000T03CUAAZ","Title": "Mondays Best"},{"ObjectType": "Note","CreatedDate": "2011-08-18 11:43:25","ActivityType": "Note","OriginalId": "0028000000SzwIjAAJ","Title": "Note Test"}]}

My jqGrid is showing (1303153262000) on the initial grid load. However when the column is sorted the date is correctly formatted to (Aug 18 11:43 AM). Is there a way to get the grid to do this formatting automatically when it loads?

This is exactly what the date looks like as it is returned from the server (2011-04-18 19:01:02).

Here is the jqGrid column model i am using:

jQuery("#list4").jqGrid({
datatype: "local",
height: 250,
colNames:['Original ID','Date Created', 'Type', 'Title'],
colModel:[  
{name:'Id',index:'Id'},
{name:'cDate',index:'cDate', width:120, sorttype:'date', formatter:'date',     formatoptions: {srcformat:'M d h:i', newformat:'M d g:i A'}},  
{name:'xData',index:'xData', width:120}, 
{name:'zData',index:'zData', width:140}
],
rowNum: 1000,
multiselect: true,
toolbar: [true, "top"]
});

Any help would be greatly appreciated! Thanks.

Edit:

@Oleg here is what the data looks like on the server side:

2011-04-18 19:01:02
2011-04-18 19:01:28
2011-01-07 01:00:20
2011-04-18 19:01:02
2011-04-18 19:01:28
2011-01-07 01:00:20
2011-06-29 22:20:29
2011-06-29 21:47:55

And here is what it looks like when returned into the jqGrid:

Test: 1303153262000
Test: 1303153288000
Test: 1294362020000
Test: 1303153262000
Test: 1303153288000
Test: 1294362020000

Am i going to have to create a new array with the returned results with a function to change the epoch date to the date format i want? Having to do so would not make sense as jqGrid obviously has that functionality, but only when a user clicks sort. Is there a way to make jqGrid do this when the page loads?

I pulled this from eclipse. I am using a list/array called NotesArray. This is the array that is returned from the server:

NotesArray:{Original_Created_Date__c=2011-07-01 01:06:55, Title__c=Title of message 1 00001073: 06/30/2011 18:07:07, Type__c=Task, Id=a0D8000000Na0Q0EAJ, Originating_Id__c=00T8000001nuEzPEAU}
NotesArray:{Original_Created_Date__c=2011-06-30 22:14:48, Title__c=Title of message 2 00001072: 06/30/2011 15:15:00, Type__c=Task, Id=a0D8000000Na0Q1EAJ, Originating_Id__c=00T8000001nuCccEAE}
NotesArray:{Original_Created_Date__c=2011-06-30 21:24:47, Title__c=Title of message 3 00001071: 06/30/2011 14:25:00, Type__c=Task, Id=a0D8000000Na0Q2EAJ, Originating_Id__c=00T8000001nuB5cEAE}
NotesArray:{Original_Created_Date__c=2011-06-30 21:19:48, Title__c=Title of message 4 00001070: 06/30/2011 14:20:00, Type__c=Task, Id=a0D8000000Na0Q3EAJ, Originating_Id__c=00T8000001nuBFiEAM}
NotesArray:{Original_Created_Date__c=2011-06-30 21:14:47, Title__c=Title of message 5 00001069: 06/30/2011 14:15:00, Type__c=Task, Id=a0D8000000Na0Q4EAJ, Originating_Id__c=00T8000001nuAzDEAU}
NotesArray:{Original_Created_Date__c=2011-06-30 21:09:47, Title__c=Title of message 6 00001068: 06/30/2011 14:10:00, Type__c=Task, Id=a0D8000000Na0Q5EAJ, Originating_Id__c=00T8000001nuASSEA2}

Changed the return to JSON:

{"rows": [{"ObjectType": "Event","CreatedDate": "2011-01-03 09:16:51","ActivityType": "Call","OriginalId": "00U8000000Elxb4EAB","Title": "TEST EVENT"},{"ObjectType": "Event","CreatedDate": "2011-01-03 08:53:22","ActivityType": "Meeting","OriginalId": "00U8000000ElxY9EAJ","Title": "sadfasdf"},{"ObjectType": "Event","CreatedDate": "2011-01-03 08:51:04","ActivityType": "Meeting","OriginalId": "00U8000000ElxXkEAJ","Title": "werwrasdf"},{"ObjectType": "Task","CreatedDate": "2011-01-06 14:42:43","ActivityType": "Call","OriginalId": "00T8000001bce9VEAQ","Title": "test"},{"ObjectType": "Task","CreatedDate": "2011-01-03 08:50:41","ActivityType": "Meeting","OriginalId": "00T8000001aiqAXEAY","Title": "asdfasdfasf"},{"ObjectType": "Task","CreatedDate": "2011-01-03 08:50:29","ActivityType": "Call","OriginalId": "00T8000001aiqAcEAI","Title": "asdfsaf"},{"ObjectType": "Task","CreatedDate": "2011-01-03 08:52:55","ActivityType": "Call","OriginalId": "00T8000001aiqBuEAI","Title": "asdfsaf"},{"ObjectType": "Note","CreatedDate": "2011-08-22 15:17:11","ActivityType": "Note","OriginalId": "0028000000T03CUAAZ","Title": "Mondays Best"},{"ObjectType": "Note","CreatedDate": "2011-08-18 11:43:25","ActivityType": "Note","OriginalId": "0028000000SzwIjAAJ","Title": "Note Test"}]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦屿孤独相伴 2024-12-08 08:42:45

如果来自服务器的日期为“2011-04-18 19:01:02”,为什么您使用 'M dh:i' 而不是 'Ymd H:i:s'< /code> 作为 srcformat 的值?

如果使用

formatoptions: {srcformat:'Y-m-d H:i:s', newformat:'M d g:i A'}

不能解决您的问题,您应该包含用于填充“cDate”列的输入数据的确切示例。

更新:您可以加载直接在 jqGrid 中发布的 JSON 数据。请参阅此处修改后的演示。您需要的只是在 jqGrid 中使用列上的名称(如 JSON 数据中的名称),另外您应该使用

loadonce: true,
jsonReader: {
    repeatitems: false,
    id: "OriginalId",
    root: "rows",
    page: function (obj) { return 1; },
    total: function (obj) { return 1; },
    records: function (obj) { return obj.length; }
}

在演示中我又添加了一列,因为我不知道是 ObjectType 还是 ActivityType 您希望显示在'Type' 列中。您可以从 jqGrid(不适用于 JSON)中删除任何不需要显示的列。例如,如果用户对 OriginalId 列中的内容不感兴趣,您可以从网格中删除相应的列。

If the date come from the server as '2011-04-18 19:01:02' why you use 'M d h:i' instead of 'Y-m-d H:i:s' as the value of srcformat?

If the usage of

formatoptions: {srcformat:'Y-m-d H:i:s', newformat:'M d g:i A'}

will not solve your problem you should include the exact example of the input data which you use to fill the 'cDate' column.

UPDATED: You can load the JSON data which you posted directly in the jqGrid. See the modified demo here. What you need is just to use in the jqGrid the names on columns like in the JSON data and additionally you should use

loadonce: true,
jsonReader: {
    repeatitems: false,
    id: "OriginalId",
    root: "rows",
    page: function (obj) { return 1; },
    total: function (obj) { return 1; },
    records: function (obj) { return obj.length; }
}

In the demo I added one more column because I don't know whether ObjectType or ActivityType you want display in the 'Type' column. You can remove from jqGrid (not for JSON) any column which you not need to display. For example if the contain from the OriginalId column are not interesting for the user you can remove the corresponding column from the grid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文