jqGrid字符串自定义格式,如日期格式

时间:2018-09-12 00:54:34

标签: javascript json jsp spring-boot jqgrid

我正在尝试使用jqGrid在网页上打印json数据, 但是有一些问题。

这是我的js脚本代码。

$('#sometables').jqGrid({
    ...
    ...
    ...
    colNames: ['created_date'], 
    colModel: [ {name: 'created_date', align: 'center} ], 
    ...
    ...
    ...
});

created_date是json数据(原始java字符串类型)。

因此,我可以在网页网格上看到created_date,如下所示:20180912093510, 这意味着2018/09/12 09:35:10

问题是:如何在jqgrid中格式化字符串类型?

我想看2018/09/12 09:35:10,而不是20180912093510

我先尝试过:

{name: 'created_date', align: 'center', formatter: 'date', formatoptions: {newformat: 'Y/m/d H:i:s'}},

但是结果是:NaN/NaN/NaN NaN:NaN:NaN

我尝试了第二次:

{name: 'created_date', align: 'center', formatter: 'date', formatoptions: {srcformat: 'string', newformat: 'Y/m/d H:i:s'}},

但是结果是:1970/01/01 00:00:00

我可以将字符串数据转换为类似日期的数据吗?必须设置字符串格式化功能?如果可以的话,请举一些例子。

1 个答案:

答案 0 :(得分:0)

例如,您可以使用自定义格式器,该格式器在内部调用预定义的日期格式器。相应的代码如下所示:

{
    name: "created_date",
    align: "center",
    sorttype: "integer",
    formatter: function (cellValue, options, rowdata, action) {
        var fixedValue = cellValue.substr(0, 4) + "-" +
                cellValue.substr(4, 2) + "-" +
                cellValue.substr(6, 2) + "T" +
                cellValue.substr(8, 2) + ":" +
                cellValue.substr(10, 2) + ":" +
                cellValue.substr(12);
        return $.fn.fmatter.call(this, "date", fixedValue, options, rowdata, action);
    },
    formatoptions: {
        srcformat: "ISO8601Long",
        newformat: "Y/m/d H:i:s"
    }
}

请参见https://jsfiddle.net/OlegKi/7pzy2x86/1/

相关问题