我很好地理解了这个问题。我同意目前可以使用的两种预定义格式器('showlink' 和 'link' 格式器)都不够灵活。
我可以建议你另一个格式化程序,你可以在这里下载。格式化程序的使用非常简单:
{label: "AddToCart", name: "Addtocrt_addtocrt", formatter: "dynamicLink",
formatoptions: {
url: function (cellValue, rowId, rowData) {
return '/Store/AddToCart' + rowId + '?' +
$.param({
quantity: rowData.Stocks_valkogus
});
}
}
}
的url
定义为函数将在被使用<a>
作为值href
属性。
除了url
formatoptions
'dynamicLink' 格式化程序支持target
选项(与 'showlink' 含义相同),cellValue
它也可以是函数和onClick
回调,以rowId
, iRow
, iCol
, cellValue
,e
作为参数。如果onClick
定义了回调,则 的值url
将被忽略。所以可以跳过格式化选项的定义url
。
该演示演示了“dynamicLink”格式化程序的用法:

formatter: 'dynamicLink'
您可以在下面找到当前的代码:
/*global jQuery */
(function ($) {
'use strict';
/*jslint unparam: true */
$.extend($.fn.fmatter, {
dynamicLink: function (cellValue, options, rowData) {
// href, target, rel, title, onclick
// other attributes like media, hreflang, type are not supported currently
var op = {url: '#'};
if (typeof options.colModel.formatoptions !== 'undefined') {
op = $.extend({}, op, options.colModel.formatoptions);
}
if ($.isFunction(op.target)) {
op.target = op.target.call(this, cellValue, options.rowId, rowData, options);
}
if ($.isFunction(op.url)) {
op.url = op.url.call(this, cellValue, options.rowId, rowData, options);
}
if ($.isFunction(op.cellValue)) {
cellValue = op.cellValue.call(this, cellValue, options.rowId, rowData, options);
}
if ($.fmatter.isString(cellValue) || $.fmatter.isNumber(cellValue)) {
return '<a' +
(op.target ? ' target=' + op.target : '') +
(op.onClick ? ' onclick="return $.fn.fmatter.dynamicLink.onClick.call(this, arguments[0]);"' : '') +
' href="' + op.url + '">' +
(cellValue || ' ') + '</a>';
} else {
return ' ';
}
}
});
$.extend($.fn.fmatter.dynamicLink, {
unformat: function (cellValue, options, elem) {
var text = $(elem).text();
return text === ' ' ? '' : text;
},
onClick: function (e) {
var $cell = $(this).closest('td'),
$row = $cell.closest('tr.jqgrow'),
$grid = $row.closest('table.ui-jqgrid-btable'),
p,
colModel,
iCol;
if ($grid.length === 1) {
p = $grid[0].p;
if (p) {
iCol = $.jgrid.getCellIndex($cell[0]);
colModel = p.colModel;
colModel[iCol].formatoptions.onClick.call($grid[0],
$row.attr('id'), $row[0].rowIndex, iCol, $cell.text(), e);
}
}
return false;
}
});
}(jQuery));
我打算将格式化程序的代码和其他一些插件放到 github 上的 jqGrid 中。
更新: 免费 jqGrid扩展了formatter: "showlink"
(参见维基文章和答案)的选项。所以formatter: "dynamicLink"
在使用免费jqGrid的情况下不需要使用。