2012-07-30 68 views
1

我有一個<rich:dataTable>內的列,其中包含<a4j:commandButton>刪除行。RichFaces dataTable行刪除

當通過commandButton的action屬性調用bean的刪除函數(例如#{bean.deleteCar(record.carId)})時,刪除行可以正常工作。不幸的是,當使用<a4j:jsFunction>action屬性時,最後一行的record.carId被傳遞給bean,而不是選定的行。

澄清的一些代碼。下面刪除點擊行的汽車:

<a4j:commandButton value="Delete" 
        action="#{bean.deleteCar(record.carId)}"/> 

下刪除最後一行的車:

<a4j:commandButton value="Delete" 
        onclick="#{rich:component('confirmDeletePane')}.show()"> 
    <a4j:jsFunction name="deleteCar" 
        action="#{bean.deleteCar(record.carId)}" 
        oncomplete="#{rich:component('confirmDeletePane')}.hide();" 
        render="carsTable"/> 
</a4j:commandButton> 

<rich:popupPanel id="confirmDeletePane" header="Delete" modal="true" autosized="true" onmaskclick="#{rich:component('confirmDeletePane')}.hide();"> 
     <h:outputText value="Delete?"/> 
     <h:panelGroup> 
       <a4j:commandButton value="Cancel" onclick="#{rich:component('confirmDeletePane')}.hide(); return false;" /> 
       <a4j:commandButton value="OK" onclick="deleteCar(); return false;"/> 
     </h:panelGroup> 
    </rich:popupPanel> 

正如你可以看到我想要確認刪除之前,用戶的選擇。 在此先感謝。

+1

a4j:jsFunction不應嵌套在commandButton中。你應該在模態內傳遞值,然後在你的確認按鈕中將action屬性的值傳遞給作爲汽車ID的後備bean。 – 2012-07-31 00:26:12

+0

@Ellie感謝您的評論。我嘗試了類似的東西:我使用將值直接傳遞給了後備bean。如果您有任何將值傳遞給模式(例如索引)的示例代碼,請隨時分享。 – 2012-08-02 15:20:12

+0

我會嘗試創建自己的樣本,然後在這裏發佈代碼。 :) – 2012-08-03 01:19:10

回答

0

正如showcase所暗示的那樣,我得出結論認爲,一個改進的解決方案是使用行選擇索引。我在這裏複製以供將來參考。

的commandLink(調用confirmPane):

<a4j:commandLink execute="@this" 
       render="@none" 
       oncomplete="#{rich:component('confirmPane')}.show()"> 
     <h:graphicImage value="/images/icons/delete.gif" alt="delete" /> 
     <a4j:param value="#{it.index}" assignTo="#{carsBean.currentCarIndex}" /> 
</a4j:commandLink> 

的confirmPane(調用JS功能):

<rich:popupPanel id="confirmPane" autosized="true"> 
     Delete? 
     <a4j:commandButton value="Cancel" onclick="#{rich:component('confirmPane')}.hide(); return false;" /> 
     <a4j:commandButton value="Delete" onclick="remove(); return false;" /> 
</rich:popupPanel> 

的jsFunction(調用Bean的功能):

<a4j:jsFunction name="remove" action="#{carsBean.remove}" render="table" execute="@this" 
     oncomplete="#{rich:component('confirmPane')}.hide();" /> 

在背豆:

private int currentCarIndex; // with the getter/setter 

public void remove() { 
    // do your List removal (or DB transaction) 
    // with the currentCarIndex 
}