如何在free jqgrid中为工具栏选择元素生成上下文菜单

时间:2015-04-05 11:56:53

标签: jquery css jqgrid free-jqgrid

以下代码用于根据答案How to change column name in column chooser pop up in jqgrid?

从顶级工具栏生成上下文菜单

工具栏还包含select元素。它在上下文菜单中显示为Form0 ... Form9,并使上下文菜单更宽。 如何从上下文菜单中排除它?

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>https://stackoverflow.com/q/29365758/315935</title>
    <meta name="author" content="Oleg Kiriljuk">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/redmond/jquery-ui.css">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
    <!--<link rel="stylesheet" type="text/css" href="http://cdn.jsdelivr.net/free-jqgrid/4.8.0/css/ui.jqgrid.css"/>-->
    <!--<link rel="stylesheet" href="../jqGrid/css/ui.jqgrid.css">-->
    <link rel="stylesheet" href="http://rawgit.com/free-jqgrid/jqGrid/master/css/ui.jqgrid.css"/>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="http://cdn.jsdelivr.net/free-jqgrid/4.8.0/plugins/ui.multiselect.css">
    <style>
        html, body { font-size: 75%; }
        .ui-datepicker select.ui-datepicker-year,
        .ui-datepicker select.ui-datepicker-month {
            color: black
        }
    </style>
    <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
    <!--<script src="http://cdn.jsdelivr.net/free-jqgrid/4.8.0/js/i18n/grid.locale-en.js"></script>-->
    <script src="http://rawgit.com/free-jqgrid/jqGrid/master/js/i18n/grid.locale-en.js"></script>
    <script>
        $.jgrid = $.jgrid || {};
        $.jgrid.no_legacy_api = true;
        $.jgrid.useJSON = true;
    </script>
    <!--<script src="../jqGrid/js/jquery.jqGrid.src.js"></script>-->
    <!--<script type="text/javascript" src="http://cdn.jsdelivr.net/free-jqgrid/4.8.0/js/jquery.jqgrid.src.js"></script>-->
    <script src="http://rawgit.com/free-jqgrid/jqGrid/master/js/jquery.jqgrid.src.js"></script>
    <script src="http://rawgit.com/free-jqgrid/jqGrid/master/plugins/jquery.jqgrid.showhidecolumnmenu.js"></script>
    <script src="http://rawgit.com/free-jqgrid/jqGrid/master/plugins/jquery.contextmenu-ui.js"></script>
    <script src="http://rawgit.com/free-jqgrid/jqGrid/master/plugins/jquery.createcontexmenufromnavigatorbuttons.js"></script>
    <script src="http://cdn.jsdelivr.net/free-jqgrid/4.8.0/plugins/ui.multiselect.js"></script>
    <script>
    //<![CDATA[
        /*global $ */
        /*jslint browser: true, unparam: true */
        $(function () {
            "use strict";
            var mydata = [
                    { id: "10",  invdate: "2007-10-01", name: "test1",  note: "note1",  amount: "200.00", tax: "10.00", closed: true,  ship_via: "TN", total: "210.00" },
                    { id: "20",  invdate: "2007-10-02", name: "test2",  note: "note2",  amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" },
                    { id: "30",  invdate: "2007-09-01", name: "test3",  note: "note3",  amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" },
                    { id: "40",  invdate: "2007-10-04", name: "test4",  note: "note4",  amount: "200.00", tax: "10.00", closed: true,  ship_via: "TN", total: "210.00" },
                    { id: "50",  invdate: "2007-10-31", name: "test5",  note: "note5",  amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" },
                    { id: "60",  invdate: "2007-09-06", name: "test6",  note: "note6",  amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" },
                    { id: "70",  invdate: "2007-10-04", name: "test7",  note: "note7",  amount: "200.00", tax: "10.00", closed: true,  ship_via: "TN", total: "210.00" },
                    { id: "80",  invdate: "2007-10-03", name: "test8",  note: "note8",  amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" },
                    { id: "90",  invdate: "2007-09-01", name: "test9",  note: "note9",  amount: "400.00", tax: "30.00", closed: false, ship_via: "TN", total: "430.00" },
                    { id: "100", invdate: "2007-09-08", name: "test10", note: "note10", amount: "500.00", tax: "30.00", closed: true,  ship_via: "TN", total: "530.00" },
                    { id: "110", invdate: "2007-09-08", name: "test11", note: "note11", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" },
                    { id: "120", invdate: "2007-09-10", name: "test12", note: "note12", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" }
                ],
                $grid = $("#list"),
                initDateEdit = function (elem) {
                    $(elem).datepicker({
                        dateFormat: "dd-M-yy",
                        autoSize: true,
                        changeYear: true,
                        changeMonth: true,
                        showButtonPanel: true,
                        showWeek: true
                    });
                },
                initDateSearch = function (elem) {
                    setTimeout(function () {
                        initDateEdit(elem);
                    }, 50);
                };

            $grid.jqGrid({
                data: mydata,
                colNames: ["Client", "Date", "Closed", "Shipped via", "Amount", "Tax", "Total", "Notes"],
                colModel: [
                    { name: "name", align: "center", width: 65, editrules: {required: true} },
                    { name: "invdate", width: 125, align: "center", sorttype: "date",
                        formatter: "date", formatoptions: { newformat: "d-M-Y" },
                        editoptions: { dataInit: initDateEdit },
                        searchoptions: { sopt: ["eq", "ne", "lt", "le", "gt", "ge"], dataInit: initDateSearch } },
                    { name: "closed", width: 70, template: "booleanCheckboxFa" },
                    { name: "ship_via", width: 105, align: "center", formatter: "select",
                        edittype: "select", editoptions: { value: "FE:FedEx;TN:TNT;IN:Intim", defaultValue: "IN" },
                        stype: "select", searchoptions: { sopt: ["eq", "ne"], value: ":Any;FE:FedEx;TN:TNT;IN:IN" } },
                    { name: "amount", width: 75, template: "number" },
                    { name: "tax", width: 52, template: "number" },
                    { name: "total", width: 60, template: "number" },
                    { name: "note", width: 60, sortable: false, edittype: "textarea" }
                ],
                rowNum: 10,
                rowList: [5, 10, 20],
                iconSet: "fontAwesome",
                toppager: true,
                pager: true,
                cmTemplate: { autoResizable: true, editable: true },
                autoResizing: { compact: true },
                //autoresizeOnLoad: true,
                autoencode: true,
                sortname: "invdate",
                viewrecords: true,
                sortorder: "desc",
                rownumbers: true,
                hidegrid: false,
                caption: "Try right mouse click inside of the grid body"
            }).jqGrid("navGrid")
            .jqGrid("setGroupHeaders", {
                useColSpanStyle: true,
                groupHeaders: [
                    { startColumnName: "amount", numberOfColumns: 3, titleText: "<em>Price</em>" },
                    { startColumnName: "closed", numberOfColumns: 2, titleText: "Shiping" }
                ]
            })

            .jqGrid("showHideColumnMenu")
            .jqGrid("createContexMenuFromNavigatorButtons", $grid.jqGrid("getGridParam", "toppager"));



        var i, layout = 1,
            selectElem= '<select tabindex="-1" id="_layout">';

        for (i=0; i<10; i++) {
            selectElem += '<option value="'+i+'" ';
            if (i==layout)
                selectElem += ' selected';
            selectElem += '>Form ' + i + '</option>'
        }

        $grid.jqGrid('navButtonAdd', '#list_toppager', {
            caption:  '<td class="ui-pg-button ui-corner-all">' +
               '<div class="ui-pg-div">' +
               selectElem +
               '</select>' +
               '</div></td>',
            buttonicon:'',
        });



        });
    //]]>
    </script>
</head>
<body>
<div id="outerDiv" style="margin:5px;">
    <table id="list"></table>
</div>
</body>
</html>

更新

结果导致最后一行未出现在上下文菜单中:

expected

1 个答案:

答案 0 :(得分:1)

我更新了createContexMenuFromNavigatorButtons插件(请参阅here),以便将.ui-pg-button-text部分导航按钮的HTML内容放置以构建上下文菜单。如果HTML内容中包含<select><input>,则默认情况下将隐藏此类菜单项。这是您在问题的更新部分所需的内容。保持插件足够常见我包含onShowContextMenu回调。所以你仍然可以使用

.jqGrid("createContexMenuFromNavigatorButtons",
    $grid.jqGrid("getGridParam", "toppager"),
    {
        onShowContextMenu: function ($menuUl, options) {
            // do customization of menu $menuUl
            // or modification of options of contextMenu
            // like modifications of options.bindings or example
        }
    });

进行上下文菜单的其他自定义。