SAPUI5更改图标的颜色

问题描述:

我想使用图标并根据型号更改颜色。

I would like to use an icon and change the color depending on the model.

var wirelessHeadItem = new sap.ui.unified.ShellHeadItem({
    icon: "sap-icon://upload-to-cloud"
});

如何将图标的颜色定义为:

How can I define the color of the icon to something like this:

style: "color:{/oSettingsModel/isOnline}"

当我在oSettingsModel中更改isOnline时,图标的颜色会发生变化。

That way the icon would change in color, when I change isOnline in the oSettingsModel.

好吧,似乎此控件无法通过标准属性指定颜色,因此您可以使用自定义数据属性表达式绑定

Well, seems that this control does not have either a possibility to specify the color via standard property, therefore you can apply the workaround using custom data attribute and expression binding.

重点是您可以指定自定义数据项的 writeToDom 属性,并在您的CSS您可以为所需的HTML属性值定义样式: data-co lor-green data-color-red

The main point is that you can specify the writeToDom attribute of the custom data item, and in your CSS you can define the style for the needed HTML attribute value: data-color-green or data-color-red.

JS:

var wirelessHeadItem = new sap.ui.unified.ShellHeadItem({
    icon: "sap-icon://upload-to-cloud",
    customData: [
        new sap.ui.core.CustomData({
            key: "color",
            value: "{= ${/oSettingsModel/isOnline} ? 'green' : 'red' }",
            writeToDom: true
        });
    ]
});

CSS可能如下所示:

CSS might look like this:

div[data-color=green] {
  color: green;
}
div[data-color=red] {
  color: red;
}