2014-01-21 26 views
0

下午好,ConfirmDialog防止從數據表中的分頁Primefaces

我使用其中包含一個操作按鈕來刪除這個記錄一個數據表的列。點擊操作按鈕後,會顯示一個確認對話框,用戶可以取消或刪除該記錄。當記錄被刪除時,分頁不再起作用。我可以重現此行爲與PF 4.0和也與真正從谷歌代碼PF快照5.這是一個很簡單的例子重現:

CategoryController.java

@Named 
@ViewScoped 
public class CategoryController { 

    private Entity category; 
    private List<Entity> categories; 

    public CategoryController() { 
    categories = new ArrayList<>(); 

    for (int i = 0; i < 100; i++) { 
     Entity newTest = new Entity(i); 
     categories.add(newTest); 
    } 

    } 
    public void removeCategory(){ 
    categories.remove(category); 
    } 

    public Entity getCategory() { 
    return category; 
    } 

    public void setCategory(Entity category) { 
    this.category = category; 
    } 


    public List<Entity> getCategories() { 
    return categories; 
    } 

    public void setCategories(List<Entity> categories) { 
    this.categories = categories; 
    } 
} 

Entity.java

import java.io.Serializable; 

public class Entity implements Serializable { 
    private static final long serialVersionUID = 4544407140706106480L; 
    long id; 

    Entity(long id) { 
    this.id = id; 
    } 

    public long getId() { 
    return id; 
    } 

    public void setId(long id) { 
    this.id = id; 
    } 

    @Override 
    public boolean equals(Object o) { 
    if (id != ((Entity)o).id) return false; 

    return true; 
    } 

    @Override 
    public int hashCode() { 
    return (int) (id^(id >>> 32)); 
    } 
} 

text.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:p="http://primefaces.org/ui"> 
<h:head> 
</h:head> 
<h:body> 
    <f:view> 
     <h:form id="form"> 
      <p:panel header="#{langRes['setupCategories.pageTitle']}" style="height: 540px;" id="categoryTablePanel"> 
       <!-- category table --> 
       <p:dataTable value="#{categoryController.categories}" var="category" 
          rows="10" id="categoryTable" emptyMessage="#{langRes['setupCategories.emptyTable']}" 
          paginator="true" paginatorPosition="top"> 
        <!-- category id column --> 
        <p:column headerText="#{langRes['setupCategories.idEntry']}" id="idColumn" width="90%"> 
         <p:outputLabel id="categoryId" value="#{category.id}"/> 
        </p:column> 

        <p:column headerText="#{langRes['setupCategories.idEntry']}" id="editColumn" width="10%"> 
         <p:commandButton value="Delete" oncomplete="PF('confirmDialog').show()"> 
          <f:setPropertyActionListener value="#{category}" target="#{categoryController.category}"/> 
         </p:commandButton> 
        </p:column> 
       </p:dataTable> 
      </p:panel> 
      <p:confirmDialog global="true" widgetVar="confirmDialog" id="confirmationDialog"> 
       <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" 
           action="#{categoryController.removeCategory}" 
           update=":setupCategoryForm:categoryTable" 
           onclick="PF('confirmDialog').hide();"/> 
       <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/> 
      </p:confirmDialog> 
     </h:form> 
    </f:view> 
</h:body> 
</html> 

我做得不對或這是一個錯誤?在此先感謝您的幫助 !

+0

ViewScoped是使用CDI註釋還是JSF? @ javax.faces.view或@ javax.faces.bean? – perissf

+0

@ javax.faces.view的CDI Bean – VWeber

回答

0

我不推薦使用這樣的confirmaDialog,因爲您將其定義爲Global。

我始終貫徹確認這樣的:

之前關閉形式:

<p:confirmDialog global="true" showEffect="fade" hideEffect="explode"> 
     <p:commandButton value="Si" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"/> 
     <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/>  
    </p:confirmDialog> 

按鈕內部數據表:

<p:commandButton id="EliminarRegistro" update=":form:mainDataTable,:form:growl" process="mainDataTable" 
         icon="ui-icon-closethick" title="Eliminar" disabled="#{!aiCarreraBean.permisosCrud.eliminar}" 
         actionListener="#{aiCarreraBean.eliminarEvent}" 
         onclick="selectCurrentRow_paginator(dt,#{rowIndex})" > 
         <p:confirm header="Confirmación" message="¿Seguro desea eliminar el registro?" icon="ui-icon-alert" /> 
        </p:commandButton> 

JavaScript類,當表是表的變量部件;

function selectCurrentRow_paginator(table,index){ 

table.unselectAllRows(); 
table.selectRow(index-(table.paginator.cfg.page*table.paginator.cfg.rows) ,false); 
//table.selectRow(index ,false); 

} 

我希望你能解決你的問題。問候。

+0

感謝您的回答!但你如何獲得選定的記錄?你不能使用PropertyActionListener? – VWeber

+0

是啊!您可以使用PropertyActionListener(對於您的情況,我認爲最好),但這是我自己的實現,當我用js函數單擊該按鈕時選擇一行: –

+0

這似乎是Primefaces 5的問題... Datatable和CommandButton不再一起工作。 – VWeber