Search code examples
jsfprimefacesprintingdatatablepaginator

p:dataTable with paginator: how to use p:printer to print all rows without paginator


In a <p:dataTable> with paginator, I need to print all the pages. When I use <p:printer> and give the datatable as target, it only prints first page with pagination controls. How can I print all the rows of the table with <p:printer>?


Solution

  • Ok, I was actually able to achieve the desired behavior but in a very unpleasant way.

    First of all, it seems that p:printer just takes a DOM element by an id from a xhtml page and passes it to a printer. So, to be able to print a full table it first should be re-rendered.

    My idea was to re-render table without paginator, refresh it and pass to the printer and after that to restore table's state. To be able to do so I created two buttons: one that triggers the table re-rendering, and other that passes this table to a printer and restores it's state.

    First button is visible and is the button a user presses to print. It calls the deactivatePaginator() method to disable pagination and prepare table view for printing. Second button is hidden and is the actual button to contain p:printer component. It calls the activatePaginator() method to restore table view.

    Here is the resulting xhtml page:

    <p:dataTable id="table" value="#{someBean.list}" var="str" paginator="#{someBean.paginatorActive}" rows="#{someBean.paginatorActive ? 10 : 0}">
        <p:column headerText="Header">
            <h:outputLabel value="#{str}"/>
        </p:column>
    </p:dataTable>
    <h:form id='frm'>
        <p:commandButton value="Print" actionListener="#{someBean.deactivatePaginator()}" 
                                 oncomplete="$('#frm\\:print').click()"
                                 update=":table"/>
         <p:commandButton id="print" value="Actual print" style="display: none">
             <p:ajax event="click" listener="#{someBean.activatePaginator()}" update=":table"/>
             <p:printer target=":table" />
         </p:commandButton>
    </h:form>
    

    Methods' realisation is pretty simple. All is needed to do is to set paginatorActive flag accordingly.

    @ViewScoped
    @ManagedBean
    public class SomeBean {
    
        private boolean paginatorActive = true;
    
        public void activatePaginator() {
            paginatorActive = true;
        }
    
        public void deactivatePaginator() {
            paginatorActive = false;
        }
    
        public boolean isPaginatorActive() {
            return paginatorActive;
        }
    }
    

    All this madness could be covered by p:blockUI as multiple table update causes a quick glitch.

    This solution is proposed as workaround and I don't think is a proper way to do things. But I also don't think there is another way to do what is required using p:printer.