如何有条件地呈现?

时间:2022-08-22 21:13:20

I would like to be able to conditionally omit a footer from a PrimeFaces panel element:

我希望能够有条件地从一个原始面板元素中省略一个页脚:

<p:panel header="some text">
    <f:facet name="footer">
        #{message}
    </f:facet>
    <!-- ... -->
</p:panel>

I hoped that the rendered attribute would work:

我希望呈现的属性可以工作:

<p:panel header="some text">
    <f:facet name="footer" rendered="#{!empty message}">
        #{message}
    </f:facet>
    <!-- ... -->
</p:panel>

But the footer is still rendered, with empty content. It appears that facet does not have the rendered attribute: http://www.jsftoolbox.com/documentation/help/12-TagReference/core/f_facet.html.

但页脚仍然呈现,内容为空。看起来facet没有呈现的属性:http://www.jsftoolbox.com/documentation/help/12tagreference/core/f_facet.html。

What's the right way to do this?

正确的方法是什么?

11 个解决方案

#1


12  

I was able to solve this by swapping the facet out for an attribute. To summarize:

我可以通过将facet转换为属性来解决这个问题。总结:

This works

<p:panel ...>
    <f:attribute name="footer" value="#{message}"/>
    <!-- ... -->
</p:panel>

But this doesn't work

<p:panel footer="#{message}">
    <!-- ... -->
</p:panel>

Neither does this

<p:panel ...>
    <f:facet name="footer">#{message}</f:facet>
    <!-- ... -->
</p:panel>

Nor this

<p:panel ...>
    <f:facet name="footer">
        <h:outputText value="#{message}" rendered="#{!empty message}"/>
    </f:facet>
    <!-- ... -->
</p:panel>

by "works" I mean:

"renders no footer — not just an empty footer — when #{message} is empty or null; otherwise, correctly renders the footer with the specified text."

“当#{消息}为空或空时,不显示页脚—不只是一个空页脚;否则,正确呈现页脚与指定的文本。


PrimeFaces forum thread on this issue

在这个问题上,黄金面论坛的话题。

#2


6  

Here's what I did in trying to conditionally render a facet within a composite component.

这是我在尝试在复合组件中有条件地呈现facet时所做的。

<composite:interface>
    <composite:facet name="header" required="false" />
</composite:interface>
<composite:implementation>
    <p:panel>
        <c:if test="#{empty component.facets.header}" >
            <f:facet id="#{cc.attrs.id}_default_header" name="header">
            all sorts of stuff here
            </f:facet>
        </c:if>
        <c:if test="#{not empty component.facets.header}">
            <composite:insertFacet id="#{cc.attrs.id}_custom_header" name="header" />
        </c:if>
        <composite:insertChildren id="#{cc.attrs.id}_content"/>
    </p:panel>
</composite:implementation>

This let's the user of the composite component supply the header facet if they want, and if they don't, we supply a default. Obviously, instead of providing a default, you could simply not do anything.

这是复合组件的用户,如果他们想要的话,可以提供header facet,如果他们不想,我们就提供默认值。显然,不提供默认值,您可以简单地不做任何事情。

This mixes c:if in jsf controls, but we didn't see any adverse effects.

这混合了c:如果在jsf控制中,但是我们没有看到任何副作用。

#3


4  

You could declare a ui:param and let the template check the param while renderring.

您可以声明一个ui:param,并让模板在renderring的时候检查param。

The facet in the template could then be declared as:

模板中的facet可以声明为:

<f:facet name="#{hideFooter == null or not hideFooter ? 'footer' : ''}">
     #{message}
</f:facet>

Any page can then declare this param

然后,任何页面都可以声明这个param。

<ui:param name='hideFooter' value='#{some rule}' />

and set the appropriate rule for the param. For any page that does not declare the param, the footer will be displayed.

并为param设置适当的规则。对于没有声明param的任何页面,将显示页脚。

#4


2  

I have come across a similar issue with plain JSF. I am not sure how a <p:panel> is rendered, but if it is rendered as a table, you can try this:

我遇到过与普通JSF类似的问题。我不确定一个 是如何呈现的,但是如果它被呈现为一个表,您可以尝试一下:

First, declare a CSS-class like this:

首先,声明一个css类:

.HideFooter tfoot {
    display: none;
}

Then set that class conditionally on the panel:

然后在面板上有条件地设置这个类:

<p:panel styleClass="#{renderFooterCondition ? null : 'HideFooter'}">

The footer is still rendered in the JSF-sense, but it is not displayed and does not take up any space in the page when viewed by the user-agent.

页脚仍然在jsf感觉中呈现,但它没有显示,并且在用户代理查看时不会占用页面的任何空间。

#5


2  

I successfully solved this problem using ui:fragment

我使用ui成功地解决了这个问题:片段。

<ui:fragment rendered="...Test...">
<f:facet name="footer">
...
</f:facet>
</ui:fragment>

works for example to conditionnaly render the footer of a primefaces datatable (the rendered attribute of the facet does not work).

例如,对一个primefaces datatable (facet的呈现属性不工作)的页脚进行条件处理。

#6


0  

Not sure how well this would work for your footer, but I had the same issue with a legend I was trying to conditionally render. I fixed it by using the rendered on anything inside the facet tag.

我不知道这对你的脚注有多好,但是我有一个同样的问题,我想要有条件的渲染。我通过在facet标签内的任何东西来修复它。

<p:fieldset>
<f:facet name="legend">
    <h:outputText value="#{header1}" rendered="#{header1.exists}"/>
    <h:outputText value="#{header2}" rendered="#{not header1.exists}"/>
</f:facet>
content
</p:fieldset>

I had difficulty trying c:if with my ui:repeat, so this was my solution. Not quite the same as your problem, but similar.

我很难尝试c:如果用我的ui:重复,这就是我的解决方案。与你的问题不完全一样,但相似。

#7


0  

Facets are not intended to render HTML, which is why it doesn't have the rendered attribute. Facets add functionality to a page. The term facet is probably a poor choice of name. It's very ambiguous.

facet并不打算呈现HTML,这就是它没有呈现属性的原因。facet向页面添加功能。facet这个词可能是一个糟糕的名字选择。它很模糊。

..if the list compiled by ITworld's Phil Johnson has it right, the single hardest thing developers do is name things.

. .如果由ITworld的菲尔·约翰逊编纂的列表正确的话,开发者所做的最困难的事情就是命名。

ie.

ie。

JSF facets

JSF方面

A project facet is a specific unit of functionality that you can add to a project when that functionality is required. When a project facet is added to a project, it can add natures, builders, classpath entries, and resources to a project, depending on the characteristics of the particular project. JSF facets define the characteristics of your JSF enabled web application. The JSF facets specify the requirements and constraints that apply to your JSF project.

项目facet是一个特定的功能单元,您可以在该功能需要时添加到项目中。当项目facet被添加到项目中时,它可以根据特定项目的特性将属性、构建器、类路径条目和资源添加到项目中。JSF方面定义了JSF支持的web应用程序的特性。JSF方面指定了应用于JSF项目的需求和约束。

The JSF facets supply a set behaviors and capabilities to your web application.

JSF facet向您的web应用程序提供了一个集行为和功能。

#8


0  

This is a counter-answer to the answer from Ludovic Pénet.

这是对卢多维克·佩内特的回答的反答案。

This worked for me in <f:facet name="footer"> in selected p:column items of a p:dataTable (Primefaces 5.3):

我在 中选择p:dataTable (Primefaces 5.3)的列项目中为我工作:

...

Note how I have the ui:fragment inside the f:facet, not outside (not wrapping) it. It definitely completely removes the entire row when every footer facet is tested to NOT render (as far as I can tell, independent of the content within the ui:fragment).

注意我如何拥有ui:在f:facet中,而不是在外部(而不是包装)。当每一个footer facet被测试到不呈现时,它肯定会完全删除整个行(据我所知,独立于ui中的内容:片段)。

#9


0  

Try with this, from primefaces web page

试试这个,从原始的网页。

<p:columnGroup type="footer">
    <p:row>
        <p:column colspan="3" style="text-align:right" footerText="Totals:" />
        <p:column footerText="your value in ajax" />

        <p:column footerText="your value in ajax" />
    </p:row>
</p:columnGroup>

clik here, to view primefaces' webpage

clik在这里,查看黄金面孔的网页。

#10


-1  

Why don't you enclose the content of the footer into a panelGroup which has the rendered attribute?

为什么不将页脚的内容封装到一个具有呈现属性的panelGroup中?

This way:

这种方式:

<p:panel header="some text">
    <f:facet name="footer">
    <h:panelGroup rendered="#{!empty message}">
        #{message}
    </h:panelGroup>
    </f:facet>
    <!-- ... -->
</p:panel>

I do it in my weapp and it works, no footer is rendered.

我在我的武器和它工作,没有页脚被渲染。

I don't use primefaces though, I do it with h:datatable, but I think that it must works with p:panel too.

虽然我不使用黄金面,但我用h:datatable,但我认为它必须与p:面板一起工作。

#11


-1  

I try this solution and ok. (http://www.coderanch.com/t/431222/JSF/java/dynamically-set-panel-header-condition)

我试试这个解。(http://www.coderanch.com/t/431222/JSF/java/dynamically-set-panel-header-condition)

<rich:dataGrid value="#{myMB.student.list}" rendered="#{!empty myMB.student and !empty myMB.student.list}" var="st" rowKeyVar="row">
    <rich:panel>                                            
        <f:facet name="header">
        <h:panelGroup id="panelHeader">
            <h:outputText value="Just one student" id="header1" required="true" rendered="#{!myMB.manyStudents}"/>
            <h:outputText value="#{row+1}º Student"  id="header2" required="true"  rendered="#{myMB.manyStudents}"/>
            </h:panelGroup>                                 
        </f:facet>
<rich:panel>

#1


12  

I was able to solve this by swapping the facet out for an attribute. To summarize:

我可以通过将facet转换为属性来解决这个问题。总结:

This works

<p:panel ...>
    <f:attribute name="footer" value="#{message}"/>
    <!-- ... -->
</p:panel>

But this doesn't work

<p:panel footer="#{message}">
    <!-- ... -->
</p:panel>

Neither does this

<p:panel ...>
    <f:facet name="footer">#{message}</f:facet>
    <!-- ... -->
</p:panel>

Nor this

<p:panel ...>
    <f:facet name="footer">
        <h:outputText value="#{message}" rendered="#{!empty message}"/>
    </f:facet>
    <!-- ... -->
</p:panel>

by "works" I mean:

"renders no footer — not just an empty footer — when #{message} is empty or null; otherwise, correctly renders the footer with the specified text."

“当#{消息}为空或空时,不显示页脚—不只是一个空页脚;否则,正确呈现页脚与指定的文本。


PrimeFaces forum thread on this issue

在这个问题上,黄金面论坛的话题。

#2


6  

Here's what I did in trying to conditionally render a facet within a composite component.

这是我在尝试在复合组件中有条件地呈现facet时所做的。

<composite:interface>
    <composite:facet name="header" required="false" />
</composite:interface>
<composite:implementation>
    <p:panel>
        <c:if test="#{empty component.facets.header}" >
            <f:facet id="#{cc.attrs.id}_default_header" name="header">
            all sorts of stuff here
            </f:facet>
        </c:if>
        <c:if test="#{not empty component.facets.header}">
            <composite:insertFacet id="#{cc.attrs.id}_custom_header" name="header" />
        </c:if>
        <composite:insertChildren id="#{cc.attrs.id}_content"/>
    </p:panel>
</composite:implementation>

This let's the user of the composite component supply the header facet if they want, and if they don't, we supply a default. Obviously, instead of providing a default, you could simply not do anything.

这是复合组件的用户,如果他们想要的话,可以提供header facet,如果他们不想,我们就提供默认值。显然,不提供默认值,您可以简单地不做任何事情。

This mixes c:if in jsf controls, but we didn't see any adverse effects.

这混合了c:如果在jsf控制中,但是我们没有看到任何副作用。

#3


4  

You could declare a ui:param and let the template check the param while renderring.

您可以声明一个ui:param,并让模板在renderring的时候检查param。

The facet in the template could then be declared as:

模板中的facet可以声明为:

<f:facet name="#{hideFooter == null or not hideFooter ? 'footer' : ''}">
     #{message}
</f:facet>

Any page can then declare this param

然后,任何页面都可以声明这个param。

<ui:param name='hideFooter' value='#{some rule}' />

and set the appropriate rule for the param. For any page that does not declare the param, the footer will be displayed.

并为param设置适当的规则。对于没有声明param的任何页面,将显示页脚。

#4


2  

I have come across a similar issue with plain JSF. I am not sure how a <p:panel> is rendered, but if it is rendered as a table, you can try this:

我遇到过与普通JSF类似的问题。我不确定一个 是如何呈现的,但是如果它被呈现为一个表,您可以尝试一下:

First, declare a CSS-class like this:

首先,声明一个css类:

.HideFooter tfoot {
    display: none;
}

Then set that class conditionally on the panel:

然后在面板上有条件地设置这个类:

<p:panel styleClass="#{renderFooterCondition ? null : 'HideFooter'}">

The footer is still rendered in the JSF-sense, but it is not displayed and does not take up any space in the page when viewed by the user-agent.

页脚仍然在jsf感觉中呈现,但它没有显示,并且在用户代理查看时不会占用页面的任何空间。

#5


2  

I successfully solved this problem using ui:fragment

我使用ui成功地解决了这个问题:片段。

<ui:fragment rendered="...Test...">
<f:facet name="footer">
...
</f:facet>
</ui:fragment>

works for example to conditionnaly render the footer of a primefaces datatable (the rendered attribute of the facet does not work).

例如,对一个primefaces datatable (facet的呈现属性不工作)的页脚进行条件处理。

#6


0  

Not sure how well this would work for your footer, but I had the same issue with a legend I was trying to conditionally render. I fixed it by using the rendered on anything inside the facet tag.

我不知道这对你的脚注有多好,但是我有一个同样的问题,我想要有条件的渲染。我通过在facet标签内的任何东西来修复它。

<p:fieldset>
<f:facet name="legend">
    <h:outputText value="#{header1}" rendered="#{header1.exists}"/>
    <h:outputText value="#{header2}" rendered="#{not header1.exists}"/>
</f:facet>
content
</p:fieldset>

I had difficulty trying c:if with my ui:repeat, so this was my solution. Not quite the same as your problem, but similar.

我很难尝试c:如果用我的ui:重复,这就是我的解决方案。与你的问题不完全一样,但相似。

#7


0  

Facets are not intended to render HTML, which is why it doesn't have the rendered attribute. Facets add functionality to a page. The term facet is probably a poor choice of name. It's very ambiguous.

facet并不打算呈现HTML,这就是它没有呈现属性的原因。facet向页面添加功能。facet这个词可能是一个糟糕的名字选择。它很模糊。

..if the list compiled by ITworld's Phil Johnson has it right, the single hardest thing developers do is name things.

. .如果由ITworld的菲尔·约翰逊编纂的列表正确的话,开发者所做的最困难的事情就是命名。

ie.

ie。

JSF facets

JSF方面

A project facet is a specific unit of functionality that you can add to a project when that functionality is required. When a project facet is added to a project, it can add natures, builders, classpath entries, and resources to a project, depending on the characteristics of the particular project. JSF facets define the characteristics of your JSF enabled web application. The JSF facets specify the requirements and constraints that apply to your JSF project.

项目facet是一个特定的功能单元,您可以在该功能需要时添加到项目中。当项目facet被添加到项目中时,它可以根据特定项目的特性将属性、构建器、类路径条目和资源添加到项目中。JSF方面定义了JSF支持的web应用程序的特性。JSF方面指定了应用于JSF项目的需求和约束。

The JSF facets supply a set behaviors and capabilities to your web application.

JSF facet向您的web应用程序提供了一个集行为和功能。

#8


0  

This is a counter-answer to the answer from Ludovic Pénet.

这是对卢多维克·佩内特的回答的反答案。

This worked for me in <f:facet name="footer"> in selected p:column items of a p:dataTable (Primefaces 5.3):

我在 中选择p:dataTable (Primefaces 5.3)的列项目中为我工作:

...

Note how I have the ui:fragment inside the f:facet, not outside (not wrapping) it. It definitely completely removes the entire row when every footer facet is tested to NOT render (as far as I can tell, independent of the content within the ui:fragment).

注意我如何拥有ui:在f:facet中,而不是在外部(而不是包装)。当每一个footer facet被测试到不呈现时,它肯定会完全删除整个行(据我所知,独立于ui中的内容:片段)。

#9


0  

Try with this, from primefaces web page

试试这个,从原始的网页。

<p:columnGroup type="footer">
    <p:row>
        <p:column colspan="3" style="text-align:right" footerText="Totals:" />
        <p:column footerText="your value in ajax" />

        <p:column footerText="your value in ajax" />
    </p:row>
</p:columnGroup>

clik here, to view primefaces' webpage

clik在这里,查看黄金面孔的网页。

#10


-1  

Why don't you enclose the content of the footer into a panelGroup which has the rendered attribute?

为什么不将页脚的内容封装到一个具有呈现属性的panelGroup中?

This way:

这种方式:

<p:panel header="some text">
    <f:facet name="footer">
    <h:panelGroup rendered="#{!empty message}">
        #{message}
    </h:panelGroup>
    </f:facet>
    <!-- ... -->
</p:panel>

I do it in my weapp and it works, no footer is rendered.

我在我的武器和它工作,没有页脚被渲染。

I don't use primefaces though, I do it with h:datatable, but I think that it must works with p:panel too.

虽然我不使用黄金面,但我用h:datatable,但我认为它必须与p:面板一起工作。

#11


-1  

I try this solution and ok. (http://www.coderanch.com/t/431222/JSF/java/dynamically-set-panel-header-condition)

我试试这个解。(http://www.coderanch.com/t/431222/JSF/java/dynamically-set-panel-header-condition)

<rich:dataGrid value="#{myMB.student.list}" rendered="#{!empty myMB.student and !empty myMB.student.list}" var="st" rowKeyVar="row">
    <rich:panel>                                            
        <f:facet name="header">
        <h:panelGroup id="panelHeader">
            <h:outputText value="Just one student" id="header1" required="true" rendered="#{!myMB.manyStudents}"/>
            <h:outputText value="#{row+1}º Student"  id="header2" required="true"  rendered="#{myMB.manyStudents}"/>
            </h:panelGroup>                                 
        </f:facet>
<rich:panel>