如何不生成工具栏中选择元素上下文菜单中的jqGrid*以下

问题描述:

代码是用来生成基于在回答*工具栏的上下文菜单How to change column name in column chooser pop up in jqgrid?如何不生成工具栏中选择元素上下文菜单中的jqGrid*以下

工具栏还包含选择元素。在上下文菜单中显示为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://*.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> 

更新

Expceted结果是,最后一行没有出现在上下文菜单:

expected

+0

首先,我认为你应该减少'caption:'的值,直到'selectElem +'''。我可以在上下文菜单中包含按钮中的HTML片段。 ''在菜单中的简单放置不会这么做,因为菜单项内的“click”将关闭项目,而不是在菜单项内部(在菜单上)打开下拉选择。 – Oleg 2015-04-05 15:21:26

+0

预期的行为不是在上下文菜单中呈现它,或者使它像在工具栏中一样选择:第一次单击打开下拉菜单,第二次单击选择项并调用onchange – Andrus 2015-04-05 15:57:58

+0

您不应该忘记该菜单是由'contextMenu'插件创建的。第一次点击将由'contextMenu'插件处理,该插件隐藏菜单,然后调用菜单项的绑定功能。创建'createContexMenuFromNavigatorButtons'的绑定函数模拟原始工具栏菜单中div的'click'。所有这些只有按钮才有意义,但不能与'

我更新createContexMenuFromNavigatorButtons插件(见here),因此它把HTML内容.ui-pg-button-text导航按钮的一部分用于构建上下文菜单。如果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 
     } 
    }); 

进行上下文菜单的额外定制。

+0

谢谢。选择元素不会在上下文菜单中显示。这是我想要的。 – Andrus 2015-04-06 16:00:28

+0

@安德鲁斯:不客气! – Oleg 2015-04-06 16:02:42