简体   繁体   English

如何在数据表的每一行添加按钮?

[英]How to add buttons in each row of data table?

I am using theme DataTable.我正在使用主题数据表。 I want to add edit or delete button in each row with clickable event to get customer id for future operations.我想在每一行中添加带有可点击事件的编辑或删除按钮,以获取客户 ID 以供将来操作。 I didn't get any solution of adding buttons.我没有得到任何添加按钮的解决方案。 I want buttons in actions column.我想要操作列中的按钮。 I tried to make custom object of customers with buttons in html but it shows html in actions column not making buttons.我试图用html中的按钮制作客户的自定义对象,但它在操作列中显示html而不是制作按钮。

这是我的数据表

Html:网址:

                <div class="card">
                    <div class="card-body">
                        <h4 class="card-title">All Customers</h4>
                        <p class="card-title-desc"></p>
                        <div class="row mb-md-2">
                            <div class="col-sm-12 col-md-6">
                                <div id="tickets-table_length" class="dataTables_length">
                                    <label class="d-inline-flex align-items-center">
                                        Show
                                        <b-form-select v-model="perPage" size="sm" :options="pageOptions"></b-form-select>entries
                                    </label>
                                </div>
                            </div>
                            <!-- Search -->
                            <div class="col-sm-12 col-md-6">
                                <div id="tickets-table_filter" class="dataTables_filter text-md-right">
                                    <label class="d-inline-flex align-items-center">
                                        Search:
                                        <b-form-input
                                                v-model="filter"
                                                type="search"
                                                placeholder="Search..."
                                                class="form-control form-control-sm ml-2"
                                        ></b-form-input>
                                    </label>
                                </div>
                            </div>
                            <!-- End search -->
                        </div>
                        <!-- Table -->
                        <div class="table-responsive mb-0">
                            <b-table
                                    :items="customers"
                                    :fields="fields"
                                    responsive="sm"
                                    :per-page="perPage"
                                    :current-page="currentPage"
                                    :sort-by.sync="sortBy"
                                    :sort-desc.sync="sortDesc"
                                    :filter="filter"
                                    :filter-included-fields="filterOn"
                                    @filtered="onFiltered"
                            ></b-table>
                        </div>
                        <div class="row">
                            <div class="col">
                                <div class="dataTables_paginate paging_simple_numbers float-right">
                                    <ul class="pagination pagination-rounded mb-0">
                                        <!-- pagination -->
                                        <b-pagination v-model="currentPage" :total-rows="rows" :per-page="perPage"></b-pagination>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
```

Script:脚本:

 data() {
            return {
                customers: [],
                totalRows: 1,
                currentPage: 1,
                perPage: 10,
                pageOptions: [10, 25, 50, 100],
                filter: null,
                filterOn: [],
                sortBy: "master_number",
                sortDesc: true,
                fields: [
                    { key: "master_number", sortable: true },
                    { key: "name", sortable: true },
                    { key: "co_applicant", sortable: true },
                    { key: "country", sortable: true },
                    { key: "phone", sortable: true },
                    { key: "email", sortable: true },
                    { key: "actions", sortable: true },
                ]
            };
        },
computed: {
            rows() {
                return this.customers.length;
            }
        },
methods: {
            onFiltered(filteredItems) {
                this.totalRows = filteredItems.length;
                this.currentPage = 1;
            },
            getAllCustomers(){
                axios.get(this.$path + 'all_customers').then((response) => {
                    this.customers = response.data.data
                }).catch((error)=>{
                    console.log('error', error);
                });
            }
        },
 mounted() {
            this.getAllCustomers();
            this.totalRows = this.customers.length;
        }

This is what i tried till now.这是我到目前为止所尝试的。

     
     getAllCustomers(){
                axios.get(this.$path + 'all_customers').then((response) => {
                    var i;
                    var cusArr = [];
                    for(i = 0; i < response.data.data.length; i++){
                        var customerArr = {
                            'master_number': response.data.data[i].master_number,
                            'name': response.data.data[i].name,
                            'co_applicant': response.data.data[i].co_applicant,
                            'country': response.data.data[i].country,
                            'phone': response.data.data[i].phone,
                            'email': response.data.data[i].email,
                            'actions': `<button>Edit</button>`,
                        }
                        cusArr.push(customerArr);
                    }

                    this.customers = cusArr
                }).catch((error)=>{
                    console.log('error', error);
                });
            },

you might be able to use the scoped slot in the component try this:您也许可以在组件中使用作用域插槽试试这个:

<b-table>
    <template v-slot:cell(actions)="data">
        <button @click="myAction">Click</button>
    </template>
</b-table>

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

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