【问题标题】:How to use jqGrid Custom Formatter and 'showlink' at the same time? Links in column do not appear link-like如何同时使用 jqGrid 自定义格式化程序和“showlink”?列中的链接不会像链接一样出现
【发布时间】:2016-06-07 22:30:13
【问题描述】:

我正在尝试将 jqgrid 第一列中的变量链接到另一个页面。现在,变量不会出现类似链接或排他的(意味着无论我在哪里点击行,我都会被带到目标页面)。

我需要使用自定义格式化程序,因为我的搜索栏中的 URL 不应更改(我在本地服务器上运行它)。但是使用自定义格式化程序,我似乎也无法使用“showlink”(当我不使用自定义格式化程序时,这最终似乎使我的数据看起来像链接)。当我将鼠标悬停在第一列中的数据上时,我想要手指光标,我现在得到的只是“我”。

有没有什么方法可以同时使用 formatter:'showlink' 来自预定义格式化程序和 formatter: returnHyperLink(name) 来自自定义格式化程序?我希望能够仅单击要带到页面的第一列数据,并且我希望这些数据显示为类似链接(不应单击行上的任何位置)。

我的相关jqgrid代码是:

    colNames:['Name','Status', 'Created On', 'Update By', 'Updated On', 'RetentionDays','ValidityTime','Edit'],
    colModel:[
              {  
                  name: 'name', width:100,editable: true, edittype:'select',
                  formatter: returnHyperLink(name),

                  xmlmap: function (obj) {
                      return $(obj).attr('name'); 
                  }

              },

而我的函数 returnHyperLink 显示为:

函数 returnHyperLink(name){

$(this).click(function() {
      $( "#contents" ).load("jsp/consumers.jsp");
         console.log(this, "Hello world");

    });

};

...好吧,如果所有这些代码甚至都没有出现在代码框中,那么显然是有问题的。我在想我可以从 href 内部调用 javascript 函数,但我也不知道该怎么做。

【问题讨论】:

    标签: javascript jquery html hyperlink jqgrid


    【解决方案1】:

    在我看来,您误解了自定义格式化程序的工作原理。 jqGrid 创建整个网格体 (<tbody>) 作为字符串。因此,自定义格式化程序应该返回字符串,并且格式化程序内部没有 $(this).click 不是您所期望的。在自定义格式化程序工作期间,<td> 元素尚不存在

    你没有写,你使用哪个版本的 jqGrid 和来自哪个分支的 jqGrid。如果你会使用free jqGrid,那么你可以使用formatter: "showlink"onClick回调选项:

    formatter: "showlink", formatoptions: {
        onClick: function (options) {
            // this is DOM of the grid
            // options.iCol is the column index
            // options.iRow is the row index
            // options.rowid is the rowid
            // options.cm is the element from colModel
            // options.cmName is the column name
            // options.cellValue is the text from the <a>
            // options.a - DOM element of the clicked <a>
            // options.event - Event object of the click event
            alert("it's clicked!");
        }
    }
    

    如果您必须使用旧版本的 jqGrid 并且无法迁移到免费的 jqGrid,那么您可以从 here 下载 jQuery.jqGrid.dynamicLink.js 并使用 formatter: "dynamicLink",我在 the old answer 中对此进行了描述。也见the answer

    如果您更喜欢使用自定义格式化程序,那么您可以使用beforeSelectRow 回调来实现onClick 操作。请参阅the answerthis one 了解更多详情。

    【讨论】:

    • @BellaWesley:onClick的值应该是回调函数。
    • 通过使用动态链接格式化程序,例如: formatter: "dynamicLink", "formatoptions":{"onClick":$( "#contents" ).load("jsp/consumers.jsp")} ,我被自动带到页面,而不是“点击”。我还尝试了示例代码中的内容:formatter:“showlink”,formatoptions:{onClick:function(options){$(“#contents”).load(“jsp/consumers.jsp”)}},但是这不会带我到指定的链接。它带我到“原始 url + ?id=XXX”
    • @BellaWesley:你能提供不起作用的演示吗?你目前的问题是什么?是否会调用onClick 回调?您写道“这不会将我带到指定的链接”。你想拥有什么样的行为?如果您指定 onClick 回调,则 href 将被忽略。您可以在formatoptions 中添加idName: null 以拥有&lt;a href="#"&gt;...&lt;/a&gt;。如果将调用 onClick 回调,那么您可以将 location 分配给任何值。请参阅the demo 作为谷歌的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多