0

I have a simple table rendered which i would like to add some conditional css style formatting to the cells based on the value in that cell. e.g.

If the value of area1 == 'one' assign td-one css style

      <tr v-for="version in versions" :key="version.id">
        <td>{{version.name}}</td>
        <td>{{version.area1}}</td>
        <td>{{version.area2}}</td>
        <td>{{version.area1}}</td>
        <td>{{version.area4}}</td>
      </tr>

Script:

<script>
export default {
  name: 'versions-table',
  props: {
    msg: String
  },
  data () {
    return {
      versions: [
              {
                id: '1',
                name: 'Name1',
                area1: 'one',
                area2: 'one',
                area3: 'two',
                area4: 'three'
              }
           ]
        }
    }
}

CSS

<style scoped>
  table {
    margin-left: auto;
    margin-right: auto;
    padding: 10px;
  }
  td {
    padding: 5px;
  }

  .td-one {
    background-color: green;
  }

  .td-two{
    background: red;
  }

  .td-three{
    background-color: blue;
  }
</style>

1 Answer 1

1

You could use class binding as follows :

  <td :class="{'td-one':version.area1 == 'one'}">{{version.area1}}</td>

and so on

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - it needed a slight adjustment, but has worked well <td :class="{'td-one':version.area1 == 'one'}">{{version.area1}}</td>
you're welcome i fixed it please try to accept it it it' heplful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.