p:dataTable with paginator:如何使用p:printer打印所有没有分页符的行
在paginator的< p:dataTable>
中,我需要打印所有页面。当我使用< p:printer>
并将datatable作为目标时,它只打印具有分页控件的第一页。如何使用< p:printer>
打印表格的所有行?
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>
?
好的,我实际上能够实现所需的行为,但是以非常不愉快的方式。
Ok, I was actually able to achieve the desired behavior but in a very unpleasant way.
首先,似乎 p:printer
只需要一个DOM元素,并将其传递给一个打印机。所以,为了能够打印一个完整的表,它首先应该重新渲染。
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.
按钮是可见的,并且是用户按打印的按钮。它调用 deactivatePaginator()
方法来禁用分页并准备表视图进行打印。
第二个按钮被隐藏,是包含 p:printer
组件的实际按钮。它调用 activatePaginator()
方法来恢复表视图。
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.
以下是生成的xhtml页面:
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>
方法的实现非常简单。所有需要做的是相应地设置 paginatorActive
标志。
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;
}
}
所有这些疯狂都可以被 p:blockUI
作为多表更新会导致快速故障。
All this madness could be covered by p:blockUI
as multiple table update causes a quick glitch.
此解决方案被提议为解决方法,我不认为是正确的做事方式但是我也不认为有另外一种方式可以使用 p:printer
来执行所需的操作。
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
.