如何将标准文本框命令添加到jqgrid上下文菜单

时间:2011-12-10 15:16:50

标签: jquery-ui jqgrid contextmenu

如果使用Custom values to Context Menu Items in JQgrid将上下文菜单添加到jqGrid并使用文本字段内联编辑,则文本框标准上下文菜单不可用,它将替换为jqGrid上下文菜单。

如何将标准文本框上下文菜单命令(撤消,剪切,复制,粘贴,删除,全选)添加到jqGrid conext菜单或如何显示文本框内联编辑的标准上下文菜单?

更新

在内联编辑中,如果通过右键单击黄色背景或自动完成框打开标准菜单,并且在打开标准浏览器上下文菜单后,自定义菜单未关闭,则会出现两个菜单。

two menus

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

在“复制”,“粘贴”等上下文菜单命令中实现起来并不容易,所以我决定在前一个问题上从the answer修改我的演示。在the new demo中,仅当页面不包含所选文本时才会显示上下文菜单。

第一个问题是jquery.contextmenu.js的原始代码包含以下代码片段:

$(this).bind('contextmenu', function(e) {
  // Check if onContextMenu() defined
  var bShowContext = (!!hash[index].onContextMenu) ? hash[index].onContextMenu(e) : true;
  if (bShowContext) display(index, this, e, options);
  return false;
});

因此contextmenu处理程序始终返回false并阻止创建标准上下文菜单。我将代码修改为以下内容(您可以下载完整修改后的代码here):

$(this).bind('contextmenu', function(e) {
  // Check if onContextMenu() defined
  var bShowContext = (!!hash[index].onContextMenu) ? hash[index].onContextMenu(e) : true;
  currentTarget = e.target;
  if (bShowContext) {
    display(index, this, e, options);
    return false;
  }
});

createContexMenuFromNavigatorButtons函数的代码here我修改了

onContextMenu: function (e) {
    var rowId = $(e.target).closest("tr.jqgrow").attr("id"), p = grid[0].p, i,
        lastSelId;

    if (rowId && getSelectedText() === '') {
        ...
        return true;
    } else {
        return false; // no contex menu
    }
}

使用getSelectedText()并仅在未选择文本时创建上下文菜单。因此,只有在没有选择文本时才会看到自定义上下文菜单,如果存在文本选择,则会看到标准上下文菜单(依赖于Web浏览器):

enter image description here

更新:我根据答案修改了jquery.contextmenu.js的{​​{1}}错误报告。我希望很快就会在jquery.contextmenu.js子目录中包含的plugins主代码中进行更改。

更新2 :您如何看到additional information所有修复已经在here的jqGrid主代码中,并且包含在jqGrid 4.3中。

更新3 :如果您想为所有启用 <input type="text" ...><input type="textarea" ...><textarea ...>元素设置标准上下文菜单你应该只修改onContextMenu回调中的一些代码。例如

onContextMenu: function (e) {
    var p = grid[0].p, i, lastSelId,
        $target = $(e.target),
        rowId = $target.closest("tr.jqgrow").attr("id"),
        isInput = $target.is(':text:enabled') ||
        $target.is('input[type=textarea]:enabled') ||
        $target.is('textarea:enabled');
    if (rowId && !isInput && getSelectedText() === '') {
        ...

再看一个github,双击即可激活内联编辑。

相关问题