javascript - 将 Id 传递给 Onclick 函数 JQGrid

标签 javascript jquery asp.net jqgrid

我有一个 JQGrid。我需要将一些 Id 带入 OnClick 函数。在我的场景中,我想将 BasicId 带入 OnClick 函数。

我的代码

 function grid() {
    //JqGrid
    $('#griddata').html('<table class="table" id="jqgrid"></table>')
    $('#jqgrid').jqGrid({

        url: '/Admin/GetBasicData/',

        datatype: 'json',
        mtype: 'GET',
        //columns names
        colNames: ['BasicId','Images'],
        //columns model
        colModel: [

                    { name: 'BasicId', index: 'BasicId', resizable: false },


                     {
                         name: 'Images',
                         width: 120,

                         formatter: function () {
                             return "<button class='btn btn-warning btn-xs' onclick='OpenDialog()' style='margin-left:30%'>View</button>";
                         }
                     },

//Some Code here

打开对话框功能

function OpenDialog(BasicId)
{
//Some code here
}

最佳答案

您可以使用 onclick='OpenDialog.call(this, event)'而不是 onclick='OpenDialog()' .你将有 this OpenDialog 内部初始化为被点击的 <button>event.target .因此您的代码可能如下所示

function OpenDialog (e) {
    var rowid = $(this).closest("tr.jqgrow").attr("id"),
        $grid = $(this).closest(".ui-jqgrid-btable"),
        basicId = $grid.jqGrid("getCell", rowid, "BasicId");

    // ...

    e.stopPropagation();
}

多一个选项就更好了:您不需要指定任何 onclick .而不是你可以使用 beforeSelectRow callback jqGrid 的:

beforeSelectRow (rowid, e) {
    var $td = $(e.target).closest("td"),
        iCol = $.jgrid.getCellIndex($td[0]),
        colModel = $(this).jqGrid("getGridParam", "colModel"),
        basicId = $(this).jqGrid("getCell", rowid, "BasicId");
    if (colModel[iCol].name === "Images") { // click in the column "Images"
        // one can make additional test for 
        //   if (e.target.nodeName.toUpperCase() === "button")
        // to be sure that it was click to the button
        // and not the click on another part of the column
        OpenDialog(rowid);
        return false; // don't select the row - optional
    }
}

最后一种方式的主要优点是:不需要再做任何额外的绑定(bind)(每次绑定(bind)都会获取内存资源,需要时间)。 click 上已经存在网格中的处理程序,可以使用它。由于事件冒泡,只需一个单击处理程序就足够了。 e.target向我们提供有关被点击元素的完整信息。

关于javascript - 将 Id 传递给 Onclick 函数 JQGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29888064/

相关文章:

javascript - Hovercard 在带绑定(bind)的 knockoutJS 中不显示

javascript - 仅在为其 margin-left 设置动画时更改图像 Src

c# - 如何使用 .NET Standard 和 Entity Framework Core 为现有数据库创建 Entity Framework edmx 文件?

c# - 如何在 ASP.NET Web.Api 中运行后台任务?

javascript - 为什么 CDATA 不保护我的 JavaScript 代码不受 HTML 验证器的影响?

javascript - 我无法在 jQuery 中使用 ".child"或 ".next"选择正确的 div

JavaScript 选中多个复选框

c# - 数据表和 View 状态

javascript - 浏览器中的 GPU 加速数学

javascript - 基于 webkit 的浏览器将 json 解释为脚本