Search code examples
gwtcelltable

GWT get CellTable contents for printing or export


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.


Solution

  • 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());
    }