我有一个 GWTCellTable
使用某种复杂而乏味的过程来填充。我希望用户能够打印或导出该表中的数据。
我宁愿不重新渲染表内容以进行导出,因为这是一个繁琐的过程。
如何从我的所有页面中获取所有行的内容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());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)