Creating vibrant tables with unique color schemes using Thymeleaf

Is there a way to create a table with different colors using Thymeleaf? I have a list of events with risk weights represented by integer values. Each column in the HTML table should be colorized based on certain conditions. For example, if the risk is less than 20, it should be green, if the risk is between 20 and 50, it should be yellow, and if the risk is over 50, it should be red. How can I set multiple conditions for one column? The solution I have tried so far doesn't seem to be working.

<td th:text="${data.riskIndex}" th:if="${data.riskIndex &lt; 20 and data.riskIndex &gt;= 0}" class="green"></td>

Answer №1

In this scenario, my approach would involve utilizing the th:class attribute instead of th:if (or th:classappend if additional classes are shared by every column). Within the th:class, I would make use of a ternary operator to check the condition.

<td th:text="${data.riskIndex}" th:class="${data.riskIndex lt 20} ? 'green' : (${data.riskIndex le 50} ? 'yellow' : 'red')"></td>

Another approach would be to create a utility method that converts the riskIndex to the appropriate color without involving javascript.

For instance, you could create a utility class named DataRepresentationUtils with a method that determines the color based on the index:

package web.utils;

public class DataRepresentationUtils {

    private DataRepresentationUtils() { }

    public static String convertRiskIndexToRGBLiteral(int riskIndex) {

        // logic implementation goes here. Example output: "#00ff00"

    }
} 

Then, you can utilize this method within your template like this:

<td th:text="${data.riskIndex}" th:styleappend="'background-color: ' + ${T(web.utils.DataRepresentationUtils).convertRiskIndexToRGBLiteral(data.riskIndex)} + ';'"></td>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can the CSS be used to conceal the PNG image while still preserving the drop shadow effect?

I recently applied the "Filter: drop-shadow" effect to my png photo and it worked perfectly. However, I am now interested in hiding the actual png image while retaining the shadow effect. My initial attempt involved adjusting the translate and drop-shadow ...

The issue of the back button not functioning in the React Multi-level push menu SCSS has

I have been developing a mobile-friendly Multi-level push navigation menu for my website using dynamically generated links based on projects from my GitHub account. I found a push menu on CodePen here and am in the process of integrating it into React inst ...

What is the best way to verify the information input by the user in a Java application?

I encountered a challenge while working on a program where the user needs to enter a double value. If a string value is entered, I want to prompt the user to try again. Here is the code I have so far: public static void main(String[] args) { Scanner ...

HTML links remain enclosed in a box even after the code has been finalized

Hey there, I have written this code and for some reason, every link shared after it is displaying with a black background. I'm new to coding and can't seem to figure out what I'm doing wrong. Any help would be greatly appreciated. a:link, ...

There are no problems with the table

Attempting to retrieve data from the second table in my database. Successful insertion into the database, but facing issues with querying. Even after modifying version numbers, no effect is observed. Seeking assistance to identify any errors in the proc ...

JAVA Selenium: Uncovering Hidden Elements

I am encountering an issue with my code while attempting to login, particularly with the password field. Here is my code snippet: public static void VentaGC() throws InterruptedException{ System.setProperty("webdriver.chrome.driver", "C:\&bs ...

Replace the bullet icon with a double quote symbol

My HTML List needs a different look - I want to change the bullet icon into a double quote icon. A website that I found interesting is this one. Here's the CSS code I have: .listStyle li:before { list-style: none; content: "\00BB"; } < ...

The width of the table remains consistent

I have created a division that includes two tables stacked on top of each other. However, I am facing an issue where the width of the second table remains fixed and does not change even when I try to increase it. Here is the code snippet below: functio ...

Is there a way to modify this JavaScript code so that it can function with a

I have developed a unique audio player that plays random sections of different songs. Currently, it is hardcoded for one song with three intros, a midsection, and three outros. I am looking to create a system where a list of songs can be randomly chosen fr ...

Adding a Django URL to a personalized email design

Trying to send a Django email using a custom template for my website has been challenging. The Django URL link is not working properly within the template, even though it works fine without the template. The username variable seems to be functioning corre ...

Preserve the visibility of a text input form while the checkbox is selected

Within my HTML code, there is a form that includes a checkbox labeled "other." When this checkbox is selected, a textbox will appear. If the user types in text and submits the form, the textbox disappears, but the checkbox remains checked (as saved in loca ...

Prevent a function from executing using ClearTimeout()

Looking for a way to control the progress of my progress bar using two buttons. Here's the code snippet: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0 /jquery.min.js"></scr ...

Using Java, transmit a UTF-8 encoded string through a web service and XML that could potentially contain unauthorized characters

I am facing an issue with my Java Web Service where I need to send strings in the form of an XML file, but there are some characters in the strings that are considered illegal in XML. I currently replace these characters with question marks, but sometimes ...

When hovering the mouse over, the text fails to appear

Is there something wrong with my overlay on this image? It darkens when I mouse over, but the text I want to display does not show up. Any ideas? http://jsfiddle.net/cryycw3n/ HTML <div class="square"> <div class="info"> <h2&g ...

The Google Map is not showing all the details

Our app is developed using ReactJS on Rails API, with a Google map integrated. However, when visiting this link and clicking "Map View", some grey space appears under the Google Map. An example image can be found here: example We are having trouble findi ...

Injecting variable styles into my VueJS component

I am currently working on developing a custom progress bar to visualize the percentage of completed tasks. The approach I am taking involves using v-bind:styles and passing {width: dynamicWidth + '%'} to regulate the progression of the bar. To ac ...

What is the method for transforming latitude and longitude coordinates into a physical address for a website?

I'm working with an API that provides latitude and longitude coordinates, and I need to retrieve the address information (city, area, etc.) based on these values. For example, there is a website like where if we enter the IP address of a location, i ...

When using Jackson's ObjectMapper to deserialize a JSON object, the method `readValue` returns an object with all fields

Currently, I am grappling with the challenge of deserializing a byte array into a Java type using Jackson object mapper. @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) public class A { String s; ...

Choosing from a dropdown menu by pressing keys

When I press the first letter of an option in my dropdown list, it works fine. However, if I try to press two, three, or four letters consecutively, the control vanishes. For example, if the option is 'Jquery' and I press J only, it works but pre ...

What causes the unusual behavior of `overflow-x: auto;` when used within a parent element with `flex-direction: row;` styling

I have gone through numerous other queries regarding horizontally scrolling tables, but none seem to address this particular problem. When using a responsive table in the default Blazor Server template of a new project built with Bootstrap 4, the browser ...