2014-10-20 15 views
0

我想打開一個基於bean布爾值的全球p:confirmDialog。我想有這樣的事情:有條件地打開全​​球p:confirmDialog與p:確認

<p:commandButton value="Save" actionListener="#{bean.save}" 
       update="@form" oncomplete="jsfunction();"> 
    <p:confirm header="Confirm" message="Are you sure?" rendered="#{bean.boolean}"/> 
</p:commandButton> 

但渲染不起作用(我希望)。

此外,我不想複製p:commandButton並使用其呈現的屬性來實現此目的。

有什麼辦法可以在不改變太多東西的情況下完成這個任務嗎?我必須在很多按鈕上做。

回答

4

我知道這個問題是有點老了,但我最近具有條件/動態確認及以上解決方案同樣的問題,我工作得很好。

一些測試後,我建立使用禁用的屬性(在PrimeFaces 6.0中引入)我的按鈕,如:

<p:confirmDialog global="true"> 
    <p:commandButton value="Yes" type="button" /> 
    <p:commandButton value="No" type="button" /> 
</p:confirmDialog> 

<p:commandButton actionListener="#{myBean.myMethod} value="Submit">  
    <p:confirm header="Confirmation" message="Are you sure?" disabled="#{myBean.boolean}"/> 
</p:commandButton> 
+0

哪個版本的PrimeFaces'確認已禁用? – TungstenX 2016-12-02 08:29:16

+1

我正在使用PrimeFaces 6。0,但不知道他們何時添加了該功能 – 2016-12-09 18:38:21

+0

都禁用了屬性和c:如果解決方案有效,但我更喜歡這一項。 – wolf97084 2017-08-07 17:54:30

1

一兩件事,試圖將使用布爾值來渲染<p:confirmDialog>

<p:commandButton value="Save" actionListener="#{bean.save()}" 
       update="@form" oncomplete="jsfunction();"> 
    <p:confirm header="Confirm" message="Are you sure?" /> 
</p:commandButton> 

<p:confirmDialog global="true" rendered="#{bean.boolean}"> 
    <p:commandButton value="Yes" type="button" /> 
    <p:commandButton value="No" type="button" /> 
</p:confirmDialog> 

如果不爲你工作的唯一的其他方式我看到的是你要設法避免兩個<p:commandButton>選項。

+0

嗯謝謝,但我不知道這是否會爲我工作。我爲整個應用程序使用該對話框,並且我必須控制布爾變量在每種情況下都被正確設置。這只是我想有條件地打開它的一些特定情況。不能這樣做嗎? :_ – mrganser 2014-10-20 16:02:26

+0

不幸的是,沒有辦法在''標籤上使用屬性有條件地渲染。你將不得不使用布爾變量來完成你想要的。初始化布爾值爲false,然後在需要使用''的少數特殊情況下將其更改爲true。 – Mark 2014-10-20 16:18:54

+0

是的...謝謝...真遺憾。我正在考慮以某種方式通過編程來實現它的想法 – mrganser 2014-10-21 13:40:27

0

您必須使用visible而不是渲染。該代碼將是這樣的:

<p:commandButton value="Save" actionListener="#{bean.save()}" 
       update="@form" onstart="dlgWgt.show();" oncomplete="jsfunction();"> 
    <p:confirm header="Confirm" message="Are you sure?" /> 
</p:commandButton> 

<p:confirmDialog global="true" visible="false" widgetVar="dlgWgt"> 
    <p:commandButton value="Yes" type="button" /> 
    <p:commandButton value="No" type="button" /> 
</p:confirmDialog> 

,如果你需要進行的onComplete所示的對話框,在適當的情況下設置的dlgWgt.show()。

+0

條件在哪裏?每次都會顯示該對話框。此外,可見屬性默認爲false。 – mrganser 2014-10-21 07:24:08

+0

我不喜歡你的問題,沒有必要有一個布爾變量,對話框始終是隱藏的,但渲染,任何按鈕都需要顯示對話框,調用widget變量的show()函數。如果呈現爲false(隱式或使用條件),則永遠無法訪問它,因爲它根本不存在(它不會呈現,因此您無法通過ajax調用來更新它)。這是我在我的應用程序中處理同一個案例的方式。我希望我沒有理解這個問題。 – Hossein 2014-10-21 10:22:52

+0

'visible =「true」'在加載時顯示'',默認情況下它是false,因爲您不希望它在頁面加載時觸發''。 – Mark 2014-10-21 13:14:50

3

這有點不方便,但是你可以在你的commandButton和p:confirm標籤之間使用JSTL「c:if」標籤。由於JSTL標籤在Facelet頁面的執行流程中被較早解釋,所以p:confirm標籤不會抱怨其父項不是有效的commandButton。例如:

 <p:commandButton action="#{myBean.myMethod} value="Submit"> 
      <c:if test="#{myBeanBean.warningEnabled}"> 
      <p:confirm header="Confirmation" message="#{myBean.warningMessage}" icon="ui-icon-alert"/> 
      </c:if> 
     </p:commandButton>