【问题标题】:how to write events in onclick of a hyperlink in ColModel of jqgrid如何在jqgrid的ColModel中的超链接的onclick中编写事件
【发布时间】:2011-12-20 03:30:39
【问题描述】:

我想在 jqgrid 的 ColModel 中的超链接的 onclick 函数中编写事件。 jqgrid中超链接列的onclick如何调用函数

js代码是这样的

colModel:[      
    {name:'controlCenterCode',
    formatter:'showlink',   //formatoptions:editControlCenterPage(results), formatoptions:onClick=editControlCenterPage(results),
    index:results.controlCenterCode, width:70}

直接调用该函数是可以的,但是我想在特定列的单元格的onclick中调用该函数,如何解决?

我也试过这样,直接在列中包含超链接,也不起作用

{name:'controlCenterCode',index:"<span><a href='#'onclick='editControlCenterPage(results)'>"+results.controlCenterCode+"</a></span>", width:70} 

请给我一个解决方案 谢谢

【问题讨论】:

  • 通常有比使用 onclick 属性更好的解决方案,例如将事件委托给具有特定类的表中的锚标记。

标签: jquery jqgrid


【解决方案1】:

我从未使用过 showlink 格式化程序,但这应该适合你:

在您的链接中添加live event handler

$('[aria-describedby="gridId_columnName"] a').live('click', function() { 
    alert('hello'); 
});

aria- describeby 属性应采用 gridId_columnName 格式,其中 gridId 是承载网格的元素的 ID:$('#gridId').jgGrid() ...,columnName 是colModel 中定义的列,因此在您的情况下,它将是“controlCenterCode”

我使用的解决方案是:

  1. 使用自定义格式化程序呈现链接。
  2. 在我的链接中,我分配了一个类(该类不必存在于任何样式表中)。
  3. 我将任何想要传递到链接中的数据作为data attribute 填充。 data-id 在 HTML4 中并不严格合法,但它适用于所有现代浏览器。
  4. 将实时事件处理程序附加到具有该类的元素。

例子:

// custom formatter:
return '<a href="#" class="actionButton" data-id="124">Click Me!</a>';

// attach live event
$('.actionButton').live('click', function() { 
    alert($(this).attr('data-id')); 
});

【讨论】:

    【解决方案2】:

    您可以使用 afterInsertRow 获取表格行 (tr) 元素并根据需要附加事件。

    $grd = 网格 ID .btn = 要附加点击处理程序的元素类。

        afterInsertRow: function(rowid, aData) {
    
            var myTR = $('#grd tr:last-child');
            myTR.find('.btn').click(function () {
    
                alert('Element with class="btn" clicked!');
    
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 2018-01-10
      相关资源
      最近更新 更多