本文来自:http://cnn237111.blog.51cto.com/2359144/782137
jqGrid中自定义格式,URL格式
当官方自带的showlink用起来不是十分顺手,因此可以考虑自己定义url格式
自定义格式其实很简单,在formatter:处填写函数名
比如formatter:makeURL
那么,可以创建一个makeURL函数。
该函数的签名必须遵循官方的约定,有3个参数。
function makeURL(cellvalue, options, rowObject ){
return urlstring;
}
cellvalue -就是要被格式化的值
options - 一个对象,包含的值是options : { rowId: rid, colModel: cm} rowId就是当前行的rowid,colModel就是jqgrid中定义的值
rowObject - 就是当前行的值。
各个值的赋值举例如下:
如果:colModel: [
{ name: "filename", index: "filename", 550, sorttype: "string", sortable: true,formatter:makeURL},
{ name: "datetime", index: "datetime", 80, sorttype: "string", formatter: "string", sortable: true }
],
function makeURL(cellvalue, options, rowObject ){
alert(cellvalue);
alert(rowObject["test"]);//此处的值为返回的json中对应的值。
alert(options["rowId"]);
alert(options["colModel"]["name"]);//此处返回的就是“filename”
}
有了这一招,返回一些自定义的URL就易如反掌,随便加什么参数都可以很容易的获得,只需要能从rowObject["test"]之类的对象中取到数据即可。
还有一种更加简单直白的方式,就是直接在返回的json中,把要显示的字段直接用处理过的<A>这种格式处理。那样的话,直接在表格里显示的就是已经可以直接使用的链接了。
------------------------------------------------------------------------------------------------------------------
这是我在实际中应用到的
场景:aliasName列的数据没有在第一次ajax请求返回数据中,需要根据第一次ajax请求返回数据中本行typesId列中的值来再一次调用ajax请求获得aliasName列的值
colNames:[
\'<div align="center">票据类型编码</div>\',
\'<div align="center">票据类型名称</div>\',
\'<div align="center">开始票据号</div>\',
\'<div align="center">截止票据号</div>\',
\'<div align="center">票据张数</div>\',
\'<div align="center">状态</div>\',
\'<div align="center">创建人</div>\',
\'<div align="center">创建时间</div>\',
\'<div align="center">id</div>\',
\'<div align="center">内部单位Id</div>\',
\'<div align="center">关联总库Id</div>\',
],
colModel:[
{name:\'typesId\',index:\'typesId\', width:\'50\',align:"center",key:true},
{name:\'aliasName\',index:\'aliasName\', width:\'50\',align:\'center\',
formatter:function(cellValue,options,rowObject){ /*根据本行数据中的typesId列的值来ajax请求,得到本列的中的值*/
$.ajax({
url:"${staticServer}/ywgl/ywcsgl/pjzlgl/selectBillTypeByTypesId.htm",
type:"POST",
async:false,
datatype:"json",
data:{
typesId : rowObject.typesId //rowObject.typesId是获取typesId列的值
},
success:function(data){
cellValue=data[0].aliasName;
}
});
return cellValue;
}
},
{name:\'startTypeno\',index:\'startTypeno\', width:\'50\',align:"center"},
{name:\'endTypeno\',index:\'endTypeno\', width:\'50\',align:"center"},
{name:\'typeNumber\',index:\'typeNumber\', width:\'50\',align:"center"},
{name:\'status\',index:\'status\', width:\'50\',align:"center",
formatter:function(cellValue){
if(cellValue==1){
return "未复核";
}else{
return "已复核";
}
}
},
{name:\'inputUser\',index:\'inputUser\', width:\'50\',align:"center",
formatter:function(cellValue){
$.ajax({
url : "${staticServer }/ywgl/zkpzgl/zkfkgl/getInputName.htm",
type : "post",
async:false,
data : {
id : cellValue
},
dataType : "json",
success : function(r){
if(r == null || r == ""){
cellValue = \'无\';
}else{
cellValue = r[0].user_name;
}
}
});
return cellValue;
}
},
{name:\'inputTime\',index:\'inputTime\', width:\'50\',align:"center"},
{name:\'id\',index:\'id\', width:\'50\',align:"center",hidden:true},
{name:\'unitId\',index:\'unitId\', width:\'50\',align:"center",hidden:true},
{name:\'allId\',index:\'allId\', width:\'50\',align:"center",hidden:true}
],