簡體   English   中英

ConfirmDialog確認后,從數據庫中刪除行

[英]delete row from database after ConfirmDialog confirmed

好的,我進行了一些搜索,我嘗試了許多對我來說似乎很好的方法,但仍然無法正常工作,在用戶單擊dialog2中的confirm之后,我希望從表“ user”中刪除一行

所以我喜歡這樣

階級親戚

@ManagedBean
@ViewScoped
public class afficherUs implements Serializable {
   private String idU;
   private List<Compte> comptes=new ArrayList<Compte>();

   private Compte selectedc = new Compte();

   private DataModel tdata ;

    public afficherUs() {

          }
  // getter and setter 

   public void delete(){   <!-- Method to remove -->

    System.out.println("in delete");
    comptes.remove(selectedc);
    tdata.setWrappedData(comptes);

   }
   public void editU(){
 Session se=geoUtil.getSessionFactory().getCurrentSession();
        Transaction tr=se.beginTransaction();
        Compte cp=new Compte();
        cp=selectedc;
        se.merge(cp);

        tr.commit();
        int i=0;
        boolean ok=false;
        while(i<comptes.size() && true==false){
            if(comptes.get(i).getId().equals(cp.getId())){
               ok=true;
               comptes.remove(i);
               comptes.add(i, cp);
            }
            i++;
        }
     tdata.setWrappedData(comptes);
   }
}

和dialog2:

<!-- Suppression Form -->
    <h:form id="supprimer">           

      <p:dialog header="Suppression" widgetVar="dialog2" resizable="false"
              width="300" showEffect="explode" hideEffect="explode">

        <h:panelGrid id="dis" columns="2" cellpadding="4">
            <h:outputText value="Valider Suppression"  style="color:#930303;" ></h:outputText><h:outputText value="#{afficherUs.selectedc.id}" ></h:outputText>

            <p:commandButton value="Supprimer" action="#{afficherUs.delete}" update=":f:form:ila" oncomplete="dialog2.hide()" ajax="true"></p:commandButton><p:commandButton value="Annuler" oncomplete="dialog2.hide()"></p:commandButton>

        </h:panelGrid>
    </p:dialog> 
    </h:form> 

並且我在cammandbutton上調用方法單擊了我的DataTable

 <p:column style="width:4%">  
       <p:commandButton id="supprimerButton" update=":f:form:supprimer:dis" oncomplete="dialog2.show()" icon="ui-icon-trash" title="View">  
                <f:setPropertyActionListener value="#{cmd}" target="#{afficherUs.selectedc}" />
        </p:commandButton> 
 </p:column> 

第一:創建具有屬性selectionMode selection rowKey表,其中selection持有選定行的bean的屬性。 例:

<p:dataTable id="ila" var="cmd" value="#{afficherUs.tdata}" paginator="true" 
rowkey="#{cmd.id}" rows="6" widgetVar="ChefTable" selection="#{afficherUs.selected}">

第二:創建按鈕將在其中顯示刪除對話框並更新信息。 例:

<p:commandButton id="delButton" value="Delete" update=":f:form:supprimer"
                                                 action="dialog2.show()"/>

第三:編寫帶有按鈕的對話框,以更新表中的信息。 例:

<p:commandButton value="Yes" actionListener="#{afficherUs.delete}"
                        update=":f:form:ila" oncomplete="delApp.hide()"/>

第四:在您的managedbean中,從ArrayList中刪除selected的示例:

public void delete(){ 
    System.out.println("in delete");
    comptes.remove(selected);
    //Some code here
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM