如何设置 doubleClick 函数来调用 jqGrid 中的自定义函数
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是纯粹的JavaScript问题。
如果您定义函数
editSelectedRow
,则可以将其称为
editSelectedRow(rowid)
而不是jQuery(this).jqGrid('editSelectedRow', rowid);
。另一个问题是您在
editSelectedRow
函数体内使用this
。这是不正确的。您可以用另一种方式定义editSelectedRow
函数,在这种情况下,
editSelectedRow
将能够将this
绑定到任何值。为此,您需要使用另一种形式的函数调用。在ondblClickRow
内部,它将是在上面的示例中,
call
的第一个参数是在函数内部用作this
的值。我们仅将当前的this
值转发到editSelectedRow
。如果我们使用editSelectedRow(rowid);
形式来调用函数,函数内部的this
值将被初始化为window
目的。navButtonAdd
中editSelectedRow
的用法可以保持不变。You question is pure JavaScript question.
If you define the function
editSelectedRow
asyou can call it as
editSelectedRow(rowid)
and not asjQuery(this).jqGrid('editSelectedRow', rowid);
.Another problem is that you use
this
inside of he body ofeditSelectedRow
function. It's not correct. You can defineeditSelectedRow
function in a little another wayIn the case
editSelectedRow
will be able to bindthis
to any value. To do this you need use another form of invocation of the function. Inside ofondblClickRow
it will beIn the above example the first parameter of
call
is the value used asthis
inside of the function. We forward just the currentthis
value forward toeditSelectedRow
. If we would use the formeditSelectedRow(rowid);
for the invocation of the function the value ofthis
inside of function will be initialized towindow
object.The usage of
editSelectedRow
inside ofnavButtonAdd
can stay unchanged.