2012-05-17 24 views
22

是否有可能從JavaScript更新PrimeFaces組件,以便強制刷新?如何從primefaces中的JavaScript觸發組件刷新?

我在對話框中使用此按鈕進行ajax保存調用。 我在oncomplete事件上附加了自定義JavaScript。

<p:growl life="1500" id="showmessage"/> 
<p:dialog id="addMemberDialog" widgetVar="addMemberDlg"> 
    <!-- More Code --> 
    <p:commandButton value="Save" 
     actionListener="#{memberManagedBean.save}" 
     oncomplete="handleSaveNewMember(xhr, status, args)" 
     update=":memberListForm:membersTable createupdateform " 
     process="@form" /> 
</p:dialog> 

..during保存按鈕,我在這裏添加消息來將其顯示給用咆哮組件客戶端。

public void save(ActionEvent event) { 
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, 
      "Successfuly Add user", "Successfuly Add user"); 
    FacesContext.getCurrentInstance().addMessage(null, message); 

} 

我的問題是,我怎麼能測序UI這樣,我應該先躲前咆哮組件可以顯示meesage對話?

function handleSaveNewMember(xhr, status, args) { 
    addMemberDlg.hide(); 
    //update the growl after the dialog was hidden? 
} 

發生什麼事情是,咆哮組件同時顯示在對話框旁邊。

謝謝。

+0

您必須在單擊按鈕上顯示對話框。你有沒有嘗試從該按鈕更新咆哮而不是從對話框中的保存按鈕? – rags

回答

4

你總是可以做這樣的事情(從您的保存按鈕更新屬性刪除showmessage ID)

<h:commandButton style="display:none" id="myBtn" > 
    <f:ajax render=":showmessage"/> 
</h:commandButton> 

function handleSaveNewMember(xhr, status, args) { 
    ... 
    jQuery("#myBtn").click(); 
} 

編輯 但無論如何,在當前的代碼,是不是該對話框處於封閉同時grwol正在更新?

+0

你的意思是需要一個「虛擬」按鈕,這不能通過一些JavaScript API完成?如果是這樣的話,我想知道我應該按照這個鏈接http://forum.primefaces.org/viewtopic.php?f=3&t=18190&view=previous。由於所有東西都已經被激活,所以我應該從客戶端自己觸發消息。我的理解是否正確? –

+0

但在你的情況下,你想發送消息從服務器,而不是從js ...關於我的方法...我想有其他方式...但這是我如何觸發jsf元素從js的「重新呈現」:) – Daniel

+0

哦,你是對的。我已經從更新列表中刪除showmessage,因爲這會自動顯示facesmessage。讓我試着進一步解釋,點擊保存按鈕,我已經排隊了facesmessage。由於咆哮的自​​動更新是錯誤的,所以消息不會顯示出來。這是完成JavaScript功能的目的。會發生什麼,我會先隱藏對話框,然後我想讓咆哮刷新並顯示我排隊的消息。這可能嗎? –

0

爲什麼不能把p:Dialog放在< h:panelGroup>裏面。像

< h:panelGroup id="addUser" rendered = "boolean value " > 
    < p:dialog id="addMemberDialog" widgetVar="addMemberDlg" > 
     <!-- More Code --> 
     < p:commandButton value="Save" actionListener="#{memberManagedBean.createOrUpdate}" 
       oncomplete="handleSaveNewMember(xhr, status, args)" 
       update=":memberListForm:membersTable createupdateform :showmessage :addUser" 
       process="@form" /> 
    </p:dialog> 
< /h:panelGroup> 

應該在你的保存方法中設置的布爾值。在保存方法中將其設置爲false時,它在更新時不會顯示。所以只會顯示咆哮信息。但在調用此save方法之前,此布爾值設置爲true。

+0

我不明白這是如何解決OP的特殊問題。 – BalusC

44

您可以使用PrimeFaces'<p:remoteCommand>

<p:remoteCommand name="updateGrowl" update="showmessage" /> 

這是要調用

<p:commandButton ... oncomplete="addMemberDlg.hide(); updateGrowl();" /> 

這種特殊情況下但是有一個簡單的方法。將<p:growl>autoUpdate屬性設置爲true

<p:growl autoUpdate="true" life="1500" id="showmessage"/> 

它會在每個ajax請求中自動更新自己。如果你的組件實際上不支持它,那麼你總是可以將它包裝在<p:outputPanel>中,該組件也支持該屬性。

<p:outputPanel autoUpdate="true"> 
    ... 
</p:outputPanel> 
+0

如果讀者使用PF的侏羅紀版本,只需要一個FYI,咆哮的自動更新只能從3.0及以上版本獲得。 http://blog.primefaces.org/?p=1164 – Mindwin

2

我的建議是:

  1. 使用<p:remoteCommand>actionListener屬性。此屬性調用包含FacesContext.addMessage代碼,這樣一個backing bean的方法:<p:remoteCommand actionListener="myBean.testMethod()" />
  2. 接下來,在你的handleSaveNewMember腳本,調用remoteCommandname屬性addMemberDlg.hide();後這樣說:<p:remoteCommand name="testScript" actionListener="myBean.testMethod()"/>。然後,function handleSaveNewMember(xhr, status, args) { addMemberDlg.hide(); testScript(); }
  3. 添加update屬性remoteCommand指着咆哮組件:<p:remoteCommand name="testScript" actionListener="myBean.testMethod()" update="showmessage" />
  4. commandButton正常。

這對我有效。

問候。

0

您可以使用名爲p:remotecommand的PrimeFaces元素。該元素將執行一個操作(例如,調用一個bean方法)並在該操作之後執行更新。

在這篇文章中有一個例子http://devdublog.blogspot.com/2015/04/best-way-for-calling-method-of.html

+0

答案應該通常包含一些上下文,而不僅僅是一個鏈接。這將更適合作爲對問題的評論。請參閱:http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers –

+1

感謝您的建議。我編輯了評論以完成此功能。 – Gaalvarez