本文介绍了GWT获取用于打印或导出的CellTable内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GWT CellTable ,它使用一些复杂而繁琐的过程来填充。我希望用户能够打印或导出该表中的数据。



我宁愿不重新渲染表格内容以便导出,因为这是一个单调乏味的过程。



如何从我的 CellTable 的所有页面中获取所有行的内容,一起打印或导出一个文件?

我会很好的抓取表格的实际HTML,或迭代和抓取呈现的算法来自细胞的内容。如果有人有更好的建议,那也是值得赞赏的。

解决方案

似乎没有可行的方法来获得 CellTable 给我导出的数据而不重新呈现内容。既然这样做会花费与我自己一样的执行时间,我自己渲染它。我使用下面的代码来呈现HTML并将其显示在新的弹出窗口中进行打印。从我的打印按钮调用 print()方法。


$ b

  / ** 
*在新的弹出窗口中打印。
* http://www.coderanch.com/t/564198/GWT/GWT-injecting-HTML-text-browser
* /
public static native void printHTMLString(String htmlString)/ * - {
var win = $ wnd.open(_ blank,Print,);
win.document.open(text / html,replace);
win.document.write(< html>< head>< / head>< body>+ htmlString +< / body>< / html>);
win.document.close();
win.focus();
var headID = win.document.getElementsByTagName(head)[0];
var fileref = win.document.createElement(link);
fileref.setAttribute(rel,stylesheet);
fileref.setAttribute(type,text / css);
fileref.setAttribute(href,tables-min.css);
headID.appendChild(fileref);
win.print();
} - * /;
$ b private void print(){
//从ColumnSortHandler获取列表,所以它保持屏幕上的排序
if(columnSortHandler.getList()== null | | columnSortHandler.getList()。isEmpty()){
Window.alert(Nothing to print);
return;
}

SafeHtmlBuilder b = new SafeHtmlBuilder();
b.appendHtmlConstant(< table class = \pure-table \>
+< thead>< tr>< th>时间戳< / th>< th< th> < th>< th>< th>< th>< th>< th>>< / tr>< THEAD>中);
int count = 1;
for(Record r:columnSortHandler.getList()){
b.appendHtmlConstant(< tr+(count%2 == 0?>:class ='pure-table余'>中));

b.appendHtmlConstant(< td>);
b.appendEscaped(timestampFormat.format(timeStampColumn.getValue(r)));
b.appendHtmlConstant(< / td>);

b.appendHtmlConstant(< td>);
b.appendEscaped(typeColumn.getValue(r));
b.appendHtmlConstant(< / td>);

b.appendHtmlConstant(< td>);
b.appendEscaped(userColumn.getValue(r));
b.appendHtmlConstant(< / td>);

b.appendHtmlConstant(< td>);
b.appendEscaped(screenColumn.getValue(r));
b.appendHtmlConstant(< / td>);

b.appendHtmlConstant(< td>);
b.appendEscaped(clientColumn.getValue(r));
b.appendHtmlConstant(< / td>);


b.appendHtmlConstant(< / tr>);

count ++;
}
b.appendHtmlConstant(< / table>);
printHTMLString(b.toSafeHtml()。asString());
}


I have a GWT CellTable that gets populated using somewhat of a complicated and tedious process. I want the user to be able to print or export the data from that table.

I would rather not re-render the table contents for export since it is a tedious process.

How can I get the contents of all the rows from all the pages of my CellTable so I can put together a document for printing or export?

I'd be fine with a method of grabbing the actual HTML of the table, or an algorithm for iterating through and grabbing the rendered contents from cells. If someone has a better suggestion, that'd be appreciated as well.

解决方案

It seems there is no viable way to get the CellTable to give me the data for export without re-rendering contents. Since that would cost the same execution time as doing it myself, I resorted to rendering it myself. I used the following code to render HTML and display it in a new popup for printing. The print() method gets called from my Print button.

/**
 * Print in a new popup.
 * http://www.coderanch.com/t/564198/GWT/GWT-injecting-HTML-text-browser
 */
public static native void printHTMLString(String htmlString)/*-{
    var win = $wnd.open("_blank", "Print", "");
    win.document.open("text/html", "replace");
    win.document.write("<html><head></head><body>" + htmlString + "</body></html>");
    win.document.close();
    win.focus();
    var headID = win.document.getElementsByTagName("head")[0];
    var fileref = win.document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", "tables-min.css");
    headID.appendChild(fileref);
    win.print();
}-*/;

private void print() {
    //get the list from the ColumnSortHandler, so it keeps the sorting on the screen
    if (columnSortHandler.getList() == null || columnSortHandler.getList().isEmpty()) {
        Window.alert("Nothing to print");
        return;
    }

    SafeHtmlBuilder b = new SafeHtmlBuilder();
    b.appendHtmlConstant("<table class=\"pure-table\">"
            + "<thead><tr><th>Timestamp</th><th>Type</th><th>User</th>"
            + "<th>Screen</th><th>Client</th></tr></thead>");
    int count = 1;
    for (Record r : columnSortHandler.getList()) {
        b.appendHtmlConstant("<tr" + (count%2==0 ? ">" : " class='pure-table-odd'>"));

        b.appendHtmlConstant("<td>");
        b.appendEscaped(timestampFormat.format(timeStampColumn.getValue(r)));
        b.appendHtmlConstant("</td>");

        b.appendHtmlConstant("<td>");
        b.appendEscaped(typeColumn.getValue(r));
        b.appendHtmlConstant("</td>");

        b.appendHtmlConstant("<td>");
        b.appendEscaped(userColumn.getValue(r));
        b.appendHtmlConstant("</td>");

        b.appendHtmlConstant("<td>");
        b.appendEscaped(screenColumn.getValue(r));
        b.appendHtmlConstant("</td>");

        b.appendHtmlConstant("<td>");
        b.appendEscaped(clientColumn.getValue(r));
        b.appendHtmlConstant("</td>");


        b.appendHtmlConstant("</tr>");

        count++;
    }
    b.appendHtmlConstant("</table>");
    printHTMLString(b.toSafeHtml().asString());
}

这篇关于GWT获取用于打印或导出的CellTable内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:14
查看更多