【问题标题】:How to update a specific cell on a PrimeFaces dataTable如何更新 PrimeFaces 数据表上的特定单元格
【发布时间】:2016-06-29 15:41:37
【问题描述】:

在我的dataTable 中,我将每篇文章与特定任务相关联。

单击commandButton 会显示任务列表,因此我想在选择任务时更新dataTable 中的特定单元格(outputTextid="columnTache")而不更新其余单元格我的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中:

public void affecterTache() {
    article.setTache(selectedSingleTache);
    RequestContext.getCurrentInstance().update("form:tabArticle");           
}

【问题讨论】:

  • 您的数据表正在完全更新,因为您的RequestContext.getCurrentInstance().update("form:tabArticle");
  • 正是@hamza-don 我必须编辑什么,以便它只更新id为columnTache的单元格
  • 你为什么不使用数据表的Cell Edit 功能。

标签: jsf primefaces datatable


【解决方案1】:

dataTable 是一个命名容器,因此其中的组件将以dataTableid 开头。此外,表(每一行)中呈现的数据的每次迭代都会对生成的ids 产生影响。由于表单也是一个命名容器,所以您的outputText 组件ids 将在第一行生成为formId:tabArticle:0:columnTache,在第二行生成formId:tabArticle:0:columnTache,以此类推。

如果您在commandButton 上设置id,您将获得formId:tabArticle:0:myButtonformId:tabArticle:1:myButton 等。您可以在点击处理程序中使用此id 来创建相应的inputText clientId。但是,由于您将 article 传递给方法,因此您无法检查单击了哪个按钮。所以首先将点击监听方法改为:

public void useArticle(AjaxBehaviorEvent event) {
    UIComponent component = event.getComponent();
}

现在,您知道点击了哪个按钮 (event.getComponent()),但您还需要您的 article。您可以将其设置为 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>

这个属性可以在你的监听器中读取:

Article article = (Article) component.getAttributes().get("article");

现在,要更新inputText,只需使用按钮的clientId

String update = component.getClientId().replace(":myButton", ":columnTache");

将它存储在你的bean中,当你准备好这样做时,更新:

RequestContext.getCurrentInstance().update(update);

另见:

【讨论】:

    猜你喜欢
    • 2017-10-23
    • 2014-08-03
    • 2013-02-21
    • 1970-01-01
    • 2014-01-20
    • 2013-09-21
    • 2015-08-25
    • 2013-05-16
    • 2021-03-27
    相关资源
    最近更新 更多