[英]p:commandButton not working within a p:dataTable while using filter
我使用的是 JSF、Primefaces 5.2 和 GlassFish 4.1。
我已经建立了一个简单的项目来测试和复制我更大的问题,并发现它在测试项目中是可重复的。
我有一个简单的页面,其中包含一个简单的对象列表。 我正在使用<p:dataTable>
并使用全局搜索功能和分页。
表代码
<ui:define name="content">
<h:form id="carsForm" >
<p:dataTable id="singleDT" var="c" value="#{testFlow.cars}" widgetVar="carsTable" rows="10" paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15" emptyMessage="No cars found with the given criteria" reflow="true"
filteredValue="#{testFlow.filteredCars}">
<f:facet name="header">
<p:outputPanel class="fright">
<h:outputText value="Search all fields: " />
<p:inputText id="globalFilter" onkeyup="PF('carsTable').filter()" style="width:200px" placeholder="Enter keyword" />
</p:outputPanel>
<p:outputPanel class="Fleft">
<p:commandButton value="New Car" action="addCar" />
</p:outputPanel>
<div class="EmptyBox5"></div>
</f:facet>
<p:column filterBy="#{c.id}" headerText="ID" sortBy="#{c.id}" >
<h:outputText value="#{c.id}"/>
</p:column>
<p:column filterBy="#{c.m}" headerText="Model Year" sortBy="#{c.modelYear}" >
<h:outputText value="#{c.modelYear}"/>
</p:column>
<p:column filterBy="#{c.make}" headerText="Make" sortBy="#{c.make}" >
<h:outputText value="#{c.make}"/>
</p:column>
<p:column filterBy="#{c.model}" headerText="Model" sortBy="#{c.model}" >
<h:outputText value="#{c.model}"/>
</p:column>
<p:column filterBy="#{c.color}" headerText="Color" sortBy="#{c.color}" >
<h:outputText value="#{c.color}"/>
</p:column>
<p:column style="width: 200px; text-align: center">
<p:commandButton actionListener="#{testFlow.removeCar(c)}" value="Delete" update="singleDT messages" ajax="true" >
<p:confirm header="Confirmation" message="Are you sure?" icon="ui-icon-alert" />
</p:commandButton>
</p:column>
</p:dataTable>
<div class="EmptyBox5"></div>
<p:panel>
<p:commandButton action="returnFromTestFlow" value="Exit the Flow" />
</p:panel>
</h:form>
<p:messages id="messages" autoUpdate="true" closable="true" />
<p:confirmDialog global="true" showEffect="fade" hideEffect="explode">
<p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</ui:define>
我的控制器有一个简单的方法来删除对象。
方法
public String removeCar(Inventory c) {
String redirectTo = "/testFlow/testFlow";
try {
this.cars.remove(c);
iFacade.remove(c);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return redirectTo;
}
当我删除所有过滤和排序时,单击是并删除该行后调用该方法。 然而,当过滤和排序到位时,整个表只是重新显示为空,但没有调用任何方法。 不会出现错误,并且在使用调试器时,它永远不会在相关方法中遇到断点。
任何帮助或指导将不胜感激。
我认为问题在于数据表首先使用您提供的列表呈现,但是当您按下按钮时,它会搜索过滤后的列表,并且由于最初是空的,因此无法找到该按钮。 另一方面,请注意,如果您输入搜索条件并再次将其删除,则按钮会起作用。 这是因为过滤列表已填充,现在可以找到按钮。 要解决此问题,您需要使用预渲染的所有值填充过滤列表。 在你的情况下:
在你的支持 bean 中:
public void init()
{
if(!FacesContext.getCurrentInstance().isPostback())
{
filteredCars = cars;
}
}
在您的 xhtml 文件中:
<f:event type="preRenderView" listener="#{testFlow.init}"/>;
另一种方法是在 init() 函数上使用@PostConstruct
注释,而不是<f:event type="preRenderView />
,这是更清晰的代码,但在我看来,在调用函数时有点模糊。
有同样的问题,@djst 解决方案对我不起作用,但我最终通过使用 Omnifaces 表单( o:form
)而不是标准h:form
解决了这个问题。 虽然不知道为什么,但可能值得一试。
当您使用过滤时,primefaces 创建一个列表并显示在结果中,当您从主列表中删除一个项目时,它与显示列表无关,这可能会导致您遇到的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.