GXT : Cancel click event on grid header

1.3k views Asked by At

I have a grid like this one . The default behavior of this widget is to sort the column when clicking on the header. I want to disable that, and leave only the options in the context menu.

grid.addHeaderClickHandler(new HeaderClickHandler() {

    @Override
    public void onHeaderClick(HeaderClickEvent event) {
        event.getEvent().cancelBubble(true);
        event.getEvent().preventDefault();
        event.getEvent().stopPropagation();
        Window.alert("Event caught");
    }
});

The alert gets displayed before the sorting. But when clicking Ok the default behavior kicks in.

Do you have any suggestions on how to accomplish this?

3

There are 3 answers

0
Alkis Kalogeris On BEST ANSWER

Ok I found it grid.getView().setSortingEnabled(false);. This will disable the sorting when clicking on the header, but keep the menu items in place.

3
MrTux On

In order to disable sorting you have to configure the column(s) to forbid sorting.

E.g. if you create a column like this (based on the source code from http://www.sencha.com/examples/#ExamplePlace:basicgrid)

ColumnConfig<Stock, String> symbolCol = new ColumnConfig<Stock, String>(props.symbol(), 100, "Symbol");

disable sorting by adding

symbolCol.setSortable(false);

See http://dev.sencha.com/deploy/gxt-3.0.0/javadoc/gxt/com/sencha/gxt/widget/core/client/grid/ColumnConfig.html#setSortable%28boolean%29 for documentation (and other options).

1
JPelletier On

A solution that might work is to use the NativePreviewHandler, something similar to

Event.addNativePreviewHandler(new NativePreviewHandler()
    {
        @Override
        public void onPreviewNativeEvent(final NativePreviewEvent event)
        {
            if (event.getTypeInt() == Event.ONCLICK)
            {
                Element targetElement = Element.as(event.getNativeEvent().getEventTarget());
                if (!tableHeader.asWidget().getElement().isOrHasChild(targetElement))
                {
                    event.getNativeEvent().stopPropagation();
                }
            }
        }
    });