Reputation: 646
I loop through a set of users to build a table using VueJS 'for' statement.
There's a column called "approved". If it's true then I want the entire table row to be green. How to achieve that?
<tbody>
<tr v-for="user in users" >
<td v-for="key in columns">
<!-- At This point I can tell if the user is approved but how to change the style of the parent tr element?? -->
<span v-if='key == "approved" && user[key] == 0'>
Not Approved
</span>
<span v-else-if='key == "approved" && user[key] == 1'>
Approved!
</span>
<span v-else>
{{user[key]}}
</span>
</td>
</tr>
</tbody>
Upvotes: 0
Views: 1641
Reputation: 4657
If you always have "approved" column you can simply achieve it in this way:
<tr v-for="user in users"
v-bind:style="{backgroundColor: user['approved'] == 1 ? 'green' : '' }">
or
<tr v-for="user in users"
v-bind:class="{green: user['approved'] == 1}">
.green {
background-color: green:
}
Upvotes: 1
Reputation: 22403
You can use method to assign class to tr
dynamically
<tbody>
<tr v-for="user in users" :class="{'green': isApprove(user, columns)}">
<td v-for="key in columns">
<!-- At This point I can tell if the user is approved but how to change the style of the parent tr element?? -->
<span v-if='key == "approved" && user[key] == 0'>
Not Approved
</span>
<span v-else-if='key == "approved" && user[key] == 1'>
Approved!
</span>
<span v-else>
{{user[key]}}
</span>
</td>
</tr>
</tbody>
<script>
...
methods: {
isApprove(user, columns) {
for (let idx in columns) {
const key = columns[idx]
if (key === 'approved' && user[key] == 1) {
return true
}
}
return false
}
}
</script>
<style>
.green {
background-color: green;
}
</style>
Upvotes: 0