In my HTML page, I have a table that lists information about purchase applications and there is a status column. I would like to change the color of the text according to the value of the status.
For example, if the purchase application is validated, I want the "Validated" to be green. But if the purchase application is declined, I want the "Declined" to be red.
My html code :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="./index.css">
</head>
<body>
<table>
<tr>
<th>Demande n°</th>
<th>Etat</th>
<th>N° Commande</th>
</tr>
<tr v-for="demande in demandes">
<td>{{demande.numDemande}}</td>
<td v-bind:style="{ color: activeColor}">{{demande.etatDemande}}</td>
<td>{{demande.numCommande}}</td>
</tr>
</table>
</div>
<script type="text/javascript" src="/Achats/vue.js"></script>
<script type="text/javascript" src="/Achats/vue-resource.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
My js code :
window.onload = function () {
var consultationAchats = new Vue({
el: "#body",
data: {
demandes: [],
activeColor: 'orange'
},
methods: {
getDemandes: function() {
this.$http.get("/getDemandes").then(function(response) {
if (response.body) {
consultationAchats.demandes = response.body;
}
})
},
styleEtat: function(etat) {
if (etat == "En attente de traitement") {
this.activeColor = 'orange';
} else if (etat == "En cours de traitement") {
this.activeColor = 'orange';
} else if (etat == "Refusé") {
this.activeColor = 'red';
} else if (etat === "Validée") {
this.activeColor = 'green';
}
}
},
created: function() {
this.getDemandes();
}
})
I tried to use the v-bind:style and defined the color into variable activeColor that will be changed in function styleEtat. But I realised that I don't know how or where I can use the function.