2017-06-07 52 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()來執行一些特定的打印邏輯(這實際上是我需要的)。

希望別人發現這個有用