如何设置 doubleClick 函数来调用 jqGrid 中的自定义函数

发布于 2024-12-17 08:26:32 字数 2680 浏览 2 评论 0原文

我在 jqGrid 导航器上添加了一个自定义编辑按钮控件,如下所示:

jQuery("#grid").navButtonAdd('#pager',
    {  
     caption:"Edit", 
     buttonicon:"ui-icon-pencil", 
     onClickButton: editSelectedRow,
     position: "last", 
     title:"click to edit selected row", 
     cursor: "pointer",
     id: "edit-row"
    } 
  );

因此,它不使用默认函数 editGridRow,而是使用我的自定义函数 editSelectedRow。但是,我还想添加 doubleClick 函数,以便它在 doubleClick 上调用 editSelectedRow。

使用默认的 editGridRow 函数是这样工作的

ondblClickRow: function()
      {
       var rowid = jQuery("#grid").jqGrid('getGridParam','selrow');
       jQuery(this).jqGrid('editGridRow', rowid);
      }

但是,当我用默认函数 editSelectedRow 替换默认的 editGridRow 函数时,

ondblClickRow: function()
      {
       var rowid = jQuery("#grid").jqGrid('getGridParam','selrow');
       jQuery(this).jqGrid('editSelectedRow', rowid);
      }

我在 firebug 中收到以下错误:

uncaught exception: jqGrid - No such method: editSelectedRow

然而,函数 editSelectedRow 确实存在,并且可以通过单击自定义编辑按钮来使用。请帮忙,谢谢。

更新: @Oleg:根据要求,这里是定义方法的代码:editSelectedRow

function editSelectedRow(rowid)
{
    var rowid = jQuery("#grid").jqGrid('getGridParam','selrow');
    if( rowid != null ) 
    {
        var dialogId = '#edit-form-dialog';
        var dialogTitle = 'Edit Customer';

        $(dialogId).load('/customer/edit/id/' + rowid, function () 
        {
            $(this).dialog(
            {
                modal: false,
                resizable: true,
                minWidth: 650,
                minHeight: 300,
                height: $(window).height() * 0.95,
                title: dialogTitle,
                buttons: 
                    {
                    "Save": function () 
                        {
                        var form = $('form', this);
                        $(form).submit();
                        $("#grid").trigger("reloadGrid");
                    },
                    "Cancel": function () 
                    {
                        $("#grid").trigger("reloadGrid");
                        $(this).dialog('close');
                    }
                }
          });

            LaunchEditForm(this);
        });
    }
    else
    {
        jQuery( "#dialogSelectRow" ).dialog();
    }
    return false;
 }

@Oleg:谢谢,您建议不要使用自定义方法 editSelectedRow 来代替 editGridRow 方法。我使用它的原因是我的表单是 Zend Forms 并且我需要 Zend Form 的所有功能都可用。服务器生成此表单并将其加载到对话框表单中。如果有一种方法可以在不诉诸我的 editSelectedRow 自定义方法的情况下仍然实现此目的,我会很高兴学习它。谢谢。

I have added a custom edit button control on the jqGrid navigator as follows:

jQuery("#grid").navButtonAdd('#pager',
    {  
     caption:"Edit", 
     buttonicon:"ui-icon-pencil", 
     onClickButton: editSelectedRow,
     position: "last", 
     title:"click to edit selected row", 
     cursor: "pointer",
     id: "edit-row"
    } 
  );

So that rather than use the default function: editGridRow, it uses my custom function editSelectedRow. However, I also want to add the doubleClick function to so that it calls editSelectedRow on doubleClick.

using the default editGridRow function works as such

ondblClickRow: function()
      {
       var rowid = jQuery("#grid").jqGrid('getGridParam','selrow');
       jQuery(this).jqGrid('editGridRow', rowid);
      }

However, when I replace the default editGridRow function with my default function editSelectedRow as such,

ondblClickRow: function()
      {
       var rowid = jQuery("#grid").jqGrid('getGridParam','selrow');
       jQuery(this).jqGrid('editSelectedRow', rowid);
      }

I get the following error within firebug:

uncaught exception: jqGrid - No such method: editSelectedRow

The function editSelectedRow however does exist and works with clicking the custom edit button. Please help, thanks.

UPDATE:
@Oleg: As requested here's the code defining method: editSelectedRow

function editSelectedRow(rowid)
{
    var rowid = jQuery("#grid").jqGrid('getGridParam','selrow');
    if( rowid != null ) 
    {
        var dialogId = '#edit-form-dialog';
        var dialogTitle = 'Edit Customer';

        $(dialogId).load('/customer/edit/id/' + rowid, function () 
        {
            $(this).dialog(
            {
                modal: false,
                resizable: true,
                minWidth: 650,
                minHeight: 300,
                height: $(window).height() * 0.95,
                title: dialogTitle,
                buttons: 
                    {
                    "Save": function () 
                        {
                        var form = $('form', this);
                        $(form).submit();
                        $("#grid").trigger("reloadGrid");
                    },
                    "Cancel": function () 
                    {
                        $("#grid").trigger("reloadGrid");
                        $(this).dialog('close');
                    }
                }
          });

            LaunchEditForm(this);
        });
    }
    else
    {
        jQuery( "#dialogSelectRow" ).dialog();
    }
    return false;
 }

@Oleg: Thanks, you advised against using a custom method editSelectedRow in place of method editGridRow. The reason I am using this is that my forms are Zend Forms and I need all the bells and whistles of Zend Form to be available. The server generates this form and it's loaded into a dialog form. If there's a way to still achieve this without resorting to my editSelectedRow custom method, I'd be glad to learn it. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-12-24 08:26:32

你的问题是纯粹的JavaScript问题。

如果您定义函数 editSelectedRow ,则

function editSelectedRow(rowid)
{
    ...
}

可以将其称为 editSelectedRow(rowid) 而不是 jQuery(this).jqGrid('editSelectedRow', rowid);

另一个问题是您在 editSelectedRow 函数体内使用 this 。这是不正确的。您可以用另一种方式定义 editSelectedRow 函数,

var editSelectedRow = function (rowid) {
    ...
};

在这种情况下,editSelectedRow 将能够将 this 绑定到任何值。为此,您需要使用另一种形式的函数调用。在 ondblClickRow 内部,它将是

ondblClickRow: function () {
    var rowid = jQuery("#grid").jqGrid('getGridParam','selrow');
    editSelectedRow.call(this, rowid);
}

在上面的示例中,call 的第一个参数是在函数内部用作 this 的值。我们仅将当前的 this 值转发到 editSelectedRow。如果我们使用 editSelectedRow(rowid); 形式来调用函数,函数内部的 this 值将被初始化为 window目的。

navButtonAddeditSelectedRow 的用法可以保持不变。

You question is pure JavaScript question.

If you define the function editSelectedRow as

function editSelectedRow(rowid)
{
    ...
}

you can call it as editSelectedRow(rowid) and not as jQuery(this).jqGrid('editSelectedRow', rowid);.

Another problem is that you use this inside of he body of editSelectedRow function. It's not correct. You can define editSelectedRow function in a little another way

var editSelectedRow = function (rowid) {
    ...
};

In the case editSelectedRow will be able to bind this to any value. To do this you need use another form of invocation of the function. Inside of ondblClickRow it will be

ondblClickRow: function () {
    var rowid = jQuery("#grid").jqGrid('getGridParam','selrow');
    editSelectedRow.call(this, rowid);
}

In the above example the first parameter of call is the value used as this inside of the function. We forward just the current this value forward to editSelectedRow. If we would use the form editSelectedRow(rowid); for the invocation of the function the value of this inside of function will be initialized to window object.

The usage of editSelectedRow inside of navButtonAdd can stay unchanged.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文