How to print some elements in gwt

主宰稳场 提交于 2020-01-25 10:49:07

问题


I want to print one panel of page in GWT.

However someone said that you must use iframe because it has print method.

I tried this code but it does not work:

     HTML html=new HTML("<iframe id="myframe"></iframe>") ;....
    searchButton.setWidth("80px");
            searchButton.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    MyNative.printIframeContent("myframe", "onlineMap");

                }
            });
    map = new SimplePanel();
    map.getElement().setId("onlineMap");
    map.add(mapView.getMapWidget());

mapView .is an instance of a class that returns a GWT MapWidget and in this manner I want to add GWT map to iframe then use print capability of iframe to print google map

MyNative is a class that use GWT JSNI to call the printPage javascript function

function printPage(idMap,idFrame) {
    var myElement = document.getElementById(idMap);
    var iframe = document.getElementById(idFrame);
    var body = iframe.getElementsByTagName('body');
    body.innerHTML = myElement;
    iframe.contentWindow.print();
}

but browser can not load body of iframe.


回答1:


You can try this:

String html = myPanel.getElement().getInnerHTML();
print(html);


public static final native void print(String html) /*-{

    top.consoleRef=$wnd.open('','_blank', "");
    top.consoleRef.document.write(html);
    top.consoleRef.print();
    top.consoleRef.document.close()

}-*/;


来源:https://stackoverflow.com/questions/25485735/how-to-print-some-elements-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!