0

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

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

4

1 回答 1

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());
    }
}

如果我们使用“简单的侦听器接口”,劫持事件可能看起来更简单——我只是想“在语义上符合开箱即用的处理程序”:)

于 2012-04-17T00:41:29.737 回答