0

As part of PSET8 finance additional implementations, I want to dynamically colour the table in history.html.

The simple code uses jinja2? to populate and print out a table in html.

{% extends "layout.html" %}

{% block title %}
    Log In
{% endblock %}

{% block main %}
    <h2>History</h2>
    <table>
        <tr>
            <th>Action</th>
            <th>Symbol</th>
            <th>Shares</th>
            <th>Price per share</th>
            <th>Timestamp</th>
        </tr>
        {% for row in transactions %}
        <tr>
           <td>{{ row['type'] }}</td>
           <td>{{ row['symbol'] }}</td>
           <td>{{ row['shares'] }}</td>
           <td>{{ row['price'] }}</td>
           <td>{{ row['time'] }}</td>
        </tr>
        {% endfor %}
    </table>
{% endblock %}

What I want to do is add some code (JS probably?) that will look at value inside the cell row['type'] and then, depending on that value, change the background of that entire row into a certain colour. However, I have no idea where to begin.

1
  • Maybe you could take the innerText of the things in the table by iterating over them using a for loop, and then make some code that converts the text into a color and then insert that using a backgroundColor, and then use a setInterval to update. Then again, I'm probably not the best person to answer this. Just look at my stackoverflow questions. Commented Jul 21, 2020 at 20:55

1 Answer 1

1

You could add a class attribute to the <tr> element and then add CSS directives for each (or a specific) type. It would be something like <tr class="{{ row['type'] }}"> in the html and in the css (or the style element in layout.html):

.type1 {
    background-color: green;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .