简体   繁体   English

如何在SAPUI5中动态将样式应用于标签元素

[英]How to apply style to a label element dynamically in SAPUI5

I am new to SAPUI5 and am trying to apply a few styles to the label element. 我是SAPUI5的新手,正在尝试将一些样式应用于label元素。 Below is the piece of the code in the controller. 以下是控制器中的部分代码。

onAfterRendering: function() {
    var busFunId = this.getView().byId("busFun");
    jQuery.sap.byId(busFunId.getId()).parent().css("width", "20%");
},

Below is the line of code for the label element in view.xml. 下面是view.xml中label元素的代码行。

<Label text="Business Function" id="busFun"/>

Here, label element is rendered within a div with default css classes. 在这里,label元素在具有默认css类的div中呈现。 I am trying to adjust the width by applying on the div element, the parent of the label element. 我试图通过应用div元素(label元素的父元素)来调整宽度。

This is not taking effect. 这没有生效。 Please share a solution if anyone have faced the same. 如果有人遇到同样的问题,请分享解决方案。

Rendered HTML: 呈现的HTML:

 <div class=""><label id="busFun" />sample</label></div> 

Expected HTML: 预期的HTML:

 <div class="" style="width:20%"><label id="busFun" />sample</label></div> 

If you only want to adjust the width, don't add styling, but make use of the property width . 如果只想调整宽度,则不要添加样式,而要使用width属性。 (check sap.m.Label for more properties you can use). (检查sap.m.Label以获取更多可以使用的属性)。

<Label text="Business Function" width="20%"/>

If the properties aren't sufficient for your styling, you can make use of css-class(es) to style your Label. 如果属性不足以进行样式设置,则可以使用css-class来为Label设置样式。

<Label class="custom" text="Business Function" width="20%"/>

If you decide to make use of css, don't forget to include a fileName.css file to your project and to register the file in your manifest.json as a resource. 如果您决定使用css,请不要忘记在项目中包含fileName.css文件,并在manifest.json文件中将该文件注册为资源。

Here are a few options: 以下是一些选择:

Apply via JS with addStyleClass: 通过JS与addStyleClass一起应用:

   this.getView().byId('yourControlId').addStyleClass("cssYouLoaded");

Load the css file via manifest: 通过清单加载css文件:

"resources": {
            "css": [
                {
                    "uri": "content/css/style.css"
                }
            ]
        }

And apply on your element: 并应用于您的元素:

<Label text="Business Function" id="busFun" class="style"/>

If you area defining the css file, don't forge the !important. 如果您要定义css文件,请不要伪造!important。 A lot of times the sapui5 Css overwrite your custom ones. sapui5 CSS通常会覆盖您的自定义代码。

.imagemCabecalho {
  margin: 0 !important;
  padding: 0.4em !important;
  visibility: visible;
  width: 8em;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM