简体   繁体   English

钩GWT方法以导出GWT HighCharts中的按钮菜单项

[英]Hook GWT method to Export button menuitem in GWT HighCharts

I'm having trouble hooking a GWT method to the onclick method of a menuitem in the export button on a GWT HighCharts chart. 我在将GWT方法与GWT HighCharts图表上的导出按钮中的菜单项的onclick方法挂钩时遇到了麻烦。

I've successfully added a custom menu item to the export button, but dont know the syntax for setting the onclick method. 我已经成功将自定义菜单项添加到了导出按钮,但是不知道用于设置onclick方法的语法。 I've tried: 我试过了:

 setOption("onclick", "window.alert('Test')");

and

setOption("onclick", "function() { window.alert('Test') }");

but I get the following error: 但我收到以下错误:

Uncaught JavaScript exception [TypeError: a.onclick.apply is not a function]...

So, it is trying to invoke whatever Im putting in the onclick string, but apparently it is not the right syntax I'm using. 因此,它试图调用Im放在onclick字符串中的任何内容,但是显然这不是我使用的正确语法。 I have been thinking about JSNI, but I am not exactly sure how I would go about and do that. 我一直在考虑JSNI,但我不确定该如何去做。 Can anyone help? 有人可以帮忙吗?

EDIT: 编辑:

Obviously, in the above example, Im trying to show an javascript alert, but I want to call a GWT instance method if possible. 显然,在上面的示例中,我试图显示一个JavaScript警报,但我想尽可能调用GWT实例方法。 (Static methods would be ok, too). (静态方法也可以)。

EDIT 2: 编辑2:

This is the code for my menuitem in the Export button: 这是“导出”按钮中我的菜单项的代码:

private class ExportButtonMenuItem extends Configurable<ExportButtonMenuItem> {

    public ExportButtonMenuItem(String name, String type) {
        setOption("text", name);

        if (type.equalsIgnoreCase(EXPORT_EXCEL)) {
            // setOption("onclick", "window.alert('Test')");

            // does not work!
            setOption("onclick", new Command() {
                @Override
                public void execute() {
                    GWT.log("export!!!");
                }
            });
        }
    }
}

and this is how the menu item(s) are added to the chart: 这是将菜单项添加到图表的方式:

chart.setOption("/exporting/buttons/exportButton/menuItems", 
    new ExportButtonMenuItem[] { 
        new ExportButtonMenuItem("Download PNG", ExportButtonMenuItem.EXPORT_PNG), 
        new ExportButtonMenuItem("Download JPEG", ExportButtonMenuItem.EXPORT_JPEG), 
        new ExportButtonMenuItem("Download PDF", ExportButtonMenuItem.EXPORT_PDF),
        new ExportButtonMenuItem("Download SVG", ExportButtonMenuItem.EXPORT_SVG),
        new ExportButtonMenuItem("Download Excel", ExportButtonMenuItem.EXPORT_EXCEL)});

EDIT 3: 编辑3:

In the HighCharts JS source (exporting.js), the onclick method for the menuitem is invoked like this: 在HighCharts JS源代码(exporting.js)中,菜单项的onclick方法被调用,如下所示:

div[hasTouch ? 'ontouchstart' : 'onclick'] = function () {
    hide();
    item.onclick.apply(chart, arguments);
};

When apply is invoked, I get this error: 调用apply时,出现此错误:

     [java] Uncaught JavaScript exception [TypeError: item.onclick.apply is not a function] in http://127.0.0.1:8888/js/modules/exporting.src.js, line 467

I have been trying to override the onclick function, but still no success... 我一直在尝试重写onclick函数,但仍然没有成功...

Pass Command interface instead of String 通过命令接口而不是字符串

Command sayHello = new Command() {
   public void execute() {
     Window.alert("Hello");
   }
 };
 sayHello.execute();

Event handlers are not encompassed as options in the HighCharts API, but you can simply use plain GWT to attach such handler to the wrapping widget: 事件处理程序不包含在HighCharts API中,但您可以简单地使用普通的GWT将此类处理程序附加到包装小部件中:

widget.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        Window.alert("clicked!");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM