javascript - jqGrid 字符串自定义格式,如日期格式
问题描述
我正在尝试使用 jqGrid 在网页上打印 json 数据,但存在一些问题。
这是我的js脚本代码。
$('#sometables').jqGrid({
...
...
...
colNames: ['created_date'],
colModel: [ {name: 'created_date', align: 'center} ],
...
...
...
});
created_date
是 json 数据(起源 java String 类型)。
所以,我可以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
我可以将字符串数据转换为类似日期的数据吗?必须制作字符串格式化功能吗?如果可以,请举一些例子。
解决方案
例如,您可以使用自定义格式化程序,它在内部调用预定义的日期格式化程序。相应的代码可能如下所示:
{
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"
}
}
推荐阅读
- sql-server - 如何从 ADF 传递 SQL 中 TABLE VALUE TYPE 中的可选列
- java - 我怎样才能实现java接口到java类
- azure-devops - 在 Azure DevOps 版本中使用 ask deploy 时出错
- c - 如何减少这段代码以避免TLE
- linux - 如何通过 rc.local 重启 Linux 中的后台进程?
- typescript - 在Vue中创建v-select菜单时动态推送新数据集
- javascript - JS Discord Bot - 创建具有特定权限的频道 ( v12+ )
- python - 启用 CORS 谷歌云函数 (Python)
- css - 如何获得这样的剪辑路径
- java - 使用 PHP 在 android 中向特定用户或特定频道发送通知