1
我正在jqGrid中使用自定义格式化函数来填充与链接的下载图标的“下载”单元格。我使用我的所有代码内联,但我只是将所有JS代码移动到一个自定义对象打包并命名空间。现在,当我调用自定义格式化函数时,“this”引用将更改为jqgrid表,并且它无法找到构建网格之前构建的图标对象。获取外部数据到自定义jqGrid格式化函数
所有这些都是有道理的,我还想知道如何引用作为包装自定义对象一部分的图标对象。下面是相关代码:
//loop over the json and build the colmodel, call custom formatter
jQuery.each(jsonObj, function() {
var sdFields = this.supplementalData.fields.field;
len = sdFields.length;
for(var i=0; i<len; i++) {
if(sdFields[i].display) {
var currOption = {};
currOption.name = sdFields[i].id;
currOption.index = sdFields[i].id;
if(sdFields[i].displayType == 'icon') {
currOption.formatter = this.resultsGridFormatter;
} else if(sdFields[i].dataType == 'date') {
//currOption.datefmt = 'mm/dd/YYYY';
currOption.formatter = 'date';
currOption.formatoptions = {
srcformat: 'Y-m-d',
newformat: 'm/d/Y'
};
}
currOption.jsonmap = sdFields[i].id;
currOption.width = sdFields[i].width;
currOption.align = sdFields[i].align;
currOption.sorttype = sdFields[i].dataType;
currOption.sortable = sdFields[i].sortable;
currOption.resizable = sdFields[i].resizeable;
colModel[i] = currOption;
}
}
});
//loop over jsonObj and build the icons object (assoc. array)
this.setIcons = function(jsonObj) {
var iconsObj = {};
jQuery.each(jsonObj, function() {
var sdIcons = this.supplementalData.icons.icon;
var len = sdIcons.length;
for(var i=0; i<len; i++) {
iconsObj[sdIcons[i].id] = sdIcons[i].file;
}
});
this.icons = iconsObj;
};
//custom formatter that formats icon cells by referencing the icons created above
this.resultsGridFormatter = function(cellvalue, options, rowObject) {
console.log(this);
var icons = this.getIcons();
var cellVal = "";
var cssclass = "icon_"+options.colModel.name;
if(cellvalue != null) {
if(cellvalue.indexOf("://") != -1) {
//it is a URL, so link create the icon and link it
cellVal += "<a href='"+cellvalue+"' target='_blank'><img src='"+icons[options.colModel.name]+"' class='"+cssclass+"' /></a>";
}else{
//it is an id, so link to the details modal
cellVal += "<img src='"+icons[options.colModel.name]+"' id='"+cellvalue+"' class='"+cssclass+"' />";
}
} else {
cellVal += " ";
}
//console.log(cellvalue);
//console.log(options);
//console.log(rowObject);
return cellVal;
};
自定义格式我的console.log语句输出的jqGrid的表,因而有“this.getIcons()”调用失败,因为没有这样的方法。
无论如何,我可以参考自定义格式化器中的图标?我是否必须以某种方式包装该功能以包含它或采取其他方法?
你发布的代码片段,而不是它使用的上下文。上下文在定义中定义了什么是`this`,比如`this.resultsGridFormatter = function(...`。这里有问题,我不明白你发布的代码**为什么你使用`this.`前缀在代码中,所以你应该附上你的问题的信息,最好的是小测试代码,可以用来重现你的问题 – Oleg 2011-01-11 20:18:27
好的,请查看http://pastebin.com/DkLe4KQW自定义的JS对象,它从HTML调用如下所示:var ov = new ObjectViewer(uid,path,rurl); ov.buildSearchResults(); – Zendog74 2011-01-11 20:35:07