如何有条件地呈现普通的HTML元素,例如< div>? [英] How to conditionally render plain HTML elements like <div>s?
问题描述
我试图实现一个复合组件,它可以以纯文本的形式显示用户的信息细节,或者在需要的细节是当前连接的用户的细节时通过可编辑的输入文本字段显示。
I'm trying to implement a composite component which either displays the information details of a user in plain text or displays them through editable input texts fields if the desired details are those of the user currently connected.
我知道al UI组件可以通过呈现属性呈现,但那些不是UI组件的属性(例如div)
I know that al UI Components can be rendered via the rendered attribute but what about the ones which are not UI Components (for example divs)
<div class = "userDetails" rendered = "#{cc.attrs.value.id != sessionController.authUser.id}">
Name: #{cc.attrs.value.name}
Details: #{cc.attrs.value.details}
</div>
<div class = "userDetails" rendered = "#{cc.attrs.value.id == sessionController.authUser.id}">
<h:form>
...
</h:form>
</div>
我知道div没有渲染的属性,可能我没有右键右移根本就没有办法。我很容易使用JSTL标签,但我想避免这种情况。
I know that the div doesn't have the rendered attribute and probably I'm not taknig the right approach at all. I could very easily use an JSTL tag but I want to avoid that.
推荐答案
<h:panelGroup layout="block" ... rendered="#{someCondition}">
...
</h:panelGroup>
或者,将它包装在< ui:fragment>
$ b
Alternatively, wrap it in an <ui:fragment>
:
<ui:fragment rendered="#{someCondition}">
<div>
...
</div>
</ui:fragment>
请注意,当您想要ajax更新有条件渲染的组件时,您应该而不是更新其父组件。
Do note that when you'd like to ajax-update a conditionally rendered component, then you should be ajax-updating its parent component instead.
- 用于UI的替代:JSF中的片段
- <一个非JSF组件的条件呈现(href =https://stackoverflow.com/questions/15946985/conditional-rendering-of-non-jsf-components-plain-vanilla-html-and-template-tex/>普通香草HTML和模板文本)
-
- https://stackoverflow.com/questions/9010734/why-do-i-need-to-nest-a-component-with-rendered-some-in-another-component-w\">为什么我需要嵌套一个补偿onent with rendered =#{some}在另一个组件中,当我想要ajax更新它?
- Alternative to ui:fragment in JSF
- Conditional rendering of non-JSF components (plain vanilla HTML and template text)
- Ajax update/render does not work on a component which has rendered attribute
- Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
这篇关于如何有条件地呈现普通的HTML元素,例如< div>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!