【问题标题】:jqgrid datetime column sorting in 24 hour formatjqgrid datetime 列以 24 小时格式排序
【发布时间】:2011-03-11 20:46:30
【问题描述】:

我正在使用来自 spring MVC 的 json 对象,通过使用 json reader 填充 jqgrid。

{
    "rows": [
        {
            "id": 1,
            "startTime": "Mar 4 08:00",
            "projectId": "Proj_001",
            "customerId": "Cust1",
            "divisionId": "Div1" 
        },
        {
            "id": 2,
            "startTime": "Mar 5 15:00",
            "projectId": "Proj_001",
            "customerId": "Cust1",
            "divisionId": "Div1" 
        },
        {
            "id": 3,
            "startTime": "Apr 14 22:00",
            "projectId": "Proj_001",
            "customerId": "Cust1",
            "divisionId": "Div1" 
        } 
    ]
}

我需要排序

开始时间

以 24 小时时间格式出现的列(如上面的 json 对象)。我尝试了一些解决方法,但仍然无法成功。

jqgrid代码-

{name:'startTime', index:'startTime', width:90,align:"left",sorttype:'datetime',datefmt:'M d, Y at h:i',sortable: true,editrules:{date:true} },

我不会在这里使用 AM/PM 符号。我在 jqgrid.base.js 中看到它使用 AM/PM 符号进行排序。任何人都可以对此有所了解吗?我找不到任何 jqgrid 示例来使用具有 24 小时时间格式的日期来进行排序。先感谢您。

我的 jqgrid 代码 -

jq(function(){
    jq("#testgrid").jqGrid({
        url:"/getall.html",
        datatype: 'json',
        mtype: 'GET',
        colNames:['Id','Start Time','Cust.Id','Div.Id','Proj.Id'],
        colModel :[
            {name:'id', index:'id', width:90,align:"left",hidden: true},
            {name:'startTime', index:'startTime', width:90, align:"left",
             sorttype:'datetime', datefmt:'M d h:i',sortable: true,
             editrules:{date:true}},
            {name:'divisionId', index:'divisionId', width:100,align:"left"},
            {name:'departmentId', index:'departmentId', width:120,align:"left"},
            {name:'projectId', index:'projectId', width:100,align:"left"},
        ],
        pager: '#testpager',
        loadonce: true,
        sortname: 'projectId',
        sortorder: 'asc',
        viewrecords: true,
        multiselect: true,
        //to hide pager buttons
        pgbuttons:false,
        recordtext:'',
        pgtext:'',
        gridview: true,
        caption: 'my JQGRID',
        jsonReader : {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            cell: "cell",
            id: "id"
        }
    });
    //toolbar search
    //jq("#testgrid").jqGrid('filterToolbar',
    //                       {stringResult:true,searchOnEnter:false});
});

【问题讨论】:

  • 您能否包括(修改并附加您的问题文本)jqGrid 的完整定义。如果可能的话,我建议您在 JSON 数据中仅使用标准 ISO 日期格式(如 2011-03-25),并在 formatoptions 中使用 formatter:'date'newformat 将数据转换为应为的格式显示。
  • 我已经使用了 'datefmt:'M d h:i'' 并且现在已正确排序。但是当 json 为 'startTime' 元素返回 'null' 时,它将无法正确排序。所以问题还没有 100% 解决。
  • 如果你发现了一个错误,你应该发布它here。我建议您在错误报告中插入可用于重现您的问题的完整 JavaScript 代码和测试数据。您只发布了'startTime' 列定义的一行,并且要重现您的问题,您必须编写一些代码,这将花费一些时间。
  • @Oleg 对我的错误感到抱歉。我也添加了我的网格代码。我不知道这是否是一个错误。我仍然认为错误在我这边,我正在努力。将通过您的建议。非常感谢奥列格。
  • @Sam:我试过你的代码,它没有任何问题。如果我将null 用于“startTime”,它也可以工作。你目前的问题是什么?如果有帮助,我可以将 URL 发布到我的工作测试中。

标签: json datetime jqgrid


【解决方案1】:

当我尝试重现您的问题时,一切似乎都正常。观看演示直播here。如果您按“开始时间”列排序,所有工作都没有任何问题,我可以看到。

【讨论】:

  • 非常感谢 Oleg。现在我正在使用你推荐的年份日期,现在工作正常。我查看了你的样本,我这边似乎有问题。我将创建新的 jqgrid 并比较以解决我的真正原因。非常感谢。
  • @Sam:不客气!如果您遇到问题,您可以在新评论或新问题中描述问题,我会尽力帮助您。
猜你喜欢
  • 1970-01-01
  • 2011-04-07
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
相关资源
最近更新 更多