javascript - 如何更新在 bootstrap-vue 中单击的按钮

标签 javascript vue.js bootstrap-vue

我正在使用 Bootstrap-Vue显示一个表格,我使用 vue-slot 添加了一个带有更新按钮的额外列。我有这个显示正常,当你点击按钮时我有一个方法被调用。在该方法中,我可以访问该项目的所有信息,但我似乎无法找到访问按钮的方法。我想禁用它并更改它的内容。如何访问按钮元素?我创建了一个 codepen example here这显示了我已经设置和需要做的事情。

HTML


<div id='app'>
    <div>{{ this.output }}</div>
    <b-table hover head-variant="dark"
             id="pages-table"
             :items="items"
             :fields="fields">
        <template slot="actions" slot-scope="data">
            <button class="btn btn-dark" @click="update(data)">Update</button>
        </template>
    </b-table>
</div>

JavaScript


new Vue({
    el: "#app",
    data: {
        output: null,
        items: [
            {
                id: 1,
                name: "Tony"
            },
            {
                id: 2,
                name: "John"
            },
            {
                id: 3,
                name: "Paul"
            }
        ],
        fields: [
            {
                key: "id",
                label: "ID",
                sortable: true
            },
            { key: "name" },
            { key: "actions" }
        ]
    },
    methods: {
        update(data) {
            // I need to disable the button here
            this.output = data;
            data.item.name = "Dave";
        }
    }
});

最佳答案

你可以给按钮添加一个动态引用

 <button class="btn btn-dark" @click="update(data)" :ref="'btn' + data.index">Update</button>

然后只需通过该 ref 访问按钮

  this.$refs["btn" + data.index].disabled = true    

这是一个带例子的codepen

https://codepen.io/vlaem/pen/gNjGQE

除了索引,您还可以使用数据的 id 属性来创建 ref (data.item.id)

虽然个人感觉这不太对,但我认为如果我们可以跟踪相同或不同数组上所有按钮的状态会更好,可能就像下面的示例一样

https://codepen.io/vlaem/pen/GbBMLe

关于javascript - 如何更新在 bootstrap-vue 中单击的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56891188/

相关文章:

php - 如何设置全局变量以供 Laravel 和 Vuejs 访问?

html - 修复图像在 bootstrap 4 中被缩小的问题

javascript - 我希望根据当前路线将多个变量切换为 true 或 false

javascript - MongoDB : How can I reformat this code to include a sort on the name field?

javascript - 如何读取ajax返回值的responseText

javascript - 从 select 中检索选定的文本

javascript - 选中复选框后滚动到顶部

vue.js - 使用 run dev 时找不到模块 'pinia/dist/pinia.mjs'

vue.js - 如何从 bootstrap-vue 模态监听事件?

html - Bootstrap Vue Carousel 高度不适合全屏