我有一个 GWT CellTable
,它使用某种复杂而乏味的过程进行填充。我希望用户能够打印或导出该表中的数据。
我宁愿不重新呈现表格内容以进行导出,因为这是一个乏味的过程。
如何从我的 CellTable
的所有页面中获取所有行的内容,以便将文档放在一起进行打印或导出?
我会接受一种获取表格的实际 HTML 的方法,或者一种用于迭代并从单元格中获取呈现内容的算法。如果有人有更好的建议,我们也将不胜感激。
最佳答案
似乎没有可行的方法让 CellTable
在不重新呈现内容的情况下为我提供导出数据。由于那样会花费与自己完成相同的执行时间,因此我求助于自己进行渲染。我使用以下代码呈现 HTML 并将其显示在新的弹出窗口中以进行打印。 print()
方法从我的“打印”按钮调用。
/**
* 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 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712559/