简体   繁体   中英

Color the cell of a table depending on the value using javascript

I am trying to color cells of an HTML table (which is automatically generated by Python and Django) depending on cells' content.

Here is my table. I would like to color only the column "status". When there is the word "Cloture" I would like to color the cell in green :

在此处输入图像描述

My table is generated by Python and Django with this code :

 <table class="list_homepage" id="myTable">

        {% for field_name in action_fields_name %}
        <th>{{ field_name }}</th>
        {% endfor %}

        {% for action in action %}
        <tr class="table-row">
        <td>
            <a style="color: #ff0000; text-align: center;" href="{% url 'action-delete' action.0.1 %}"><i class="bi bi-trash3" style="font-size: 30px;"></i></a>
            <a style="color: #3CB4E6; text-align: center;" href="{% url 'action-update' action.0.1 %}"><i class="bi bi-pencil-square" style="font-size: 30px;"></i></a>
        <br>
            <a style="; text-align: center;" href="{% url 'action-detail' action.0.1 %}">Detail</a></td>

            {% for field in action %}
            <td>{{ field.1 }}</td>

            {% endfor %}
        </tr>

        {% endfor %}
    </table>

This is how one row of my table is in HTML :

<tr class="table-row">
        <td style="border-bottom: 1px solid black;";>
            <a style="color: #ff0000; text-align: center;" href="/action/13/delete/"><i class="bi bi-trash3" style="font-size: 30px;"></i></a>
            <a style="color: #3CB4E6; text-align: center;" href="/action/13/update/"><i class="bi bi-pencil-square" style="font-size: 30px;"></i></a>
        <br>
            <a style="; text-align: center;" href="/action/13/">Detail</a></td>

            
            <td style="color: black; text-align: center; border-bottom: 1px solid black;";>13</td>

            
            <td style="color: black; text-align: center; border-bottom: 1px solid black;";>Panne THM 08</td>

            
            <td style="color: black; text-align: center; border-bottom: 1px solid black;";>May 12, 2022, midnight</td>

            
            <td style="color: black; text-align: center; border-bottom: 1px solid black;";>Cloture</td>

            
            <td style="color: black; text-align: center; border-bottom: 1px solid black;";>May 9, 2022, 11:30 a.m.</td>

            
            <td style="color: black; text-align: center; border-bottom: 1px solid black;";>METRO</td>

            
            <td style="color: black; text-align: center; border-bottom: 1px solid black;";>METRO</td>

            
            <td style="color: black; text-align: center; border-bottom: 1px solid black;";>Romain</td>

            
        </tr>

I tried something in JQuery but it doesn't work :

$('select').closest(function(){
        if($(this).val() === 'Cloture') {
      $(this).parent().css({
        'background-color': 'green'
      });
    }else{
        $(this).parent().css({
        'background-color': 'white'
      });
    }
})

CSS code associated to the JQuery solution :

.green {
       background-color: lightgreen;
       }

.white {
       background-color: white;
       }

If someone sees a solution using javascript that would be really helpful !

Try this:

 function changeColor() { var td = $("#myTable" + " td"); $.each(td, function(i) { if ($(td[i]).html() == 'Cloture') { $(td[i]).addClass("green"); } else { $(td[i]).addClass("white"); } }); } changeColor();
 tr { border-top: 1px solid #C1C3D1; border-bottom: 1px solid #C1C3D1; color: #666B85; font-size: 16px; text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1); } td { background: #FFFFFF; padding: 20px; text-align: left; vertical-align: middle; font-weight: 300; font-size: 18px; text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1); border-right: 1px solid #C1C3D1; } td:last-child { border-right: 0px; } .green { background-color: lightgreen !important; } .white { background-color: white !important; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="myTable"> <tr class="table-row"> <td style="border-bottom: 1px solid black;"> <a style="color: #ff0000; text-align: center;" href="/action/13/delete/"><i class="bi bi-trash3" style="font-size: 30px;"></i></a> <a style="color: #3CB4E6; text-align: center;" href="/action/13/update/"><i class="bi bi-pencil-square" style="font-size: 30px;"></i></a> <br> <a style="; text-align: center;" href="/action/13/">Detail</a> </td> <td style="color: black; text-align: center; border-bottom: 1px solid black;">13</td> <td style="color: black; text-align: center; border-bottom: 1px solid black;">Panne THM 08</td> <td style="color: black; text-align: center; border-bottom: 1px solid black;">May 12, 2022, midnight</td> <td style="color: black; text-align: center; border-bottom: 1px solid black;">Cloture</td> <td style="color: black; text-align: center; border-bottom: 1px solid black;">May 9, 2022, 11:30 am </td> <td style="color: black; text-align: center; border-bottom: 1px solid black;">METRO</td> <td style="color: black; text-align: center; border-bottom: 1px solid black;">METRO</td> <td style="color: black; text-align: center; border-bottom: 1px solid black;">Romain</td> </tr> </table>

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