[英]Vue - how can i add a button to each row in a Vuetify datatable?
I'm new to Vue and i'm dealing with datatables.我是 Vue 的新手,我正在处理数据表。 I'm using Vuetify's datatables to create a component that, on page load, sends a request to my backend, receives some data and shows that data on a datatable.
我正在使用 Vuetify 的数据表创建一个组件,该组件在页面加载时向我的后端发送请求,接收一些数据并在数据表上显示该数据。
This is my current code:这是我当前的代码:
<template>
<v-data-table
:headers="headers"
:items="balances"
:items-per-page="5"
class="elevation-1"
></v-data-table>
</template>
<script>
export default {
data() {
return {
search: '',
headers: [
{ text: 'Asset', value: 'symbol' },
{ text: 'Amount', value: 'amount' },
],
balances: [],
}
},
mounted() {
this.fetchData()
},
methods: {
fetchData() {
fetch('MYURL')
.then(response => response.json())
.then(data => {
this.balances = data;
})
}
}
}
</script>
The problem i'm facing now is adding a button to each row in the table, and this button should send a request to my backend with that row's data, so i need to add a button that, when clicked, can fetch the row's data.我现在面临的问题是向表中的每一行添加一个按钮,并且这个按钮应该向我的后端发送一个包含该行数据的请求,所以我需要添加一个按钮,单击该按钮可以获取该行的数据. Is there any way to do that?
有没有办法做到这一点? I tried looking into Vuetify's docs but i didn't found much about a task like this one.
我尝试查看 Vuetify 的文档,但我没有发现很多关于这样的任务的信息。
You can add a new column, set value to action
for example, and add a slot
in the table as follows:您可以添加一个新列,例如将值设置为
action
,并在表中添加一个slot
,如下所示:
new Vue({ el:"#app", vuetify: new Vuetify(), data() { return { search: '', headers: [ { text: 'Asset', value: 'symbol' }, { text: 'Amount', value: 'amount' }, { text: 'Send', value: 'action' } ], balances: [ { symbol: "$", amount: 100 }, { symbol: "$", amount: 200 }, ], } }, methods: { sendRequest(rowData) { console.log(rowData) } } });
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script><link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet"> <v-app id="app"> <v-data-table:headers="headers":items="balances":items-per-page="5" class="elevation-1" > <template v-slot:item.action="{ item }"> <v-btn @click="sendRequest(item)"> Send </v-btn> </template> </v-data-table> </v-app>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.