首页 > 技术文章 > Jqgrid入门-Jqgrid格式化数据(九)

hongzai 2013-07-04 18:53 原文

上一章已经说明了在Jqgrid中如何对数据分组,这一章主要探讨如何格式化Jqgrid中的数据。何谓格式化呢?举个例子,比如对时间格式化处理,去掉后面的时分秒;对数字进行处理,加上千分位分隔符,小数的保留位数,加上前缀或后缀。对超链接或邮箱等等。

        jqGrid中对列表cell属性格式化设置主要通过colModel中formatter、formatoptions来设置的。jqGrid中也预定义了常见的格式及其options属性。
formatter formatoptions
integer  thousandsSeparator://千分位分隔符, 如”,” defaulValue://默认值
currency decimalSeparator: //小数分隔符,如”.” thousandsSeparator: //千分位分隔符,如”,” decimalPlaces: //小数保留位数 defaulValue:  prefix://前缀,如加上”$” suffix://后缀
number  decimalSeparator: //小数分隔符,如”.” thousandsSeparator: //千分位分隔符,如”,” decimalPlaces: //小数保留位数 defaulValue:
date srcformat, //原来的格式 newformat //新格式
select 没有参数,使用方法如: editoptions:{value:”0:男;1:女”}} 
checkbox  disabled:true or false 
showlink  baseLinkUrl://在当前cell中加入link的url showAction://在baseLinkUrl后加入&action=actionName addParam://参数,如”&name=xx” target: idName:默认会在baseLinkUrl后加入,如”.action?id=1″。如果 设置idName=”XX”,那么”.action?XX=1″。取值为当前rowid
email 没有参数

用法也比较简单,在Jqgrid的ColModel加入相应的formatter和formatoptions即可。如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
name : "age",
index : "age",
label : "年龄",
width : 120,
sortable : true,
align : 'center',
formatter : "number",
formatoptions : {
decimalSeparator : ".",
thousandsSeparator : ",",
decimalPlaces : 0,
defaulValue : 0
},

如果预定义的格式化不能满足你的要求,Jqgrid还提供了自定义格式化数据的方式。语法为:

1
2
3
function DIYFmatter(cellvalue, options, rowObject){
//your code
}

对于自定义formatter,在修改时需要获取原来的值,这里就提供了unformat函数。如:

1
2
3
4
5
6
7
8
9
10
{
name : "age",
index : "age",
label : "年龄",
width : 120,
sortable : true,
align : 'center',
formatter : DIYFmatter,
unformat:DIYUnFormat
}

然后自己写自定义格式化的方法。如:

1
2
3
4
5
6
7
8
function DIYFormat( cellvalue, options, rowObject ){
return '<img alt="" src="'+cellvalue+'" />';
}
function DIYUnFormat( cellvalue, options, cell){
 
return $('img', cell).attr('src');
 
}

如果还有什么疑惑的地方,可以留言。

原创文章,转载请注明: 转载自java开发者

本文链接地址: Jqgrid入门-Jqgrid格式化数据(九)

推荐阅读