【问题标题】:GWT handle print event of the browser windowGWT 处理浏览器窗口的打印事件
【发布时间】:2017-06-07 04:15:53
【问题描述】:

当用户单击“打印”按钮或发出 Ctrl+P 时,我需要处理该事件。原因是我们在 GWT 应用程序中添加了一个打印方法,它准备了一个漂亮的打印输出。这不能用 CSS 完成。因此,我们需要捕获事件。我已经在这里找到了https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/,但我不确定如何在 GWT 中实现它?任何帮助表示赞赏。

我使用 GWT 2.8。

最好的问候 汉内斯

【问题讨论】:

标签: javascript gwt


【解决方案1】:

毕竟我是这样解决的,并且通过这种方式学到了很多关于 JSNI 的知识 :-)

public native void handlePrintEvent() /*-{

    var theInstance = this;
    (function() {
        var beforePrint = function() {
            console.log('Functionality to run before printing.');
            theInstance.@com.myproject.xyz::preparePrint()();
        };
        var afterPrint = function() {
            console.log('Functionality to run after printing');

        };

        if (window.matchMedia) {
            var mediaQueryList = window.matchMedia('print');
            mediaQueryList.addListener(function(mql) {
                if (mql.matches) {
                    beforePrint();
                } else {
                    afterPrint();
                }
            });
        }

        window.onbeforeprint = beforePrint;
        window.onafterprint = afterPrint;
    }());
}-*/;

变量 theInstance 的东西很重要,否则不起作用!

应该在您的 Widget (com.myproject.xyz) 类的 C'tor 中调用此函数来安装打印处理程序

然后你需要实现函数 preparePrint() 来执行一些打印特定的逻辑(这实际上是我需要的)。

希望其他人觉得这很有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    相关资源
    最近更新 更多