I'm using a bootstrap-vue table to display infos I'm retrieving from a JSON. One info I'm receiving is an int named "Status", I want to change the color of the entire row according to this variable, e.g, if Status is equal to 1, then row is green. ]
In the documentation of bootstrap-vue it shows the row changing color according to a _rowVariant object inside each element in the item array data, but how can I change the color of my row without having this object in my items array? If not possible, how can I insert this variable into every object of my array?
Here's the code regarding the table content:
<b-container fluid>
<b-table hover :items="requests" :fields="fields"
@row-clicked="onRowClicked"
>
<template slot="show_details" slot-scope="row">
<!-- we use @click.stop here to prevent emitting of a 'row-clicked' event -->
<b-button size="sm" @click.stop="row.toggleDetails" class="mr-2">
{{ row.detailsShowing ? 'Hide' : 'Show'}} Details
</b-button>
</template>
<template slot="row-details" slot-scope="row">
<b-card>
<b-row class="mb-2">
<b-col sm="3" class="text-sm-right"><b>Info 1:</b></b-col>
<b-col>{{ row.item.horas_info }}</b-col>
</b-row>
<b-row class="mb-2">
<b-col sm="3" class="text-sm-right"><b>Info 2:</b></b-col>
<b-col>{{ row.item.pdf }}</b-col>
</b-row>
<b-button size="sm" @click="row.toggleDetails">Hide Details</b-button>
</b-card>
</template>
</b-table>
</b-container>
You can use computed properties to add extra field to item list:
computed: {
formartedItems () {
if (!this.requests) return []
return this.requests.map(item => {
item._rowVariant = this.getVariant(item.Status)
return item
})
}
},
methods: {
getVariant (status) {
switch (status) {
case 1:
return 'success'
case 1:
return 'danger'
default:
return 'active'
}
}
}
then in HTML code:
<b-table hover :items="formartedItems" :fields="fields" @row-clicked="onRowClicked">
...
</b-table>
If you want more customized style, you can check tdClass
, thClass
or thStyle
in bootstrap-vue table.