jqGrid - 如何通过自定义格式化程序提供多个预定义的格式化程序

jqGrid - How to supply multiple pre-defined formatters via custom formatter

我遇到了一种情况,我正在为我的一个 colModel 使用 formatter: 'select'。例如:

{ key: false, name: 'Col5Id', index: 'Col5Id', editable: true, width: 140, edittype: 'select', editoptions: { value: getKeyValuePairsForDisplay('/Controller/Action', 'Id', 'Name') }, formatter: 'select' },

这工作正常,但我还需要将该值转换为超链接(这通常由 formatter: 'showlink' 完成,但我不能使用它,因为 jqGrid 只接受一个格式化程序)。我研究的解决方法是使用自定义格式化程序并完成此操作。

this question 看来,自定义格式化程序可以调用 "multiple formatters depending upon the value of the underlying data"。怎么做到这一点?

非常感谢任何帮助,谢谢!

知道了!

有几种方法可以完成。

首先,我将 colModel 更改为:

{ key: false, name: 'Col5Id', index: 'Col5Id', editable: true, width: 140, edittype: 'select', editoptions: { value: getKeyValuePairsForDisplay('/Controller/Action', 'Id', 'Name') }, formatter: myFormatter },

然后,我将 myFormatter 构建为:

function myFormatter(cell, options, row) { 
    //Method 1 - Manually specify URL (worked better in my case)
    var selectValue = $.fn.fmatter.select(cell, options, row);
    return "<a href='/Controller/Action' title='View' id='viewId'>" + selectValue + "</a>";

    //Method 2 - Pass the formatted value from the select formatting into the showlink function
    return $.fn.fmatter.showlink(($.fn.fmatter.select(cell, options, row), options);
}

还偶然发现 this answer by Oleg 指定了另一种完成任务的方法。

希望其他人能从中受益!干杯!