【发布时间】:2013-06-10 12:34:50
【问题描述】:
我正在使用JQGrid,并且由于它来自 DB,所以在网格中显示 Null。我可以更改查询以返回空白值。
但我尝试使用 JQGrid 来处理。我如何在网格中replace null by blank values。
我不想向用户显示 NULL 而不是显示空白。
我如何在 JQGrid 中实现这一点?
谢谢
【问题讨论】:
我正在使用JQGrid,并且由于它来自 DB,所以在网格中显示 Null。我可以更改查询以返回空白值。
但我尝试使用 JQGrid 来处理。我如何在网格中replace null by blank values。
我不想向用户显示 NULL 而不是显示空白。
我如何在 JQGrid 中实现这一点?
谢谢
【问题讨论】:
最好由你处理这个服务器端,但如果你想在 jqGrid 中处理,你可以使用将 null 转换为空字符串的custom formatter。 (我不确定您是否真的取回了值null 或字符串"NULL",所以我处理了这两种情况):
var nullFormatter = function(cellvalue, options, rowObject) {
if(cellvalue === undefined || isNull(cellvalue) || cellvalue === 'NULL') {
cellvalue = '';
}
return cellvalue;
}
$("#myGridContainer").jqGrid({
....
colModel: [{
label: 'Name',
name:'name',
index:'name',
formatter:nullFormatter
}, {
label: 'Next Column',
name:'nextCol',
index:'nextCol',
formatter: nullFormatter
}, ...],
....
}
我遇到了同样的问题。
另外,我希望jqGrid 以千位分隔符和 2 个小数位显示我的数字,但使用默认的 'number' 格式化程序导致任何 nulls(来自数据库)显示为“ 0.00" 而不是留空。
$("#tblListOfRecords").jqGrid({
...
colModel: [
{ name: "SomeNumber", formatter: 'number', sorttype: "integer", formatoptions: { decimalPlaces: 2 } }
]
...
这不是我想要的。
我的解决方案是编写自己的格式化程序:
$("#tblListOfRecords").jqGrid({
...
colModel: [
{ name: "SomeNumber", formatter: formatNumber}
]
});
function formatNumber(cellValue, options, rowdata, action) {
// Convert a jqGrid number string (eg "1234567.89012") into a thousands-formatted string "1,234,567.89" with 2 decimal places
if (cellValue == "")
return "";
if (cellValue == null || cellValue == 'null')
return "";
var number = parseFloat(cellValue).toFixed(2); // Give us our number to 2 decimal places
return number.toLocaleString(); // "toLocaleString" adds commas for thousand-separators.
}
【讨论】: