如何更新PrimeFaces dataTable上的特定单元格 [英] How to update a specific cell on a PrimeFaces dataTable
问题描述
在我的 dataTable
中,我将每篇文章与特定任务进行链接。
In my dataTable
I am linking each article with a specific task.
点击 commandButton
一个任务列表显示,所以我想要选择一个任务,更新 dataTable
中的特定单元格( outputText
与 id =columnTache
),而不更新我的其他 dataTable
。
On the click of a commandButton
a list of tasks shows up, so I want on the select of a task, update a specific cell in the dataTable
(outputText
with id="columnTache"
) without updating the rest of my dataTable
.
<p:dataTable value="#{myController.articleList}"
id="tabArticle"
var="article"
rowKey="#{article.id}" >
<p:column headerText="quantite" >
<pe:inputNumber value="#{article.quantite}" />
</p:column>
<p:column headerText="taches" >
<h:outputText value="#{article.tache.libelleTache}" id="columnTache" />
</p:column>
<p:column headerText="taches" >
<p:commandButton oncomplete="PF('dialogTasks').show();" update=":formSelectAffecterTache">
<p:ajax event="click" listener="#{myController.setArticle(article)}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:dialog header="#{bundleTech.lbl_taches}" widgetVar="dialogTasks" >
<h:panelGrid columns="1" >
<h:form id="formSelectAffecterTache">
<ui:include src="/pages/listTacheSelect.xhtml">
<ui:param name="bean" value="#{myController}" />
<ui:param name="action" value="affecterTache" />
</ui:include>
</h:form>
</h:panelGrid>
</p:dialog>
我的 dataTable
的更新在管理bean:
The update of my dataTable
is in the managed bean:
public void affecterTache() {
article.setTache(selectedSingleTache);
RequestContext.getCurrentInstance().update("form:tabArticle");
}
推荐答案
A dataTable
是一个命名容器,因此其中的组件将以 dataTable
的 id
。而且,表格(每行)中显示的数据的每次迭代都将产生 id
s的效果。由于表单也是一个命名容器,因此您的 outputText
组件 id
将生成为第一行中的formId:tab文章:0:columnTache
第二行中的 formId:tabArticle:0:columnTache
A dataTable
is a naming container, so components within it will be prepended with the dataTable
's id
. Also, each iteration of data presented in the table (each row) will have effect of the generated id
s. Since a form is also a naming container, your outputText
component id
s will be generated as formId:tabArticle:0:columnTache
in the first row, formId:tabArticle:0:columnTache
in the second row, etc.
如果您在 commandButton
上设置 id
,您将获得 formId:tab文章:0:myButton
, formId:tab文章:1:myButton
等你可以使用这个 id
在您的点击处理程序中创建相应的 inputText
clientId
。但是,由于您将文章
传递给某个方法,因此您无法检查是否点击了哪个按钮。所以首先将点击监听器方法更改为:
If you would set an id
on your commandButton
you will get formId:tabArticle:0:myButton
, formId:tabArticle:1:myButton
etc. You can use this id
in your click handler to create the corresponding inputText
clientId
. But, since you pass article
to a method, you are not able to check which button was clicked. So first of all change the click listeners method to:
public void useArticle(AjaxBehaviorEvent event) {
UIComponent component = event.getComponent();
}
现在,您知道点击了哪个按钮(事件.getComponent()
),但您还需要文章
。您可以将其设置为XHTML中按钮的属性:
Now, you know which button was clicked (event.getComponent()
), but you need your article
as well. You can set that as an attribute to your button in XHTML:
<p:commandButton id="myButton"
oncomplete="PF('dialogTasks').show();"
update=":formSelectAffecterTache">
<p:ajax event="click"
listener="#{myController.useArticle}" />
<f:attribute name="article"
value="#{article}" />
</p:commandButton>
此属性可以在您的监听器中读取:
This attribute can be read in your listener:
Article article = (Article) component.getAttributes().get("article");
现在,为了更新 inputText
使用按钮的 clientId
:
Now, for updating the inputText
, simply use the button's clientId
:
String update = component.getClientId().replace(":myButton", ":columnTache");
将其存储在您的bean中,当您准备好这样做时,更新:
Store it in your bean, and when you are ready to do so, update:
RequestContext.getCurrentInstance().update(update);
另见:
- How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
- Send additional parameter to Ajax event listener
这篇关于如何更新PrimeFaces dataTable上的特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!