javascript - Primefaces 对话框框架——来自菜单项的 dialogReturn 事件

标签 javascript jquery ajax jsf primefaces

我在 Table.xhtml 中有一个 primefaces p:datatable 并且在我正在使用的同一页面上有一个 p:commandbutton使用对话框框架打开一个对话框。 dialog相关的内容在Dialog.xhtml中。我为 Table.xhtmlDialog.xhtml 使用一个名为 TableDialog.java 的支持 bean。当对话框关闭时,p:datatable 中的值会相应地使用

更新
<p:ajax event="dialogReturn" listener="#{tableDialog.test}" update = ":form:colors"  />

Table.xhtml如下

<?xml version="1.0" encoding="ISO-8859-1" ?>
    <!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:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui">
    <h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Table</title>
    </h:head>
    <h:body>
    <h:form id="form">  
     <p:dataTable id="colors" var="col" value="#{tableDialog.resourceList}" rowKey="#{col}"  
                     selection="#{tableDialog.selected}" selectionMode="single">  
     <p:column headerText="Model">  
                #{col}  
            </p:column>  
     <f:facet name="footer">  
                <p:commandButton id="viewButton" value="Add" icon="ui-icon-search" 
                actionListener="#{tableDialog.updateValue}"    > 
       <p:ajax event="dialogReturn" listener="#{tableDialog.test}" update = ":form:colors" onclick="this.form.submit()" />  
                        </p:commandButton> 
            </f:facet>  
    </p:dataTable>  
        <p:contextMenu for="colors">
            <p:menuitem  value="Add" actionListener="#{tableDialog.updateValue}"  update=" :form:colors   "  >
                 
                </p:menuitem>
                </p:contextMenu>
    </h:form> 
   </h:body>
    </html>

这是 Dialog.xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Dialog</title>
</h:head>

    <h:body>  
        <h:form>
          <h:panelGrid id="updateValuePanel" columns="2" style="margin-bottom:10px">  
          <h:outputLabel value="Attribute Value "  />
           <p:inputText id="attributeValue" value="#{tableDialog.attributeValue}" required="true" />
        </h:panelGrid>  

        <p:commandButton id="saveValue" value="Submit" actionListener="#{tableDialog.saveValue}" 
                />  
                <p:commandButton id="cancelValue" value="Cancel "
                action="#{tableDialog.cancelValue}"
                />
        <p:defaultCommand target="saveValue" />
              </h:form>
</h:body>
</html>

TableDialog.java 包含以下代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;



@ManagedBean
@SessionScoped
public class TableDialog {   
    
    public ArrayList<String> resourceList=new ArrayList<String>();
    
private String selected;
String attributeValue = null;

public  TableDialog(){
    this.resourceList.add("Black");
    this.resourceList.add("White");
    
}
public void updateValue(){
    System.out.println("update  value");
    RequestContext context = RequestContext.getCurrentInstance();  
    Map<String, Object> options = new HashMap<String, Object>();
    options.put("resizable", false);
    options.put("dynamic", true);
    options.put("height", 100);
    options.put("width", 300);
    options.put("contentHeight", 100);
    options.put("contentWidth", 250);
    
    context.openDialog("Dialog", options, null);
    
    
}
public void test(){
    System.out.println("test");
    
}

public void  cancelValue(){
    RequestContext context = RequestContext.getCurrentInstance();  
    context.closeDialog(this.attributeValue);
    
    System.out.println("cancel update resource attribute value");
    this.attributeValue = null;
    System.out.println("this.attributevalue  = " + this.attributeValue);
      
}

public void  saveValue(){
    RequestContext context = RequestContext.getCurrentInstance();  
    if (this.attributeValue == null) 
    {
         System.out.println("No value");
           context.execute("noValueDialog.show()"); 
           
          return;
          
    }
    
    System.out.println("this.attributevalue = " + this.attributeValue);
        this.resourceList.add(this.attributeValue);
        this.attributeValue = null;
        context.update("form:resourceAttributeValueDataTable");
        RequestContext.getCurrentInstance().update("form:resourceAttributeValueDataTable");
        
    context.closeDialog(this.attributeValue); 
    System.out.println("after hidden button execute " );
        
}

public String getSelected() {
    return selected;
}

public void setSelected(String selected) {
    this.selected = selected;
}
public ArrayList<String> getResourceList() {
    return resourceList;
}
public void setResourceList(ArrayList<String> resourceList) {
    this.resourceList = resourceList;
}
public String getAttributeValue() {
    return attributeValue;
}
public void setAttributeValue(String attributeValue) {
    this.attributeValue = attributeValue;
}


}  

一切正常。

我的问题是:

我想使用 p:contextMenu 打开对话框。 p:menuitem 正确打开对话框,但我无法在对话框关闭后更新 p:dataTable。由于对话框框架只支持p:commandButtonp:commandLink 我不能使用

<p:ajax event="dialogReturn" />

p:menuitem 中。在互联网上搜索变通方法后,我在 http://forum.primefaces.org/viewtopic.php?f=3&t=32131 找到了解决方案。 .它说

"For now, you can do the following to workaround :

  1. Remove p:ajax from menuitem
  2. Add p:commandbutton xhtml and add id="..." and style="display:none"
  3. Add onclick="..." to menuitem to use javascript to trigger the click() event of the >commandbutton, and reference the button where name should be "formID:buttonID"."

我是 java、primefaces 和 ajax 的新手,对 javascript 和 jquery 一无所知。所以我无法弄清楚 onclick="......."里面到底写了什么

<p:menuitem  value="Add" actionListener="#{tableDialog.updateValue}" 
            update=" :form:colors   "  onclick="........" >

这样每当我选择一个菜单项时,隐藏的按钮就会被执行。任何帮助将不胜感激。

最佳答案

我在 How to trigger hidden JSF commandLink in JavaScript? 上找到了解决方案

Table.xhtml 的更新版本包含

  <h:form id="form">  

        <p:dataTable id="colors" var="col" value="#{tableDialog.resourceList}" rowKey="#{col}"  
                     selection="#{tableDialog.selected}" selectionMode="single">  


            <p:column headerText="Model">  
                #{col}  
            </p:column>  




        </p:dataTable>  
        <p:contextMenu for="colors">
                <p:menuitem  value="Add"  onclick="triggerHiddenEvent(); return false;"
                update=" :form:colors   "  >


                </p:menuitem>
                </p:contextMenu>



       <p:commandButton id="hiddenCommand" styleClass="button"  action="#{tableDialog.updateValue}"  style="display:none">
         <p:ajax event="dialogReturn"  update = ":form:colors"  />  

        </p:commandButton>

        <h:outputScript >

      function triggerHiddenEvent() {
        document.getElementById("form:hiddenCommand").click();
      }
    </h:outputScript>

    </h:form>

关于javascript - Primefaces 对话框框架——来自菜单项的 dialogReturn 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931384/

相关文章:

javascript - 使用 jQuery 更新滚动条上的多个背景位置?

javascript - 在 html 文件中引用 javascript?

jquery - JSONP 与 Jquery 1.9 和 Jersey 2.5.1

javascript - 在多个页面 html/php 中拆分一个非常长的表

javascript - THREE.meshphongmaterial 不工作 - 呈现黑色

javascript - 如何使用 JQuery 倒计时从 Bootstrap 日期选择器中开始计算 "change"事件后的剩余时间?

jquery - CSS、z-order 和 jQuery 迷你菜单

javascript - 我希望 html 表只能通过 jquery javascript 双击才能编辑

javascript - 使用内联参数触发 javascript 事件

javascript - 使用 javascript 访问外部 CSS