【问题标题】:jqGrid - How to supply multiple pre-defined formatters via custom formatterjqGrid - 如何通过自定义格式化程序提供多个预定义的格式化程序
【发布时间】:2015-03-11 21:00:02
【问题描述】:

我遇到了一种情况,我为我的一个 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 看来,自定义格式化程序可以调用“多个格式化程序,具体取决于基础数据的值”。怎么做呢?

感谢您的帮助,谢谢!

【问题讨论】:

  • 能否包含您需要获取的列输入数据示例和格式化结果示例。了解您使用的 jqGrid 版本可能很重要。例如,formatter: "showlink" 在免费的 jqGrid 4.8(我最近发布了 herehere)中有更多的可能性。见the wiki article
  • @Oleg 感谢 Oleg 的回复。我正在使用 jqGrid 4.6 顺便说一句。我已经在下面发布了我的答案 - 您对另一个问题的较早回复之一也帮助了我。

标签: javascript jquery jqgrid jqgrid-formatter


【解决方案1】:

知道了!

有几种方法可以做到。

首先,我将 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,它指定了完成任务的另一种方式。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    相关资源
    最近更新 更多