Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Tree Grid Record Context Menu

    hi,
    I am trying to add a context menu to a tree grid using setContextMenu.
    the problem is that it is set on the whole grid and not only records.
    I want the menu to show only if i am right clicking on a record.
    and not if the listGrid is empty or if it contains records but someone clicks on the blank part of the grid and not on a record.

    I've seen some answers referencing to a nodeContextClick,
    but I didn't really understood what it meant.
    Can you help me?

    #2
    There are various context click events you can register for on TreeGrid. Usage is exactly like all other events.

    Comment


      #3
      We have no problem with registering for the events.
      The problem is that once we do, we couldn't find a way to show the context menu.
      we used setContextMenu(menu)
      we registered using addShowContextMenuHandler , and tried to cancel the event if our condition is not matched, but once we catch the event the menu is never shown.
      we couldn't find the method that is supposed to do that, showContextMenu() did not show it, show() on the menu did not work as well..

      what are we doing wrong?

      Comment


        #4
        Hi,

        I have a similar problem: I want to show different context menus depending on whether the user has right-clicked on a tree node or on the blank part of the grid. However, as far as I have figured out so far, there is no way to determine if the right-click took place on the blank part, but only if it took place on a node/folder/leaf etc. by using the different context click handlers.

        So, for example, if I do something like
        Code:
        tree.addFolderContextClickHandler(new FolderContextClickHandler() {
        	@Override
        	public void onFolderContextClick(FolderContextClickEvent event) {
        		
        		Menu myCustomMenu = new Menu();
        		myCustomMenu.setItems(...);
        	
        		tree.setContextMenu(myCustomMenu);
        	}
        });
        the custom context menu remains when the user clicks on a blank spot of the tree grid. Obviously, there is no handler you could add that would handle such a context click. Or am I missing something?

        I am using Smart GWT 2.5 power edition and would appreciate any help.
        Last edited by jafr; 6 Sep 2011, 03:37.

        Comment


          #5
          getEventRow() will tell you what row was clicked.

          Comment


            #6
            Having the same problems with ListGrids.

            getEventRow() only works on a selected row. if no row is selected getEventRow() will return null.

            And, by the way, if a row is selected and you right-click in the blank space of the grid, getEventRow() returns the last selected row num and no info about the blank portion of the grid.
            It would be nice to have a possibility to get the info about the blank space. so u can disable the context menu for the blank part e.g. .

            Comment


              #7
              getEventRow() returns the row the mouse is currently over. If it's not over a row, getEventRow() returns a negative number.

              Comment


                #8
                Thank you for the correction. Of course you are right on this one.

                One Problem remains:
                When i click in the blank portion of the Grid no Handler is fired.
                I tried with RightMouseDownHandler and getRowNum but the event was only fired when no record was selected. If the user selects a record and then RightClick in the blank space of the Grid this event won´t be fired.
                I also tried the ShowContextMenuHandler, which got fired, but the ContextMenu won´t show.
                I´m in need to disable the ContextMenuItems if the user right-clicks in the blank space of the Grid.

                Now i´m working with enableIfConditions, but these won´t work on the blank part either.

                Any idea?

                Comment


                  #9
                  ContextMenu events seem to be firing normally and there is no apparent issue with contextMenus being displayed. If you think there's a problem here, please create a minimal, standalone test case which demonstrates a framework issue.

                  Comment


                    #10
                    After much trial and error, I found a way to solve this issue.

                    Say you want the context menu to only display when right-clicking on a leaf. The code for this would be:

                    Code:
                    Menu contextMenu = new MyMenu();
                            
                    addLeafContextClickHandler(new LeafContextClickHandler()
                    {
                        public void onLeafContextClick(LeafContextClickEvent event) 
                        {
                            contextMenu.showContextMenu();
                        }
                    });
                    Be sure to NOT use setContextMenu, as the menu will pop up everywhere, e.g. even in the blank area.
                    Last edited by ahumphreys; 10 Feb 2012, 14:30.

                    Comment


                      #11
                      Unfortunately ahumphrey's naked contextMenu.showContextMenu() solution does not work. Well not with SmartClient Version: v12.1p_2020-07-31/PowerEdition Deployment (built 2020-07-31) and Chrome Version 87.0.4280.141 (Official Build) (64-bit) anyway. The issue is that with out calling setContextMenu(...), chrome draws it's own context menu on top of the desired MyMenu context menu.

                      I solved it in this manner, but I would be interested in a cleaner solution:


                      Code:
                                      // Remove the context menu so it will not pop up when you click on blank space in the grid.
                                      // If you don't call setContextMen(contexMenu), the Chrome's context menu draws it's self on top of this menu. 
                                      // Clearing it some time after it has been drawn seems to keep Chrome's context menu from appearing
                                      contextMenu.addDrawHandler(drawEvent -> {
                                          new Timer() {
                                              @Override
                                              public void run() {
                                                  contextMenu.addVisibilityChangedHandler(new VisibilityChangedHandler() {
                      
                                                      @Override
                                                      public void onVisibilityChanged(VisibilityChangedEvent event) {
                                                          if (!event.getIsVisible()) {
                                                              setContextMenu(null); 
                                                          }
                                                      }
                                                  });
                                              }
                                          }.schedule(350);
                                      });
                                      setContextMenu(menu); 
                                      menu.showContextMenu();

                      Comment


                        #12
                        You need to combine his approach with a ShowContextMenuHandler, to cancel the native context menu when you want to provide your own.

                        Comment

                        Working...
                        X