Change Vue element style according to data value

Viewed 582

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.

1 Answers

Use a style object to avoid using methods in the template:

data: {
    demandes: [],
    etatColors: {
      "En attente de traitement": "orange",
      "En cours de traitement": "orange",
      "Refusé": "red",
      "Validée": "green"
    }
},

Use it like this:

<td v-bind:style="{ color: etatColors[demande.etatDemande] }"> 
   {{ demande.etatDemande }}
</td>

Here is a demo:

new Vue({
  el: '#app',
  data() {
    return {
      demandes: [
        { etatDemande: "En attente de traitement" },
        { etatDemande: "En cours de traitement" },
        { etatDemande: "Refusé" },
        { etatDemande: "Validée" },
      ],
      etatColors: {
        "En attente de traitement": "orange",
        "En cours de traitement": "orange",
        "Refusé": "red",
        "Validée": "green"
      }
    }
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <table>
    <tr v-for="demande in demandes">
      <td :style="{ color: etatColors[demande.etatDemande] }">
        {{ demande.etatDemande }}
      </td>
    </tr>
  </table>
</div>

Related