使用ruby中的上下文菜单获取右键单击的元素

时间:2011-07-26 12:53:59

标签: jquery ruby-on-rails ruby ruby-on-rails-3 contextmenu

见附图

enter image description here

以上树视图菜单代码:

      ret = "<ul id='navigation1'>"
      tree.each do |node|
#       if node.InstallationName == parent_id
          ret += "<li>"
          ret += link_to_remote(node.InstallationName, :url => {:controller => "ptcgeodatabase", :action => "page_node"},
                  :with => "'installation_name_delete='+'#{node.InstallationName}'",:update => "mcfcontent1")
          Geoptcmenu.connectdb($mantmasterdblocation)
          sub = Geoptcmenu.find(:all,:select=>'Distinct MCFName',:conditions=>['InstallationName=?', node.InstallationName])
          ret += display_mcftree_child(sub, node.InstallationName)
          ret += "</li>"
#        end
      end
       ret += "</ul>"

并显示上下文菜单,同时右键单击我已编写代码:

$('#navigation1').contextMenu('context-menu-1', {
        'Delete Installation': {
         click: function(element) {  // element is the jquery obj clicked on when context menu launched

            $.post("/ptcgeodatabase/deletemcf", {

            }, function(data){
               window.location.href = "/ptcgeodatabase/ptcgeo_mcfextractor"
            });
         },
         klass: "ocemenu-item-1" // a custom css class for this menu item (usable for styling)
       }});

我无法获得我右键单击的元素。是获取右键单击元素名称的任何方法吗?

1 个答案:

答案 0 :(得分:0)

    ret = "<ul id='navigation1'>"
          tree.each do |node|
    #       if node.InstallationName == parent_id
              ret += "<li>"
              ret += link_to(node.InstallationName, "#")
              Geoptcmenu.connectdb($mantmasterdblocation)
              sub = Geoptcmenu.find(:all,:select=>'Distinct MCFName',:conditions=>['InstallationName=?', node.InstallationName])
              ret += display_mcftree_child(sub, node.InstallationName)
              ret += "</li>"
    #        end
          end
           ret += "</ul>"


 $(function() {
                $('#navigation1 li').mousedown(function(event) {
                    var selectedelementid = this.id;
                    if (selectedelementid)  {               
                    switch (event.which) {
                        case 1:
                            //alert('Left mouse button pressed');
                            $('#navigation1 li').removeClass('selected');
                            $(this).addClass('selected');
                            $.post("/ptcgeodatabase/page_node", {
                                installation_name_delete: selectedelementid
                            });
                            break;

                        case 2:
                            var message ="<div style="+"padding-top:30px;padding-left:5px;color:red;padding-right:5px;padding-bottom:10px;font-size:small;"+">You have clicked middle mouse button</div>";
                            $.colorbox({html:message ,transition:"none" ,width:"300px" ,height:"150px"});
                            //alert('You have clicked middle mouse button.');
                            break;
                        case 3:
                            //alert('Right mouse button pressed');
                            $('#navigation1 li').removeClass('selected');
                            $(this).addClass('selected');
                            $.post("/ptcgeodatabase/page_node", {
                                installation_name_delete: selectedelementid
                            });
                            break;
                        default: break;
                    }
                    }
                });         
            });