简体   繁体   中英

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. 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". Are there any way to put this value to the corresponding cells witouth touch css. I watched this methods:

[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. Is posible?

I found a solution, that was a merge between some answers in Inte.net. To get the colors from the OData I change a little bit the backend. My table before was with colors in HEX, now I changed for letters like this:

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:

.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.

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. I left this here because I couldn't find any example of this that get the colors from the OData, may can help someone. Btw sorry for my english, I hope that all was clear.

NOTE: For the text color you need to put in the css *:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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