我在 p:dataTable 中有一个带有 p:commandLink 的列,它有一个分页器。当用户单击命令链接时,将打开一个对话框,显示该行的数据。
数据表html代码:
<p:dataTable id="dtSample" value="#{sessionBean.sampleList}"
binding="#{requestBean.dtSampleList}"
paginator="true" paginatorPosition="bottom"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="#{10,25,50}"
currentPageReportTemplate="({startRecord} of {totalRecords})"
var="item" emptyMessage="No entries." rows="10" rowKey="#{item.id}">
<p:column headerText="Id">
<p:commandLink id="lnkSample"
action="#{requestBean.onLinkClick(item)}"
oncomplete="PF('dlgSample').show();"
update="@(.dialogClass)">
<h:outputText value="#{item.id}" />
</p:commandLink>
</p:column>
<p:column headerText="Code">
<h:outputText value="#{item.code}" />
</p:column>
<p:column headerText="Description">
<h:outputText value="#{item.descr}" />
</p:column>
</p:dataTable>
请求bean:
public class RequestBean {
private SessionBean sessionBean;
private DataTable dtSampleList;
public void init() {
// load samle list
loadSampleList();
}
public String onLinkClick(Sample sample) {
getSessionBean().setSelectedSample(sample);
return "success";
}
private void loadSampleList() {
List<Sample> list = new ArrayList<Sample>();
for (int i = 0; i < 100; i++) {
Sample tmp = new Sample();
tmp.setId(new BigDecimal(i + 1));
tmp.setCode("code" + (i + 1));
tmp.setDescr("desc" + (i + 1));
list.add(tmp);
}
getSessionBean().setSampleList(list);
}
// getters and setters
}
session bean:
public class SessionBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<Sample> sampleList;
private Sample selectedSample;
// getters and setters
}
对话框html代码:
<p:dialog id="dlgSample" closeOnEscape="true" widgetVar="dlgSample"
styleClass="dialogClass" modal="true">
<p:panelGrid columns="1">
<h:outputText value="Id: #{sessionBean.selectedSample.id}" />
<h:outputText value="Code: #{sessionBean.selectedSample.code}" />
<h:outputText value="Description: #{sessionBean.selectedSample.descr}" />
</p:panelGrid>
</p:dialog>
当我单击数据表第一页上的链接时,将执行链接操作,并正确刷新显示行数据的对话框。但是,当我移动到数据表的以下任何页面时,单击链接不会刷新对话框中的数据(未调用链接操作,因此对话框中的数据是错误的 - selectedSample
变量具有旧值)。当然,当我返回到数据表的第一页时,命令链接再次起作用(调用操作方法并刷新数据)。
我做错了什么?为什么在任何数据表页面上都没有调用操作方法?
我使用的是 Primefaces 5.2。
最佳答案
看起来问题出在 PF dataTable 组件中。缺少 first
和 rows
属性,添加它们后,所有页面上的 commandLink 都按预期工作。
关于jsf - Primefaces 的 commandLink 仅适用于数据表的第一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31607225/