How do I conditionally render an ?

前端 未结 12 2089
刺人心
刺人心 2020-12-28 16:55

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


    

        
相关标签:
12条回答
  • 2020-12-28 17:14

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

    <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.

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

    0 讨论(0)
  • 2020-12-28 17:18

    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.

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

    ie.

    JSF facets

    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.

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

    0 讨论(0)
  • 2020-12-28 17:26

    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):

    ...

    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).

    0 讨论(0)
  • 2020-12-28 17:27

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

    The facet in the template could then be declared as:

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

    Any page can then declare this 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.

    0 讨论(0)
  • 2020-12-28 17:27

    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:

    First, declare a CSS-class like this:

    .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.

    0 讨论(0)
  • 2020-12-28 17:29

    I try this solution and ok. (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>
    
    0 讨论(0)
提交回复
热议问题