2017-06-07 47 views
0

我需要处理事件,当用户点击“打印”按钮或发出Ctrl + P。原因是我们在我们的GWT应用程序中添加了一个打印方法,可以打印出不错的打印结果。这不能用CSS来完成。因此我们需要捕捉事件。我已经在这里发现了这个https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/,但我不确定如何在GWT中实现它?任何帮助赞赏。GWT处理浏览器窗口的打印事件

我使用GWT 2.8。

问候 汉纳斯

+1

可以使用JSNI http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html – WLGfx

+0

或JsInterop。那将是做这种事情的“新”方式 –

回答

0

毕竟我解决了它这样的,我在这条路上学到了很多关于JSNI :-)

public native void handlePrintEvent() /*-{ 

    var theInstance = this; 
    (function() { 
     var beforePrint = function() { 
      console.log('Functionality to run before printing.'); 
      [email protected]::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()来执行一些特定的打印逻辑(这实际上是我需要的)。

希望别人发现这个有用