Javascript jqgrid中带有自定义函数调用的超链接/按钮

Javascript jqgrid中带有自定义函数调用的超链接/按钮,javascript,jquery,jqgrid,Javascript,Jquery,Jqgrid,我想在jqgrid的每一行中添加一个超链接/按钮,它触发一个自定义javascript函数。厌倦了各种考验 jQuery('#ProductListGrid').jqGrid({ url: '/Product/ProductListGrid', datatype: 'json', multiselect: true, height: 250, autowidth: true, mtype: 'GET', loadComplete: addl

我想在jqgrid的每一行中添加一个超链接/按钮,它触发一个自定义javascript函数。厌倦了各种考验

jQuery('#ProductListGrid').jqGrid({
    url: '/Product/ProductListGrid',
    datatype: 'json',
    multiselect: true,
    height: 250,
    autowidth: true,
    mtype: 'GET',
    loadComplete: addlinks,
    colNames: ['ProductId', 'ProductName', 'edit'],
    colModel: [
      { name: 'ProductId', index: 'ProductId',key:true, width: 70, hidden: true, editable: true, size: 5 },
      { name: 'ProductName', index: 'ProductName', width: 70, editable: true },
      { name: 'edit', index: 'edit', width: 70},

    ],
    pager: jQuery('#ProductListGrid_pager'),
});
function addlinks() {
    var ids = jQuery("#ProductListGrid").getDataIDs();
    alert(ids);
    for (var i = 0; i < ids.length; i++) {
        be = "<a href='#' style='height:25px;width:120px;' type='button' title='Slet' onclick=\"ff()\" >Slet</>";
        jQuery("#ProductListGrid").jqGrid('setCell', ids[i],'edit','', { act: be });
    }
    //for (var i = 0; i < ids.length; i++) {
    //    jQuery("#ProductListGrid").setCell(i, 'edit', '<a href="#">edit</edit>');
    //}
}
function ff()
{
    alert("hi");
}
jQuery('#ProductListGrid').jqGrid({
url:“/Product/ProductListGrid”,
数据类型:“json”,
多选:对,
身高:250,
自动宽度:正确,
mtype:'获取',
loadComplete:addlinks,
colNames:['ProductId','ProductName','edit'],
colModel:[
{name:'ProductId',index:'ProductId',key:true,width:70,hidden:true,editable:true,size:5},
{name:'ProductName',index:'ProductName',宽度:70,可编辑:true},
{name:'edit',index:'edit',宽度:70},
],
pager:jQuery(“#ProductListGrid_pager”),
});
函数addlinks(){
var id=jQuery(“#ProductListGrid”).getDataId();
警报(ids);
对于(变量i=0;i

正在执行addlinks中的代码,但列中没有显示任何内容。我也尝试过使用自定义格式,但我无法显示自定义文本“编辑”和链接单击不会触发js方法。

您需要为编辑列调用格式设置程序,如下所示:

格式化程序:addLink
添加到最后一列:

colModel: [
      { name: 'ProductId', index: 'ProductId',key:true, width: 70, hidden: true, editable: true, size: 5 },
      { name: 'ProductName', index: 'ProductName', width: 70, editable: true },
      { name: 'edit', index: 'edit', width: 70,formatter: addLink},

    ]
添加javascript函数:

function addLink(cellvalue, options, rowObject) 
{
  //to get row Id
  alert(options.rowId);
  // to get product Id
  alert(rowObject.ProductId);
  return "<a href='#' style='height:25px;width:120px;' type='button' title='Slet' onclick=\"ff()\" >Slet</a>";
}
函数addLink(单元格值、选项、行对象)
{
//获取行Id的步骤
警报(options.rowId);
//获取产品Id的步骤
警报(rowObject.ProductId);
返回“”;
}

有关的详细信息。

您是否尝试过
格式化程序
?如果是这样,您可以共享它们或提供…我在网格中添加了另一列,其中显示链接{name:'ProductId',formatter:'showlink',formatoptions:{baseLinkUrl:'/Product/ProductEdit/',addParam:'&action=edit'}我建议您阅读并演示如何使用1)自定义格式化程序在“编辑”列中放置一些文本/链接/按钮,以及2)如何使用
beforeSelectRow
回调而不是
onclick
属性来执行任何JavaScript代码,单击链接/按钮…
beforeSelectRow
回调拥有第二个参数
e
,用于提供有关单击元素的信息<代码>$(e.target)。最近的(“td”)
将是单击的单元格,
$(e.target)。最近的(“tr”)
将是单击的行,
$(e.target)。最近的(“td”)。attr(“id”)
将是行id,依此类推<代码>var iCol=$.jgrid.getCellIndex($(e.target).closest(“td”)[0])是单击的单元格的列号。无论如何,我严格建议您使用
gridview:true
选项(请参阅)来提高性能。使用
addlinks
会使网格变慢。谢谢,这会激发该方法。您能帮助我如何将id传递给函数吗?是否要将productId传递给ff函数?是的。产品ID和行ID,如果我需要任何其他信息,您可以在addLink函数中使用rowObject(如rowObject.ProductId或rowObject.ProductName)访问当前行的任何列。并使用选项获取行ID(如options.rowId)