预定义格式化
格式化在单独的模块中定义,该模块名为jquery.fmatter.js. 该模块包含整在整个jquery.jqGrid.js 文件中。
以下是英文档案的预设格式选项:
1 $.jgrid.regional["en"] = { 2 ... 3 formatter : { 4 integer : {thousandsSeparator: ",", defaultValue: '0'}, 5 number : {decimalSeparator:".", thousandsSeparator: ",", decimalPlaces: 2, defaultValue: '0.00'}, 6 currency : {decimalSeparator:".", thousandsSeparator: ",", decimalPlaces: 2, prefix: "", suffix:"", defaultValue: '0.00'}, 7 date : { 8 dayNames: [ 9 "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", 10 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 11 ], 12 monthNames: [ 13 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 14 "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" 15 ], 16 AmPm : ["am","pm","AM","PM"], 17 S: function (j) {return j < 11 || j > 13 ? ['st', 'nd', 'rd', 'th'][Math.min((j - 1) % 10, 3)] : 'th';}, 18 srcformat: 'Y-m-d', 19 newformat: 'n/j/Y', 20 parseRe : /[#%\\\/:_;.,\t\s-]/, 21 masks : { 22 // see http://php.net/manual/en/function.date.php for PHP format used in jqGrid 23 // and see http://docs.jquery.com/UI/Datepicker/formatDate 24 // and https://github.com/jquery/globalize#dates for alternative formats used frequently 25 // one can find on https://github.com/jquery/globalize/tree/master/lib/cultures many 26 // information about date, time, numbers and currency formats used in different countries 27 // one should just convert the information in PHP format 28 ISO8601Long:"Y-m-d H:i:s", 29 ISO8601Short:"Y-m-d", 30 // short date: 31 // n - Numeric representation of a month, without leading zeros 32 // j - Day of the month without leading zeros 33 // Y - A full numeric representation of a year, 4 digits 34 // example: 3/1/2012 which means 1 March 2012 35 ShortDate: "n/j/Y", // in jQuery UI Datepicker: "M/d/yyyy" 36 // long date: 37 // l - A full textual representation of the day of the week 38 // F - A full textual representation of a month 39 // d - Day of the month, 2 digits with leading zeros 40 // Y - A full numeric representation of a year, 4 digits 41 LongDate: "l, F d, Y", // in jQuery UI Datepicker: "dddd, MMMM dd, yyyy" 42 // long date with long time: 43 // l - A full textual representation of the day of the week 44 // F - A full textual representation of a month 45 // d - Day of the month, 2 digits with leading zeros 46 // Y - A full numeric representation of a year, 4 digits 47 // g - 12-hour format of an hour without leading zeros 48 // i - Minutes with leading zeros 49 // s - Seconds, with leading zeros 50 // A - Uppercase Ante meridiem and Post meridiem (AM or PM) 51 FullDateTime: "l, F d, Y g:i:s A", // in jQuery UI Datepicker: "dddd, MMMM dd, yyyy h:mm:ss tt" 52 // month day: 53 // F - A full textual representation of a month 54 // d - Day of the month, 2 digits with leading zeros 55 MonthDay: "F d", // in jQuery UI Datepicker: "MMMM dd" 56 // short time (without seconds) 57 // g - 12-hour format of an hour without leading zeros 58 // i - Minutes with leading zeros 59 // A - Uppercase Ante meridiem and Post meridiem (AM or PM) 60 ShortTime: "g:i A", // in jQuery UI Datepicker: "h:mm tt" 61 // long time (with seconds) 62 // g - 12-hour format of an hour without leading zeros 63 // i - Minutes with leading zeros 64 // s - Seconds, with leading zeros 65 // A - Uppercase Ante meridiem and Post meridiem (AM or PM) 66 LongTime: "g:i:s A", // in jQuery UI Datepicker: "h:mm:ss tt" 67 SortableDateTime: "Y-m-d\\TH:i:s", 68 UniversalSortableDateTime: "Y-m-d H:i:sO", 69 // month with year 70 // Y - A full numeric representation of a year, 4 digits 71 // F - A full textual representation of a month 72 YearMonth: "F, Y" // in jQuery UI Datepicker: "MMMM, yyyy" 73 }, 74 reformatAfterEdit : false, 75 userLocalTime : false 76 }, 77 baseLinkUrl: '', 78 showAction: '', 79 target: '', 80 checkbox : {disabled:true}, 81 idName : 'id' 82 }, 83 ... 84 }
For example.
1 jQuery("#grid_id").jqGrid({ 2 ... 3 colModel : [ 4 ... 5 {name:'myname', ... formatter:'number', ...}, 6 ... 7 ], 8 ... 9 });
列特定选项
可以为特定列定义格式化选项,覆盖语言文件中的默认值。通过使用colModel中的formatoptions选项来实现的. For example:
1 jQuery("#grid_id").jqGrid({ 2 ... 3 colModel : [ 4 ... 5 {name:'myname', ... formatter:'currency', formatoptions:{decimalSeparator:".", thousandsSeparator: ",", decimalPlaces: 2, prefix: "$ "} } , 6 ... 7 ], 8 ... 9 });
预定义的格式类型
下面是预定义格式类型的列表
integer
- thousandsSeparator - 千位符
- defaultValue 默认值.
如果传入的数字不是整数,带有小数位数的数字,decimalPlaces 属性应该设置为0:
1 ... 2 colModel : [ 3 ... 4 {name:'myname', ... formatter:'integer', formatoptions:{thousandsSeparator: " ", deaultValue: "0", decimalPlaces : 0} } , 5 ... 6 ], 7 ...
"1234.12" => "1 234" " " => "0" "123.65" => "124"
number
- thousandsSeparator - 千位符.
- decimalSeparator 小数的分隔符.
- decimalPlaces 保留小数的位数
- defaultValue 默认值.
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'number', 6 formatoptions:{ 7 thousandsSeparator: " ", 8 decimalSeparator: ".", 9 decimalPlaces : 1, 10 deaultValue: "0.0" 11 } 12 } , 13 ... 14 ], 15 ...
currency
- thousandsSeparator - 千位符.
- decimalSeparator 小数的分隔符.
- decimalPlaces 保留小数的位数
- prefix - 前缀字符
- suffix - 后缀字符
- defaultValue 默认值.
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'currency', 6 formatoptions:{ 7 thousandsSeparator: ",", 8 decimalSeparator: ".", 9 decimalPlaces : 2, 10 prefix : "$ ", 11 suffix : "" 12 deaultValue: "$ 0.00" 13 } 14 } , 15 ... 16 ], 17 ...
date
- srcformat 源格式,即应该转换的日期格式
- newformat 新的输出格式
- parseRe 一个解析日期字符串的表达式.
- reformatAfterEdit (default false) 日期是否应该在编辑后重新格式化 - i.e 用户更改日期后保存到grid中。因为在editiong模块中使用日期没有格式化.
- userLocalTime (default false) 在插入grid时,强制将本地时间偏移计算到日期中.
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'date', 6 formatoptions:{ 7 srcformat: "Y-m-d", 8 newformat: "ShortDate" // we use here option mask which is = n/j/Y 9 } 10 } , 11 ... 12 ], 13 ...
"2017-03-01" = > 3/1/2017
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'date', 6 formatoptions:{ 7 srcformat: "ISO8601Long", 8 newformat: "m/d/Y H:i" 9 } 10 } , 11 ... 12 ], 13 ...
如果当前时间是+ 2 GMT,结果是:
"2017-03-01 11:19:22Z" = > 03/01/2017 13:19
结果等同于:
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'date', 6 formatoptions:{ 7 srcformat: "Y-m-d H:i:s", 8 newformat: "m/d/Y H:i", 9 userLocalTime : true 10 } 11 } , 12 ... 13 ], 14 ...
"2017-03-01 11:19:22" = > 03/01/2017 13:19
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'date', 6 formatoptions:{ 7 srcformat: "Y-m-d H:i:s", 8 newformat: "m/d/Y H:i", 9 userLocalTime : true 10 } 11 } , 12 ... 13 ], 14 ...
john.smith@yahoo.com => <a href="mailto:john.smith@yahoo.com">john.smith@yahoo.com</a>
link
- target - 定义在何处打开链接的文档.默认是空值。
target属性的可能值为:
- _blank 在新窗口或tab中打开
- _self 在被当季的同一个frame中打开(这个是默认值)
- _parent 在父框架中打开
- _top 在窗口的整个主体中打开
- framename 在指定的框架中打开
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'link', 6 formatoptions : { 7 target : "_blank" 8 } 9 } , 10 ... 11 ], 12 ...
http://www.guriddo.net => <a href="http://www.guriddo.net" target="_blank">http://www.guriddo.net</a>
showLink
- baseLinkUrl 使用的链接
- showAction 是在baseLinkUrl后添加的附加值
- addParam 附加参数,可以添加到idName属性之后.
- target -指定在何处打开链接
- idName 在baseLinkUrl后添加的第一个参数,默认值是id
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'showLink', 6 formatoptions : { 7 baseLinkUrl : 'http://myserver.com/' 8 showaction: 'editrecord.php' 9 addParam : '&action=edit', 10 idName : 'id', // this is default 11 target : '_blank' 12 } 13 } , 14 ... 15 ], 16 ...
如果单元格内容是Edit Record,且行id=10,则
Edit Record => <a href="http://myserver.com/editrecord.php?id=10&action=edit" target="_blank">Edit Record</a>
checkbox
- disabled - 默认值是true,此选项确定是否可以更改checkbox的值。如果设置为false,checkbox的值可以更改。
1 ... 2 colModel : [ 3 ... 4 { name:'myname', ..., 5 formatter:'checkbox', 6 formatoptions : { 7 disabled : false 8 } 9 } , 10 ... 11 ], 12 ...
true => <input checked="checked" value="true" offval="no" type="checkbox">
特殊格式化
select
- value 定义key的映射到grid的值。这个属性可以是字符串或对象
- delimiter 当Vaule是字符串时使用的分隔符,用来分开不同的key:value。默认是“;”
- separator 当Value是字符串时使用的分隔符,用来分开key和value。默认是":"
- decodeValue 当Value是字符创时使用。此选项为true时,将对字符串进行解码,以防它被编码。当对字符串进行编码并使用editing时,推荐使用此设置。
Note:如果没有设置formatoptions,则尝试从editoptions对象获取相同的值
1 <script> 2 jQuery("#grid_id").jqGrid({ 3 ... 4 colModel : [ {name:'myname', formatter:'select', formatoptions : {value:"1:One;2:Two"}} ... ] 5 ... 6 }); 7 </script>
源数据可以包含key(“1”或“2”),但是value(“1”或“2”)将显示在grid中。
对象的定义如下:
1 <script> 2 jQuery("#grid_id").jqGrid({ 3 ... 4 colModel : [ 5 { 6 name:'myname', 7 formatter:'select', 8 formatoptions : { 9 value: { "1":"One","2":"Two" } 10 } 11 } 12 ... ] 13 ... 14 }); 15 </script>
格式化支持在editoptions中设置多选,如果设置了该选项,就会显示一个由逗号分隔的多个值:
1 <script> 2 jQuery("#grid_id").jqGrid({ 3 ... 4 colModel : [ 5 { 6 name:'myname', 7 edittype : 'select', 8 formatter:'select', 9 editoptions : { 10 value: { "1":"One","2":"Two"}, 11 multiple : true 12 } 13 } 14 ... ] 15 ... 16 }); 17 </script>
actions
这是一个特殊的特性,我们在其中准备了一个e函数,他在每一行上添加可编辑的按钮。为此,需要再colModel中定义附加列(通常在第一列或最后一列)并设置他的格式化操作。
1 $("#jqGrid").jqGrid({ 2 ... 3 colModel: [ 4 { 5 label: "Edit Actions", 6 name: "actions", 7 width: 100, 8 formatter: "actions", 9 formatoptions: { 10 keys: true, 11 editOptions: {}, 12 delOptions: {} 13 } 14 }, 15 ... 16 ] 17 ... 18 });

此格式化在悬停按钮时使用语言文件中的nav属性来获取一些文本,并使用通用的styleUI属性来获取所需的图标。
默认参数:
formatoptions: { keys : false, //如果设置为true,则当用户按ESC键取消编辑不保存;当用户按回车时,保存编辑;如果当前编辑字段是textarea,按回车键时不发生保存 editbutton:true, //启用或禁用编辑按钮 delbutton:true, //启用或禁用删除按钮 editformbutton: false, //启用或禁用表单模式编辑按钮,而不是inline下的编辑 onEdit : null, //在成功进入编辑状态时触发。行id作为参数传给这个函数 onSuccess : false, //在保存数据到服务器的请求成功后立即调用。这个函数传递从服务器返回的数据。取决于服务器返回的数据,这个函数将返回true或false。通知用户是否成功。 url : "", //数据应该保存的url,可以用clientArray进行本地保存 extraparam : {}, //可以添加到服务器的其他用户参数 afterSave : null, //数据保存到服务器后调用此函数。传递的参数是rowid和服务器请求的response。当url被设置为clientArray时也会调用该事件 onError : null, //在数据保存到服务器后调用。传入的参数是rowid和来自服务器响应的response和status。但url被设置为clientArray时也会调用该事件。要么在ajax错误时调用,要么在onSuccess事件返回false时调用该事件 afterRestore : null, //在恢复行之后调用(esc键或取消编辑按钮)。传入参数为rowid restoreAfterError: null, mtype: 'POST', //发布数据的方法类型(GET或POST) editOptions: {}, //如果editformbutton设置为true,则使用有效的editGridRow选项 delOptions: {} //有效的editGridRow选项 }
Note:如果编辑按钮在字段处于编辑模式后备激活,我们将自动添加两个附加按钮--保存行和取消编辑。无法禁用这些按钮.
自定义格式化
指定列定义自己的formatter。通常是一个函数,返回一个字符串
一种是定义一个函数,然后在formatter中直接显示函数的名字
1 <script> 2 jQuery("#grid_id").jqGrid({ 3 ... 4 colModel: [ 5 ... 6 { 7 name:'price', 8 index:'price', 9 formatter:currencyFmatter 10 }, 11 ... 12 ] 13 ... 14 }); 15 16 function currencyFmatter (cellvalue, options, rowObject) 17 { 18 // do something here 19 return new_format_value 20 } 21 </script>
另一种直接在formatter中直接设置函数和参数
1 <script> 2 jQuery("#grid_id").jqGrid({ 3 ... 4 colModel: [ 5 ... 6 { 7 name:'price', 8 index:'price', 9 formatter: function(cellvalue, options, rowObject) { 10 // do something here 11 return new_format_value 12 } 13 }, 14 ... 15 ] 16 ... 17 }); 18 </script>
- cellvalue - 要格式化的值
- options - 包含这些元素{ rowId: rid, colModel: cm, gid : gridId, pos : column_position, styleUI : guiStyle , isExported : boolval, exporttype : type_when_export}
- rowId - 行id,
- colModel 从jqGrid的colModel数组中获取的这个列的属性的对象,
- gid grid的id,
- pos colModel中列的位置,
- styleUI style对象,
- isExported 仅在导出发生时才传递的参数,允许在导出操作时进行自定义格式化。本例子中的参数为true,仅在导出到CSV、EXCEL或PDF时有效,
- exporttype 一个字符串,只有isExported为true时才可用。表示导出的类型:csv,pdf或excel
- rowObject- 用datatype选项确定格式表示的行数据。如果我们有数据类型:xml/xmlstring - rowObject是xml节点,根据xmlReader提供的规则提供。如果数据类型是:json/jsonstring - rowObject为对象,根据jsonReader的规则提供
Unformatting
在使用自定义formatter函数时,可以在使用编辑或getRowData和getCell方法时取回原始值
Example : 显示图片和编辑图片的路径
1 <script> 2 jQuery("#grid_id").jqGrid({ 3 ... 4 colModel: [ 5 ... 6 { 7 name:'price', 8 index:'price', 9 editable: true, 10 formatter:imageFormat, 11 unformat:imageUnFormat 12 }, 13 ... 14 ] 15 ... 16 }); 17 18 function imageFormat( cellvalue, options, rowObject ){ 19 return '<img src="'+cellvalue+'" />'; 20 } 21 function imageUnFormat( cellvalue, options, cell){ 22 return $('img', cell).attr('src'); 23 } 24 </script>
unformat 函数的参数:
- cellvalue - 未格式化的值(纯文本)
- options -包含以下元素
options : { rowId: rid, colModel: cm} rowId - 行id colModel jqGrid的colModel数组中获取的colModel。 - cellobject - 一个jQuery单元格对象。可用于单元格元素获取不同的内容-通过示例jQuery(cellobject).html()可用于获取html内容,而不是文本
1 <script> 2 jQuery("#grid_id").jqGrid({ 3 ... 4 colModel: [ 5 ... 6 { 7 name:'price', 8 index:'price', 9 editable: true, 10 formatter:currencyFmatter, 11 unformat:unformatCurrency 12 }, 13 ... 14 ] 15 ... 16 }); 17 18 function currencyFmatter (cellvalue, options, rowObject) 19 { 20 return "$"+cellvalue; 21 } 22 function unformatCurrency (cellvalue, options) 23 { 24 return cellvalue.replace("$",""); 25 } 26 </script>
如果在grid中插入或更新的值为123.00,则在grid中显示的值为$123.00,当我们使用getRowData()或getCell()方法或任何编辑模块时,本列的值将是123.00
定义公共格式化函数
如果在代码中多次使用某些自定义formatter/unformatter,则可以一次性定义格式化函数并将其使用到代码中。
1 <script type="text/javascript"> 2 jQuery.extend($.fn.fmatter , { 3 currencyFmatter : function(cellvalue, options, rowdata) { 4 return "$"+cellvalue; 5 } 6 }); 7 jQuery.extend($.fn.fmatter.currencyFmatter , { 8 unformat : function(cellvalue, options) { 9 return cellvalue.replace("$",""); 10 } 11 }); 12 </script>
1 <script> 2 jQuery("#grid_id").jqGrid({ 3 ... 4 colModel: [ 5 ... 6 { 7 name:'price', 8 index:'price', 9 editable: true, 10 formatter:'currencyFmatter' 11 }, 12 ... 13 ] 14 ... 15 }); 16 ... 17 </script>
浙公网安备 33010602011771号