Environment:
vue@^2.6.10:
vuetify@^2.1.0
I want to use v-data-table
to show search results and add evaluate button in each row in the v-data-table
.
Unfortunately I have two issues:
What do I need to change?
Template
<v-data-table
:headers="headers"
:items="search_result"
>
<template slot="items" slot-scope="row">
<td>{{row.item.no}}</td>
<td>{{row.item.result}}</td>
<td>
<v-btn class="mx-2" fab dark small color="pink">
<v-icon dark>mdi-heart</v-icon>
</v-btn>
</td>
</template>
</v-data-table>
Script
data () {
return {
headers: [
{ text: 'no', value: 'no' },
{ text: 'result', value: 'result' },
{ text: 'good', value: 'good'},
],
// in real case initial search_result = [], and methods: search function inject below data
search_result: [{no: 0, result: 'aaa'}, {no:2, result: 'bbb'],
}
},
You have bound your items to the fields array. All you need to do is push the new value to the array and it will add the row. Vuetify has good documentation, they have a CRUD example that does this. They have the new item/row button at the top of the table, but you can put it anywhere on the page.
– These Components call TutorialDataService methods which use axios to make HTTP requests and receive responses. You will see some options, choose default (babel, eslint). After the Vue project is created successfully, we import Vuetify with command: vue add vuetify.
You will see some options, choose default (babel, eslint). After the Vue project is created successfully, we import Vuetify with command: vue add vuetify.
You have bound your items to the fields array. All you need to do is push the new value to the array and it will add the row. Vuetify has good documentation, they have a CRUD example that does this.
item
, not items
<tr>
into slot template@click="onButtonClick(row.item)
to v-btn
and create method onButtonClick
<v-data-table :headers="headers" :items="search_result">
<template v-slot:item="row">
<tr>
<td>{{row.item.no}}</td>
<td>{{row.item.result}}</td>
<td>
<v-btn class="mx-2" fab dark small color="pink" @click="onButtonClick(row.item)">
<v-icon dark>mdi-heart</v-icon>
</v-btn>
</td>
</tr>
</template>
</v-data-table>
methods: {
onButtonClick(item) {
console.log('click on ' + item.no)
}
}
...solution above is replacing default row rendering with your own so expect some of the v-data-table
features to not work (didn't try but I expect row selection, grouping, in place editing etc. will be broken). If that's problem for you, here is alternative solution:
{ text: "", value: "controls", sortable: false }
item
slot (row rendering). Override item.controls
slot instead. Notice "controls" is the same as in column definition - we are overriding just rendering of "controls" column <v-data-table :headers="headers" :items="search_result">
<template v-slot:item.controls="props">
<v-btn class="mx-2" fab dark small color="pink" @click="onButtonClick(props.item)">
<v-icon dark>mdi-heart</v-icon>
</v-btn>
</template>
</v-data-table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With