ConfirmDialog在Primefaces中阻止DataTable进行分页

时间:2014-01-21 15:41:17

标签: java jsf java-ee primefaces jsf-2.2

下午好,

我在数据表中使用了一个列,其中包含一个删除此记录的操作按钮。单击操作按钮时,将显示一个确认对话框,用户可以取消或删除该记录。删除记录后,分页不再起作用。我可以使用PF 4.0以及来自Google Code 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>

我做错了什么或者这是一个错误?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我不建议使用这样的confirmmaDialog,因为您将其定义为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 Class,当table是表的变量小部件时;

function selectCurrentRow_paginator(table,index){

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

}

我希望你能解决你的问题。问候。