在GWT中单击时在单元格表中获取标题列名称

时间:2012-04-16 13:50:06

标签: gwt

我正在使用GWT 2.2版的Cell Table。我想获取我点击的标题列的名称。我没有得到任何点击事件。

我有什么工作可以完成我的任务。

1 个答案:

答案 0 :(得分:0)

这样的东西? ;)

public class CellTableExample implements EntryPoint, ClickHandler {

    private static class SomeEntity {
        /* ... */
    }


    private static class ClickableTextHeader extends TextHeader {

        private ClickHandler handler;

        public ClickableTextHeader(String text, ClickHandler handler) {
            super(text);
            this.handler = handler;
        }

        @Override
        public void onBrowserEvent(Context context, final Element elem,
                final NativeEvent event) {

            //maybe hijack click event
            if(handler != null) {

                if(Event.ONCLICK == Event.getTypeInt(event.getType())) {

                    handler.onClick(new ClickEvent() {
                        {
                            setNativeEvent(event);
                            setRelativeElement(elem);
                            setSource(ClickableTextHeader.this);
                        }
                    });
                }
            }

            //default dom event handler 
            super.onBrowserEvent(context, elem, event);
        }
    }

    CellTable<SomeEntity> cellTable;
    TextColumn<SomeEntity> firstColumn;
    TextColumn<SomeEntity> secondColumn;
    TextColumn<SomeEntity> thirdColumn;

    @Override
    public void onModuleLoad() {

        /* somehow init columns - it's not the point for this example */

        cellTable.addColumn(firstColumn, new ClickableTextHeader("First column header", this));
        cellTable.addColumn(secondColumn, new ClickableTextHeader("Second column header", this));
        cellTable.addColumn(thirdColumn, new ClickableTextHeader("Third column header", this));
    }

    @Override
    public void onClick(ClickEvent event) {
        ClickableTextHeader source = (ClickableTextHeader) event.getSource();
        Window.alert(source.getValue());
    }
}

如果我们使用“简单的侦听器接口”,劫持事件可能看起来更简单 - 我只是想“与开箱即用的处理程序兼容”:)