如何在运行时更新 JSF 组件的样式
如何在运行时更新 JSF 组件的样式,我必须澄清我想要更改组件的位置并在某些情况下隐藏它。
<ui:define name="reverso" id="reverso" >
<!-- Logo Estado Próspero -->
<p:graphicImage value="./resources/oficiales/prospero.png" style="width: 90px; height: 50px;position: relative; left: 150px" />
<h:form id="dataRfc">
<h:outputText id="display_rfc" rendered="true" value="#{IDWizard.rfc}" binding="#{IDWizard.outRfc}" style="color: #333333;text-align:center;margin-top: 30px" />
</h:form>
</ui:define>
public void setNoPersonal(String noPersonal) {
this.noPersonal = noPersonal;
this.outNombre.setValue(this.noPersonal);
this.outNombre.setRendered(true);
this.outRfc.setStyle("text-align:left;color:red;margin-top:2px");
//component.getAttributes().put("style", "color:red");
this.outRfc.setRendered(true);
}
How to update the style of a JSF component at runtime, I must clarify that I want to change the position of the component and in some cases hide it.
<ui:define name="reverso" id="reverso" >
<!-- Logo Estado Próspero -->
<p:graphicImage value="./resources/oficiales/prospero.png" style="width: 90px; height: 50px;position: relative; left: 150px" />
<h:form id="dataRfc">
<h:outputText id="display_rfc" rendered="true" value="#{IDWizard.rfc}" binding="#{IDWizard.outRfc}" style="color: #333333;text-align:center;margin-top: 30px" />
</h:form>
</ui:define>
public void setNoPersonal(String noPersonal) {
this.noPersonal = noPersonal;
this.outNombre.setValue(this.noPersonal);
this.outNombre.setRendered(true);
this.outRfc.setStyle("text-align:left;color:red;margin-top:2px");
//component.getAttributes().put("style", "color:red");
this.outRfc.setRendered(true);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只在
style
和styleClass
属性中使用 EL 表达式。您可以在 EL 中使用条件运算符?:
根据布尔条件打印不同的字符串。例如,
使用此 getter
和此 CSS
请注意,上面仍然将组件呈现到客户端,因此您可以使用纯 JavaScript 切换可见性。
或者,如果您实际上想完全在服务器端显示/隐藏它,那么您可以使用
rendered
属性来代替。它还只接受 EL 表达式:您只需记住,当其计算结果为 false 时,该组件根本不存在于客户端,因此您将无法显示它再次使用纯 JavaScript 或 Ajax。当使用 Ajax 显示它时,您需要重新渲染其父组件。
根据您的新代码片段更新,这不是正确的方法。您不应该为此将组件绑定到 bean。您还应该在其自己的
.css
文件中定义 CSS 样式,这样更容易维护,并使您的 bean 和视图免受特定 CSS 混乱的影响。例如(我随机猜测样式取决于某种错误/成功状态)
使用这些 getter
和此 CSS
您只需在 bean 的(后)构造函数或操作(侦听器)方法中相应地设置这些布尔值。
You can just use EL expressions in the
style
andstyleClass
attributes. You can use the conditional operator?:
in EL to print different strings based on a boolean condition.E.g.
with this getter
and this CSS
Note that the above still renders the component to the client side, so you would be able to toggle the visibility using plain JavaScript.
Or, if you actually want to show/hide it entirely server side, then you could use the
rendered
attribute for this instead. It also just accepts EL expressions:You only need to keep in mind that when this evaluates
false
, then this component is not present in the client side at all, so you won't be able to show it again using plain JavaScript or Ajax. When showing it using Ajax, you need to re-render its parent component instead.Update based on your new code snippet, this is not the right way. You should not bind the component to the bean for this. You should also define CSS styles in its own
.css
file which is much easier to maintain and keeps your bean and view from specific CSS clutter.E.g. (I randomly guess that the styles are dependent on some kind of error/success state)
with those getters
and this CSS
You just have to set those booleans accordingly in bean's (post)constructor or action(listener) method.