Announcement

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

    getHeaderContextMenuItems and event.getColNum()

    SmartClient Version: v9.1p_2014-06-04/Pro Deployment (built 2014-06-04)

    I have an example below where I am overriding ListGrid.getHeaderContextMenuItems(Integer) to add a custom header context-menu item and I am not getting the expected column number from the event when I click on my custom context-menu item.

    No matter what column header I click on to open the header context-menu, when I click on the "Custom Menu Item" menu item, event.getColNum() is always 1.

    Code:
    @Override
    protected MenuItem[] getHeaderContextMenuItems(Integer fieldNum) {
        final List<MenuItem> menuItems = new ArrayList<MenuItem>();
        final MenuItem[] currentMenuItems = super.getHeaderContextMenuItems(fieldNum);
    
        // Add the current menu items.
        for (MenuItem currentMenuItem : currentMenuItems) {
            menuItems.add(currentMenuItem);
        }
    
        // Add custom menu item.
        MenuItem customMenuItem = new MenuItem("Custom Menu Item");
        customMenuItem.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(MenuItemClickEvent event) {
                int colNum = event.getColNum();
                String fieldName = getFieldName(colNum);
                SC.say("columnNum: " + colNum + ", columnName: " + fieldName);
            }
        });
        menuItems.add(new MenuItemSeparator());
        menuItems.add(customMenuItem);
    
        return menuItems.toArray(new MenuItem[menuItems.size()]);
    }
    Thanks

    #2
    This is actually behaving correctly- it's just a little confusing.

    What's happening is that the menu itself is a subclass of ListGrid which shows several columns. By default you see a thin column on the left containing just icons and a thicker column containing the titles of each menu item.

    The itemClick handler is passed the index of the column *within the menu* that the user clicked - so if you click over the item title that'll be 1 - if you click over the icon-column on the left it'll be zero. It does not reflect the index of the column for which the menu is being shown.

    Instead you need to use the fieldNum parameter passed into the 'getHeaderContextMenuItems' method to determine what field within the list grid the menu is showing for.
    Here's an example of doing this by storing the fieldNum directly on the item for convenience (using 'setAttribute').

    Let us know if you have further questions on this

    Thanks
    Isomorphic Software



    Code:
    			@Override
    			protected MenuItem[] getHeaderContextMenuItems(Integer fieldNum) {
    			    final List<MenuItem> menuItems = new ArrayList<MenuItem>();
    			    final MenuItem[] currentMenuItems = super.getHeaderContextMenuItems(fieldNum);
    	
    			    // Add the current menu items.
    			    for (MenuItem currentMenuItem : currentMenuItems) {
    			        menuItems.add(currentMenuItem);
    			    }
    	
    			    // Add custom menu item.
    			    MenuItem customMenuItem = new MenuItem("Custom Menu Item");
    			    customMenuItem.setAttribute("fieldNum",  fieldNum);
    			    customMenuItem.addClickHandler(new ClickHandler() {
    			        @Override
    			        public void onClick(MenuItemClickEvent event) {
    			        	int fieldNum = event.getItem().getAttributeAsInt("fieldNum");
    			        	String fieldName = getFieldName(fieldNum);
    			        	SC.say("Showing menu for " + fieldName);
    			        }
    			    });
    			    menuItems.add(new MenuItemSeparator());
    			    menuItems.add(customMenuItem);
    	
    			    return menuItems.toArray(new MenuItem[menuItems.size()]);
    			}

    Comment


      #3
      Thank you for the clarification. I have made the code adjustment and all is working fine.
      Regards

      Comment

      Working...
      X