简体   繁体   English

使用来自 OData SAPUI5/JAVASCRIPT 的值放置背景颜色

[英]Put background color with values from OData SAPUI5/JAVASCRIPT

I need to set background colors with the values that I'm getting from the OData.我需要使用从 OData 获取的值设置背景 colors I have the following code:我有以下代码:

oModel.read("/EspCoSet", {
    filters: [aFilters],
    success: function (oD, oR) {
        var oResults = oD.results;
        for (var i = 0; i < oResults.length; i++) {
            var color_fondo = oResults[i].ColorFondo;
            var color_texto = oResults[i].ColorTexto;
        }
    },
    error: function (oE) {
        console.log("Error");
    }
});

In the variable "color_fondo" I'm saving the value of the color in HEX like this "FFFFFF".在变量“color_fondo”中,我以 HEX 格式保存颜色值,如“FFFFFF”。 Are there any way to put this value to the corresponding cells witouth touch css. I watched this methods:有什么方法可以在不触摸 css 的情况下将此值放入相应的单元格。我看了这个方法:

[data-esp="Hospitalizado"] * {
  background-color:   #FFFFFF !important;
  color: red !important;
}

but if I do like this I need to write the information again ( the information that comes from the OData) and then the value from the OData doesn't have any use.但如果我这样做,我需要再次写入信息(来自 OData 的信息),然后 OData 的值没有任何用处。 Is posible?可能吗?

I found a solution, that was a merge between some answers in Inte.net.我找到了一个解决方案,它是 Inte.net 中一些答案的合并。 To get the colors from the OData I change a little bit the backend.为了从 OData 中获取 colors,我稍微更改了后端。 My table before was with colors in HEX, now I changed for letters like this:我之前的表格是 colors 的十六进制,现在我改成了这样的字母:

color_fondo  |  color_texto
WHITE        |  BLACK
BLUE         |  RED  
GREEN        |  BLACK

Then, in the css I put the name of the color as a class, like this:然后,在 css 中,我将颜色名称设置为 class,如下所示:

.BLUE {
    background-color: #9dcfdd !important;
}

and finally in the controller, when I read the model and I get the respective color for each patient, I get the item of each cell and with addStyleClass I apply the color_fondo that is the variable that comes from the OData.最后在 controller 中,当我阅读 model 并获得每个患者的相应颜色时,我获得了每个单元格的项目,并使用addStyleClass应用来自 OData 的变量 color_fondo。

oModel.read("/EspCoSet", {
    filters: [aFilters],
    success: function (oD, oR) {
        var oResults = oD.results;
        for (var i = 0; i < oResults.length; i++) {
            var color_fondo = oResults[i].ColorFondo;
            var color_texto = oResults[i].ColorTexto;
            var aItems = oTable.getItems();
            aItems[i].addStyleClass(color_fondo);
            aItems[i].addStyleClass(color_texto);
        }
    },
    error: function (oE) {
        console.log("Error");
    }
});

and finally I get the colors in my frontend.最后我在前端得到了 colors。 I left this here because I couldn't find any example of this that get the colors from the OData, may can help someone.我把它留在这里是因为我找不到任何从 OData 中获取 colors 的示例,可能会对某人有所帮助。 Btw sorry for my english, I hope that all was clear.顺便说一句,对不起我的英语,我希望一切都清楚。

NOTE: For the text color you need to put in the css *:注意:对于文本颜色,您需要输入 css *:

.RED *{
    color: #c63637 !important;
}

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

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