简体   繁体   English

Vue - 如何在 Vuetify 数据表的每一行中添加一个按钮?

[英]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.

相关问题 在 Vuetify 中,如何在每一行中添加一个按钮<v-data-table> ?</v-data-table> - In Vuetify, how to add a button into each row in <v-data-table>? Vuetify数据表添加总计行 - Vuetify datatable add totals row 如何在 Vue Vuetify 中使用单个添加节点按钮添加 treeview 节点? - How to add treeview nodes with a single add node button in Vue Vuetify? 如何重用 Vue 和 Vuetify 代码? - How can I reuse Vue and Vuetify code? 如何将父子组复选框添加到 vuetify 数据表? - How can i add parent-child group checkbox to on vuetify datatable? 如何在 vue 和 vuetify 中添加滚动条 - How to add scrollbars to vue and vuetify 如何在 VueJS 中动态添加输入,其中每个下拉列表都指定给同一行中的单选按钮组? - How can I dynamically add inputs in VueJS wherein each dropdown is designated to a radio button group in the same row? 如何在 datepicker vuetify 中添加加载? - How can i add loading in the datepicker vuetify? 如何在 vuetify 上添加 google recaptcha 3? - How can I add google recaptcha 3 on vuetify? 如果我下个月在 vuetify 的日期选择器中单击图标,如何添加按钮单击以调用方法? - How can I add button click for call a method if I click icon next month in the datepicker on vuetify?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM