简体   繁体   English

如何在Vuetify数据表中更改单个按钮的属性?

[英]How to change property of indivudal buttons in vuetify data-table?

I have a vuetify data-table which renders the data received from axios call. 我有一个vuetify数据表,该表呈现了从axios调用接收的数据。

On the last column I'm using template v-slot for bespoke column so I can add two buttons. 在最后一列中,我使用定制模板列的v型槽模板,因此可以添加两个按钮。 v-btn accepts has two props for loading state as per the documentation https://vuetifyjs.com/en/components/buttons#loaders : 根据文档https://vuetifyjs.com/en/components/buttons#loaders,v-btn accepts有两个用于加载状态的道具:

  • :loading :加载
  • :disabled :禁用

The problem is that when I call a function that changes those values, all of the buttons in the data table are receiving the prop state so instead of 1 button displying loader, all of them are. 问题是,当我调用一个更改这些值的函数时,数据表中的所有按钮都处于prop状态,因此它们不是1个显示加载器的按钮,而是全部。

<v-row no-gutters>      
                <v-data-table
                :headers="tableHeaders"
                :items="requests"
                :items-per-page="10"
                class="elevation-1"
                :loading="loading" loading-text="Loading... Please wait"
                >

                <template v-slot:item.action="{ item }">
                    <v-btn color="success" @click="createPacks(item)" :loading="createloading" :disabled="createloading">Create</v-btn>
                    <v-btn color="error" @click="excludeRequest(item)" :loading="cancelloading" :disabled="cancelloading">Cancel</v-btn>     
                </template>
                </v-data-table>
            </v-row>

I'm aware this is because the buttons in the DOM are not unique and the framework is calling all of them but I have no idea how to change that default behaviour. 我知道这是因为DOM中的按钮不是唯一的,并且框架正在调用所有按钮,但是我不知道如何更改该默认行为。


Data: 数据:

export default {
    data() {
        return {
            loading: null,
            error: null,
            tableHeaders: [
                {
                    text: 'ID',
                    value: 'req_id',
                    align: 'center',
                },
                { text: 'Template', value: 'templateConcatenated'},
                { text: 'No of batches', value: 'no_batches' },
                { text: 'Batch size', value: 'batch_size' },
                { text: 'Date requested', value: 'requested_at' },
                { text: 'Requested by', value: 'requester' },
                { text: 'Actions', value: 'action', sortable: false, align: 'center'},
            ],

            createloading: false,
            cancelloading: false,
            successmessage: '',
            errormessage: '',

        };
    },
    methods: {

        createPacks(item) {
            this.loading = true;
            this.createloading = true;
            let page_url = '/api/CreateProcedure?api_token='+this.$api_token;
            axios
                .post(page_url, item)
                .then((response) => {
                    this.loading = false;

                    this.error = null;
                    this.createloading = false;
                    this.successmessage = 'Packs created successfully!';
                    this.errormessage = null;
                })
                .catch(err => {
                    this.createloading = false;
                    this.successmessage = null;
                    this.errormessage = 'Error creating the packs: '+err;
                    console.log("error: "+err);
                })
        },

    }
}

Any idea how to call each individual button to change it's state? 知道如何调用每个按钮来更改其状态吗?

Thank you 谢谢

You've to set the loading properties on the item itself instead of defining them globally: 您必须在项目本身上设置加载属性,而不是全局定义它们:

createPacks(item) {
        this.loading = true;
        item.createloading = true;
        let page_url = '/api/CreateProcedure?api_token='+this.$api_token;
        axios
            .post(page_url, item)
            .then((response) => {
                this.loading = false;

                this.error = null;
                item.createloading = false;
                this.successmessage = 'Packs created successfully!';
                this.errormessage = null;
            })
            .catch(err => {
                item.createloading = false;
                this.successmessage = null;
                this.errormessage = 'Error creating the packs: '+err;
                console.log("error: "+err);
            })
    },

== UPDATE == ==更新==

I added a code based on the codepen you added in the comments, you have to use item.createloasing also in the HTML else it is not working. 我根据注释中添加的代码笔添加了代码,您还必须在HTML中使用item.createloasing,否则它将无法正常工作。 https://codepen.io/reijnemans/pen/LYPerLg?editors=1010 https://codepen.io/reijnemans/pen/LYPerLg?editors=1010

Currently only one button is working at the same time but this is probably because of axios is not defined in the codepen. 当前,只有一个按钮可以同时工作,但这可能是因为在代码笔中未定义axios。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM