Jqgrid入门-Jqgrid格式化数据(九)

本文介绍了在 Jqgrid 中如何对数据进行格式化处理,包括时间格式化、数字格式化等常见操作,并提供了自定义格式化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

        jqGrid中对列表cell属性格式化设置主要通过colModel中formatter、formatoptions来设置的。jqGrid中也预定义了常见的格式及其options属性。
formatterformatoptions
integer  thousandsSeparator://千分位分隔符, 如”,” defaulValue://默认值
currency decimalSeparator: //小数分隔符,如”.” thousandsSeparator: //千分位分隔符,如”,” decimalPlaces: //小数保留位数 defaulValue:  prefix://前缀,如加上”$” suffix://后缀
number  decimalSeparator: //小数分隔符,如”.” thousandsSeparator: //千分位分隔符,如”,” decimalPlaces: //小数保留位数 defaulValue:
datesrcformat, //原来的格式 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');
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值