1 回答
TA贡献1802条经验 获得超5个赞
如果您有唯一标识符(如 id)并且只想允许一次编辑一行,则可以将edit
变量设置为id
当前正在编辑的行的 。
您还可以使用通用槽v-slot:cell()
来减少编写的模板数量。
例子
new Vue({
el: "#app",
data() {
return {
edit: null,
employees: [{
id: 0,
employeeName: "Jane",
joinDate: "11-11-1111",
selectedDepartment: "IT",
jobDescription: "Nerd"
},
{
id: 1,
employeeName: "Peter",
joinDate: "12-12-1212",
selectedDepartment: "Accounting",
jobDescription: "Moneier"
}
],
fields: [{
key: 'employeeName',
label: 'Employee Name',
sortable: true
},
{
key: 'joinDate',
label: 'Join Date',
sortable: true
},
{
key: 'selectedDepartment',
label: 'Selected Department',
sortable: true
},
{
key: 'jobDescription',
label: 'Job Description',
sortable: true
},
{
key: 'actions',
label: 'Actions'
}
]
}
},
computed: {
rows() {
return this.employees.length
}
},
methods: {
onEdit(id) {
this.edit = this.edit !== id ? id : null;
}
}
})
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.18.1/bootstrap-vue.min.css" />
<script src="//cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.18.1/bootstrap-vue.min.js"></script>
<div id="app">
<b-table striped hover :items="employees" :fields="fields">
<template v-slot:cell()="{ value, item, field: { key }}">
<template v-if="edit != item.id">{{ value }}</template>
<b-form-input v-else v-model="item[key]" />
</template>
<template v-slot:cell(actions)="{ item: { id }}">
<b-dropdown variant="primary" text="Actions">
<b-dropdown-item @click="onEdit(id)">{{ edit === id ? 'Save' : 'Edit' }}</b-dropdown-item>
<b-dropdown-divider></b-dropdown-divider>
<b-dropdown-item @click="onDelete(id)">Delete</b-dropdown-item>
</b-dropdown>
</template>
</b-table>
</div>
如果您没有每行的唯一标识符,或者希望能够一次编辑多行,您可以向您的项目添加一个标志(在示例中),这将切换isEditing
当前是否正在编辑该行或不。
实施例2
new Vue({
el: "#app",
data() {
return {
employees: [{
id: 0,
employeeName: "Jane",
joinDate: "11-11-1111",
selectedDepartment: "IT",
jobDescription: "Nerd"
},
{
id: 1,
employeeName: "Peter",
joinDate: "12-12-1212",
selectedDepartment: "Accounting",
jobDescription: "Moneier"
}
],
fields: [{
key: 'employeeName',
label: 'Employee Name',
sortable: true
},
{
key: 'joinDate',
label: 'Join Date',
sortable: true
},
{
key: 'selectedDepartment',
label: 'Selected Department',
sortable: true
},
{
key: 'jobDescription',
label: 'Job Description',
sortable: true
},
{
key: 'actions',
label: 'Actions'
}
]
}
},
computed: {
rows() {
return this.employees.length
}
},
methods: {
onEdit(item) {
if (item.isEditing)
item.isEditing = false;
else
this.$set(item, 'isEditing', true)
}
}
})
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.18.1/bootstrap-vue.min.css" />
<script src="//cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.18.1/bootstrap-vue.min.js"></script>
<div id="app">
<b-table striped hover :items="employees" :fields="fields">
<template v-slot:cell()="{ value, item, field: { key }}">
<template v-if="!item.isEditing">{{ value }}</template>
<b-form-input v-else v-model="item[key]" />
</template>
<template v-slot:cell(actions)="{ item }">
<b-dropdown variant="primary" text="Actions">
<b-dropdown-item @click="onEdit(item)">{{ item.isEditing ? 'Save' : 'Edit' }}</b-dropdown-item>
<b-dropdown-divider></b-dropdown-divider>
<b-dropdown-item>Delete</b-dropdown-item>
</b-dropdown>
</template>
</b-table>
</div>
添加回答
举报