2011-12-12 97 views
2

切换按钮使用奥列格答案来自how to add standard textbox command to jqgrid context menu如何从jqGrid的工具栏上的jqGrid顶部工具栏添加切换按钮,自动生成上下文菜单

var autoedit; 
$("#grid_toppager_left table.navtable tbody tr").append(
    '<td class="ui-pg-button ui-corner-all" >' + 
     '<div class="ui-pg-div my-nav-checkbox">' + 
     '<input tabindex="-1" type="checkbox" id="AutoEdit" '+(autoedit ? 'checked ' : '')+'/>' + 
     '<label title="Toggle autoedit" for="AutoEdit">this text is ignored in toolbar</label></div></td>' 
); 
$("#AutoEdit").button({ 
    text: false, 
    icons: {primary: "ui-icon-star"} 
}).click(function() { 
    autoedit = $(this).is(':checked'); 
}); 

答案已被用于自动生成上下文菜单工具栏自定义网格。

在为此项目生成的上下文菜单中,只显示文本“此文本在工具栏中被忽略”并选择它不执行任何操作。

如何使其工作或从上下文菜单中删除此项目?

+1

我更新了我的答案。 – Oleg

回答

6

the demo或与另一主题相同的演示:thisthis

enter image description here

enter image description here

enter image description here

首先我修改了jquery.contextmenu.js的代码来支持jQuery UI的主题。然后,我更多地修改了代码,以便能够更动态地创建上下文菜单。在jquery.contextmenu.js的修改版本中,不仅可以在onContextMenu中创建菜单和绑定,还可以在onShowMenu中创建菜单和绑定。里面的onContextMenu一个只能创建空的菜单

<div id="myMenu"><ul></ul></div> 

是很重要的,如果只用一个动态的按钮,从导航条的文本图标的切换。

您可以下载文件here的修改版本。

在演示中,我使用了the answer代码的最后一次修改,因此标准上下文菜单仍可以在选定文本的网格中或启用的input/textarea字段中使用。浏览器的上下文菜单将显示在该情况下:

enter image description here

我改性createContexMenuFromNavigatorButtons函数的代码从the answer为以下:

var getSelectedText = function() { 
     var text = ''; 
     if (window.getSelection) { 
      text = window.getSelection(); 
     } else if (document.getSelection) { 
      text = document.getSelection(); 
     } else if (document.selection) { 
      text = document.selection.createRange().text; 
     } 
     return typeof (text) === 'string' ? text : text.toString(); 
    }, 
    createContexMenuFromNavigatorButtons = function (grid, pager) { 
     var menuId = 'menu_' + grid[0].id, menuUl = $('<ul>'), 
      menuDiv = $('<div>').attr('id', menuId); 

     menuUl.appendTo(menuDiv); 
     menuDiv.appendTo('body'); 

     grid.contextMenu(menuId, { 
      bindings: {}, // the bindings will be created in the onShowMenu 
      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() === '') { 
        i = $.inArray(rowId, p.selarrrow); 
        if (p.selrow !== rowId && i < 0) { 
         // prevent the row from be unselected 
         // the implementation is for "multiselect:false" which we use, 
         // but one can easy modify the code for "multiselect:true" 
         grid.jqGrid('setSelection', rowId); 
        } else if (p.multiselect) { 
         // Edit will edit FIRST selected row. 
         // rowId is allready selected, but can be not the last selected. 
         // Se we swap rowId with the first element of the array p.selarrrow 
         lastSelId = p.selarrrow[p.selarrrow.length - 1]; 
         if (i !== p.selarrrow.length - 1) { 
          p.selarrrow[p.selarrrow.length - 1] = rowId; 
          p.selarrrow[i] = lastSelId; 
          p.selrow = rowId; 
         } 
        } 
        return true; 
       } else { 
        return false; // no contex menu 
       } 
      }, 
      onShowMenu: function (e, $menu) { 
       var options = this, $menuUl = $menu.find('ul:first').empty(); 

       $('table.navtable .ui-pg-button', pager).each(function() { 
        var $spanIcon, text, $td, id, $li, $a, button, 
         $div = $(this).children('div.ui-pg-div:first'), 
         gridId = grid[0].id; 

        if ($div.length === 1) { 
         text = $div.text(); 
         $td = $div.parent(); 
         if (text === '') { 
          text = $td.attr('title'); 
         } 
         if (this.id !== '' && text !== '') { 
          id = 'menuitem_' + this.id; 
          if (id.length > gridId.length + 2) { 
           id = id.substr(0, id.length - gridId.length - 1); 
          } 
         } else { 
          // for custom buttons 
          id = $.jgrid.randId(); 
         } 
         $li = $('<li>').attr('id', id); 
         $spanIcon = $div.children('span.ui-icon'); 
         if ($spanIcon.length > 0) { 
          // standard navGrid button or button added by navButtonAdd 
          $li.append($('<a>') 
           .text(text) 
           .prepend($spanIcon.clone().css({ 
            float: 'left', 
            marginRight: '0.5em' 
           }))); 
          $menuUl.append($li); 
          options.bindings[id] = (function ($button) { 
           return function() { $button.click(); }; 
          }($div)); 
         } else { 
          button = $div.children("input").data("button"); 
          if (button !== undefined) { 
           $a = $('<a>') 
            .text(button.options.label) 
            .prepend(
             $('<label>').addClass("ui-corner-all").css({ 
              float: 'left', 
              width: '16px', 
              borderWidth: '0px', 
              marginRight: '0.5em'//'4px' 
             }).append(
              $('<span>').addClass("ui-button-icon-primary ui-icon " + 
               button.options.icons.primary) 
               .css({ 
                float: 'left', 
                marginRight: '0.5em' 
               }) 
             ) 
            ); 
           $li.append($a); 
           if (button.type === "checkbox" && button.element.is(':checked')) { 
            $a.find('label:first').addClass("ui-state-active"); 
           } 
           $menuUl.append($li); 
           options.bindings[id] = (function ($button, isCheckbox) { 
            if (isCheckbox) { 
             return function() { 
              if ($button.is(':checked')) { 
               $button.siblings('label').removeClass("ui-state-active"); 
              } else { 
               $button.siblings('label').addClass("ui-state-active"); 
              } 
              $button.click(); 
              $button.button("refresh"); // needed for IE7-IE8 
            }; 
            } else { 
             return function() { $button.click(); }; 
            } 
           }(button.element, button.type === "checkbox")); 
          } 
         } 
        } 
       }); 
       return $menu; 
      } 
     }); 
    }, 
    autoedit = false; 

,并在填充所述检查按钮与仅改变一点的代码的导航栏:

$("#pager_left table.navtable tbody tr").append(
    '<td class="ui-pg-button ui-corner-all">' + 
     '<div class="ui-pg-div my-nav-checkbox">' + 
     '<input tabindex="-1" type="checkbox" id="AutoEdit" />' + 
     '<label title="Checkx caption which should appear as button tooltip"' + 
     ' for="AutoEdit">Autoedit</label></div></td>' 
); 
$("#AutoEdit").button({ 
    text: false, 
    icons: {primary: "ui-icon-mail-closed"} 
}).click(function() { 
    var iconClass, $this = $(this); 
    if (!autoedit) { // $this.is(':checked')) { 
     autoedit = true; 
     iconClass = "ui-icon-mail-open"; 
    } else { 
     autoedit = false; 
     iconClass = "ui-icon-mail-closed"; 
    } 
    $this.button("option", {icons: {primary: iconClass}}); 
}); 
createContexMenuFromNavigatorButtons($grid, '#pager'); 

已更新:另外一个支持按新增inlineNav方法添加按钮的演示,您可以找到here。此外,我包括在演示的功能normalizePagers,我用它来提高寻呼机的外观:

enter image description here

你怎么能看到的ContextMenu仅包括启用按钮从导航栏。

+0

谢谢。大。如果上下文菜单在浏览器窗口底部打开,则它将在浏览器之外呈现。它是不可见和可访问的。如何强制它在浏览器内呈现,因此它是可见的和可访问的。 jQuery的其他想法可以使用jQuery的小部件吗? – Andrus

+1

@Andrus:“contexmenu”的当前实现使用事件对象的“pageX”和“pageY”属性作为Web浏览器窗口中的绝对位置。作为第一个参数,可以在'onShowMenu'回调中访问该事件。例如,如果要将“e.pageX”和“e.pageY”属性更改为0,则contexmenu将显示在浏览器窗口的左上角。这种方式可以用来解决你所描述的问题。 – Oleg

+0

谢谢。如何强制菜单在点击位置附近打开,以便大部分菜单始终可见(如果有大量项目,则显示垂直滚动条)? – Andrus