【问题标题】:How can I set up a contextMenu for my jqGrid?如何为我的 jqGrid 设置 contextMenu?
【发布时间】:2016-01-05 09:07:32
【问题描述】:

我在我们的网站上有一个供内部使用的 jqGrid,它列出了我们所有的用户。在每个用户/行上,我希望能够应用各种选项(取决于该行中的数据)。与其向寻呼机添加导航按钮,不如在右键单击一行时出现一个上下文菜单更有意义。

我们目前在我们的网站上导入了this jQuery contextMenu plugin,因此最好以某种方式将其集成到我的 jqGrid 中。

我的 jqGrid 缩小到基础看起来像这样:

$("#users").jqGrid({
    datatype: 'json',
    url: 'myMethodURL',
    gridview: true,
    colModel: [
        {name: 'id', label: 'User ID', hidden:true},
        {name: 'lastname', label: 'Last Name'},
        {name: 'firstname', label: 'First Name'},
        {name: 'status', label: 'Status', stype: 'select', searchoptions: {value: ':All;ACTIVE:Active;INACTIVATED:Inactive;PENDING APPROVAL:Pending Approval;'}},
        ... more fields ...
      ],
    height:'auto',
    autowidth:true,
    caption:'Users',
    rowNum:20,
    rowList:[10,20,50],
    sortorder:'asc',
    sortname: 'lastname',
    ignoreCase: true, // case-insensitive filtering
    pager: '#pager',
    jsonReader: {
        root: "ROWS", //our data
        page: "PAGE", //current page
        total: "TOTAL", //total pages
        records:"RECORDS", //total records
        cell: "", //not used
        id: "0" //will default first column as ID
    },
    postData: postData
});
$("#users").jqGrid("filterToolbar", {searchOnEnter: true});

上下文菜单中我需要的一些选项:

  1. 激活或停用(取决于用户当前是否处于活动/非活动状态 - 基本上需要切换)
  2. 处理待处理用户(应仅在用户状态为“待处理”时显示)
  3. 编辑用户信息
  4. 发送重置密码链接

如何设置带有可变选项的上下文菜单(取决于该特定行的值),并定义单击选项时会发生什么?

【问题讨论】:

    标签: jquery jqgrid contextmenu


    【解决方案1】:

    总的来说,the jQuery contextMenu plugin 与 jqGrid 的用法在我看来非常简单。您可以将菜单绑定到网格体。只需知道rowid 是<tr> 元素的id 属性的值,具有真实数据的tr 元素具有.jqgrow 类。

    因此代码可能如下所示

    $("#users").jqGrid({
        datatype: 'json',
        ...
    }).contextMenu({
        selector: ".jqgrow", 
        build: function ($trigger, e) {
            // this callback is executed every time the menu is to be shown
            // its results are destroyed every time the menu is hidden
            // e is the original contextmenu event
    
            var $tr = $(e.target).closest("tr.jqgrow"),
                rowid = $tr.attr("id"),
                item = $grid.jqGrid("getRowData", rowid);
    
            // item contains now the data of the row and we can
            // build the context menu dynamically
            if (item.status === "ACTIVE") {
                return {
                    callback: function (key, options) {
                        var m = "clicked: " + key + " on rowid=" + rowid +
                                " (" + item.firstname + " " + item.lastname + ")";
                        alert(m); 
                    },
                    items: {
                        edit: {name: "Edit", icon: "edit"},
                        cut: {name: "Cut", icon: "cut"},
                        copy: {name: "Copy", icon: "copy"},
                        paste: {name: "Paste", icon: "paste"},
                        delete: {name: "Delete", icon: "delete"},
                        sep1: "---------",
                        quit: {name: "Quit", icon: function($element, key, item) {
                                return 'context-menu-icon context-menu-icon-quit';
                              }}
                    }
                };
            } else {
                return {
                    callback: function (key, options) {
                        var m = "clicked: " + key + " on rowid=" + rowid +
                                " (" + item.firstname + " " + item.lastname + ")";
                        alert(m); 
                    },
                    items: {
                        delete: {name: "Delete", icon: "delete"},
                        sep1: "---------",
                        quit: {name: "Quit", icon: function($element, key, item) {
                                return 'context-menu-icon context-menu-icon-quit';
                              }}
                    }
                };
            }
        }
    });
    

    查看演示 https://jsfiddle.net/OlegKi/37rb593h/。您可以根据自己的需要修改build回调的代码。

    【讨论】:

    • 谢谢,这看起来很棒!我将看看是否可以合并动态“项目”代码,这样我就不必为每个逻辑更改重复所有菜单选项(只有单个菜单项需要更改),但这是一个非常有用的起点和例子。
    • @froadie:不客气!我不确定你是否知道另一个插件:createContexMenuFromNavigatorButtons。通常我会在导航栏中添加按钮以执行不同的操作。例如,它对于支持触摸设备很实用。可以根据所选行隐藏/禁用某些按钮。 createContexMenuFromNavigatorButtons 方法基于 nav.bar 的活动按钮构建上下文菜单。查看为the answer创建的the demo上下文菜单的“保存”菜单项
    • 我对您在 jsfiddle 中使用的 CDN 中的 contextmenu 插件的版本感到困惑。它似乎是 2.0.1 版本,但可供下载的最新版本是 1.6.6 ...
    • @froadie:你把plugins 的链接发给我,这个链接已经过时一年多了。旧的插件站点仍然可用,但它是只读的。 jQuery 推荐使用npmjs.com,但bower.io 也是众所周知的。插件上的链接包含对GitHub 的引用,这是开发的起源。开发人员对 git 标签使用不填充标准版本号(2.0.1 而不是 v2.0.1),但所有版本都存在于此。您可以从 CDN 下载或使用。
    • @froadie:如果您编写的应用程序可以从 Internet 获得,那么 CDN 是最佳选择。我不知道你使用的是哪个开发框架,但是现代的 Intranet 应用程序就像here,其中一个使用<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js" asp-fallback-src="~/lib/jquery/dist/jquery.min.js" asp-fallback-test="window.jQuery"></script>。换句话说,应该使用 CDN 作为 main 源,并使用本地文件作为 fallback 源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2011-07-29
    • 2014-09-21
    • 1970-01-01
    相关资源
    最近更新 更多