jqGrid中對列表cell數次那個格式話設置主要通過colModel中formatter,formatoptions來設置。
基本用法:
- jQuery("#jqGrid_id").jqGrid({
- ...
- colModel: [
- ...
- {name:'price', index:'price', formatter:'integer', formatoptions:{thousandsSeparator: ','}},
- ...
- ]
- ...
- });
jqGrid中預定義了常見的格式及其options:
integer
thousandSeparator://千分位分隔符,
defaulValue
number
decimalSeparator,//小數分隔符,如"."
tousandsSwparator,//千分位分隔符,如","
decimalPlaces,//小數保留位數
defaulValue
currency
- var datas = [
- {"id":1, "name":"name1", "price":123.1, "email":"[email protected]", "amount":1123423, "gender":"1", "type":"0"},
- {"id":2, "name":"name2", "price":1452.2, "email":"[email protected]", "amount":12212321, "gender":"1", "type":"1"},
- {"id":3, "name":"name3", "price":125454, "email":"[email protected]", "amount":2345234, "gender":"0", "type":"0"},
- {"id":4, "name":"name4", "price":23232.4, "email":"[email protected]", "amount":2345234, "gender":"1", "type":"2"}]
- colModel:[
- {name:'id', index:'id', formatter: customFmatter},
- {name:'name', index:'name', formatter: "showlink", formatoptions:{baseLinkUrl:"save.action",idName: "id", addParam:"&name=123"}},
- {name:'price', index:'price', formatter: "currency", formatoptions: {thousandsSeparator:",",decimalSeparator:".", prefix:"$"}},
- {name:'email', index:'email', formatter: "email"},
- {name:'amount', index:'amount', formatter: "number", formatoptions: {thousandsSeparator:",", defaulValue:"",decimalPlaces:3}},
- {name:'gender', index:'gender', formatter: "checkbox",formatoptions:{disabled:false}},
- {name:'type', index:'type', formatter: "select", editoptions:{value:"0:無效;1:正常;2:未知"}}
- ],
- function customFmatter(cellvalue, options, rowObject){
- console.log(cellvalue);
- console.log(options);
- console.log(rowObject);
- return "["+cellvalue+"]";
- };
- function customFmatter(cellvalue, options, rowObject){
- }
- //cellvalue - 當前cell的值
- //options - 該cell的options設置,包括{rowId, colModel,pos,gid}
- //rowObject - 當前cell所在row的值,如{ id=1, name="name1", price=123.1, ...}
- jQuery("#grid_id").jqGrid({
- ...
- colModel: [
- ...
- {name:'price', index:'price', width:60, align:"center", editable: true, formatter:imageFormat, unformat:imageUnFormat},
- ...
- ]
- ...
- });
- function imageFormat( cellvalue, options, rowObject ){
- return '<img src="'+cellvalue+'" />';
- }
- function imageUnFormat( cellvalue, options, cell){
- return $('img', cell).attr('src');
- }