qxl*_*lab 2 datatable commandlink primefaces jsf-2
以下代码是关于一个表,我可以在其中添加(commandButton)或删除(commandLink)行.这两个按钮都在工作并调用相应的bean方法.但是,对于每次单击添加按钮将更新表立即添加一行,对于删除按钮,我必须单击它两次以删除行.即使第一次没有删除该行,也会调用bean方法.我该怎么办?谢谢!
<h:form id="form">
<table>
<tr>
<td>
<h:panelGrid columns="2" width="100%">
<p:dataTable id="univertitiesTable" value="#{universityBean.universityList}" var="university"
editable="true" editMode="cell" style="align:center;" >
<p:column headerText="Name" style="width:80px" >
<p:inputText value="#{university.name}" style="width:25px;" id="nameField" label="name" />
</p:column>
<p:column headerText="" style="width:20px; ">
<p:commandLink actionListener="#{universityBean.deleteUniversity}" update="univertitiesTable" id="removeButton" ajax="true">
<h:graphicImage value="/resources/icones/delete.gif" />
<f:setPropertyActionListener value="#{university}"
target="#{universityBean.university}" />
</p:commandLink>
</p:column>
</p:dataTable>
<p:commandButton value="+" update="univertitiesTable" id="addButton" ajax="true"
actionListener="#{universityBean.addUniversity}"
styleClass="ui-priority-primary" />
</h:panelGrid>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<h:commandButton id="save" value="Save"
action="#{universityBean.save}" binding="#{save}" />
</td>
</tr>
</table>
</h:form>
Run Code Online (Sandbox Code Playgroud)
你必须使用action而不是actionListener.更新在操作完成后完成,因此当您未指定任何操作时,将立即执行更新.因此,视图将识别出在执行另一次更新时删除该行; 这是在您再次单击链接时完成的.
顺便说一句ajax="true",无论如何是ajax-attribute 的默认值.
即:
<p:commandLink action="#{universityBean.deleteUniversity}" update="univertitiesTable" id="removeButton">
<h:graphicImage value="/resources/icones/delete.gif" />
<f:setPropertyActionListener value="#{university}"
target="#{universityBean.university}" />
</p:commandLink>
Run Code Online (Sandbox Code Playgroud)