<script>
jQuery("#grid_id").jqGrid({
...
   colModel: [
      ...
      {name:'price', index:'price', width:60, align:"center", editable: true, formatter:currencyFmatter},
      ...
   ]
...
});

function currencyFmatter (cellvalue, options, rowObject)
{
   // do something here
   return new_format_value
}
</script>

定义的参数

function myformatter ( cellvalue, options, rowObject )
{
// format the cellvalue to new format
return new_formated_cellvalue;
}

cellvalue:要被格式化的值
options:对数据进行格式化时的参数设置,格式为:
{ rowId: rid, colModel: cm}
rowObject:行数据

数据的反格式化跟格式化用法相似。

<script>
jQuery("#grid_id").jqGrid({
...
   colModel: [
      ...
      {name:'price', index:'price', width:60, align:"center", editable: true, formatter:currencyFmatter, unformat:unformatCurrency},
      ...
   ]
...
});

function currencyFmatter (cellvalue, options, rowObject)
{

   return "$"+cellvalue;
}
function  unformatCurrency (cellvalue, options)
{

   return cellvalue.replace("$","");
}

</script>

表格中数据实际值为123.00,但是显示的是$123.00; 我们使用getRowData ,getCell 方法取得的值是123.00。
创建通用的格式化函数
定义如下:   

<script type="text/javascript">
jQuery.extend($.fn.fmatter , {
    currencyFmatter : function(cellvalue, options, rowdata) {
    return "$"+cellvalue;
}
});
jQuery.extend($.fn.fmatter.currencyFmatter , {
    unformat : function(cellvalue, options) {
    return cellvalue.replace("$","");
}
});

</script>

具体使用:

<script>
jQuery("#grid_id").jqGrid({
...
   colModel: [
      ...
      {name:'price', index:'price', width:60, align:"center", editable: true, formatter:currencyFmatter},
      ...
   ]
...
});
posted on 2013-03-04 10:30  tware_dsy  阅读(437)  评论(0)    收藏  举报