如何在CommandButton单击上隐藏p:panel

问题描述 投票:1回答:1

我想实现此处编写的内容-How to hide and show p:panel on commandbutton click,但似乎hide()不再可用...

什么是正确的方法?

我尝试过toggle(),但它没有隐藏它:

Panel after toggle()

我真的必须在支持bean上具有一些panelVisibile属性并使用visible=#{.panelVisible}吗?

我正在尝试使用PrimeFaces 7.0。

项目基于https://github.com/Betlista/joinfaces-maven-jar-example

index.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:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Primefaces Test</title>
</h:head>

<h:body>
    <h:form>
        <p:panel id="button_panel" widgetVar="testPanel" closable="true" toggleable="true">
            <h1>Testing</h1>
        </p:panel>

        <p:commandButton onclick="PF('testPanel').show()" value="Show Panel" type="button"/>

        <p:commandButton onclick="PF('testPanel').hide();" value="Hide Panel" type="button"/>
    </h:form>
</h:body>

</html>

结果

hide is not a function error in console

即使在浏览器的控制台中尝试了PF('testPanel'),也只有show(),没有hide()

尝试解决方法

test1.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:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Primefaces Test</title>
</h:head>

<h:body>
    <h:form id="form1">
        <p:panel id="button_panel" widgetVar="testPanel" closable="true" toggleable="true" visible="#{test1View.panelVisible}">
            <h1>Testing</h1>
        </p:panel>

        <p:commandButton value="Show Panel" actionListener="#{test1View.setPanelVisible(true)}" update="form1"/>

        <p:commandButton value="Hide Panel" actionListener="#{test1View.setPanelVisible(false)}" update="form1" />
    </h:form>
</h:body>

</html>

Test1View

package app;

import org.primefaces.PrimeFaces;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
//import javax.faces.bean.RequestScoped;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
//@SessionScoped
@ViewScoped
//@RequestScoped
public class Test1View implements Serializable {

    boolean panelVisible = false;

    public boolean isPanelVisible() {
        return panelVisible;
    }

    public void setPanelVisible(boolean panelVisible) {
        this.panelVisible = panelVisible;
        PrimeFaces.current().ajax().update("form1:button_panel");
    }

}

...但不起作用=仅在刷新后才隐藏/显示...

jsf primefaces
1个回答
-1
投票

PF解决方案

最后,我也尝试了close(),但发现这是我想要的东西,但这是一种具有效果的皮革:

look and feel of panel on close()

不是正确的,如果面板可见,toggle()与面板的close()相同:

look and feel of panel on toggle()

结果为(test6.xhtml

  • closeable="true"
  • closeSpeed=0
  • 不需要切换
<h:form>
    <p:panel id="button_panel" widgetVar="testPanel" closable="true" closeSpeed="0">
        <h1>Testing</h1>
    </p:panel>

    <p:commandButton onclick="PF('testPanel').show()" value="Show Panel" />

    <p:commandButton onclick="PF('testPanel').close();" value="Close Panel" />
</h:form>

替代解决方案

jQuery:为此,我将testPanelJqId定义为面板的类。

<?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:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Primefaces Test</title>
</h:head>

<h:body>
    <h:form id="form1">
        <div class="testPanelJqId">
            <h1>Testing</h1>
        </div>

        <p:commandButton value="Show Panel" onclick="jQuery('.testPanelJqId').show()"/>

        <p:commandButton value="Hide Panel" onclick="jQuery('.testPanelJqId').hide()" />
    </h:form>
</h:body>

</html>

只是感觉不像PrimeFaces方法...在这样的设置中,我根本不需要使用p:panel ...

PrimeFaces p:panel和id替代

<?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:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Primefaces Test</title>
</h:head>

<h:body>
    <h:form id="form1">
        <p:panel id="panel1">
            <h1>Testing</h1>
        </p:panel>

        <p:commandButton value="Show Panel" onclick="jQuery(PrimeFaces.escapeClientId('form1:panel1')).show()"/>

        <p:commandButton value="Hide Panel" onclick="jQuery(PrimeFaces.escapeClientId('form1:panel1')).hide()" />
    </h:form>
</h:body>

</html>

所有示例都在GitHub上可用。

© www.soinside.com 2019 - 2024. All rights reserved.