I'm trying to implement a table that allows the users to edit a row by toggle with Edit button.
I was able to implement the toggle functionality but I ran into an issue where it toggles all rows instead of single row.
I believe that I have to select the row by index and with my implementation I'm able to do so but it seems I'm not able to find any use for it.
Vue.component('employee-data', {
template:
/*html*/
`
<b-container>
<h3>Employee Data</h3>
<b-pagination v-model="currentPage" :total-rows="rows" :per-page="perPage"
aria-controls="employee-table"></b-pagination>
<b-table striped hover :items="employees" id="employee-table"
:per-page="perPage" :current-page="currentPage" :fields="fields">
<template v-slot:cell(employeeName)="row" v-if="edit">
<b-form-input v-model="row.item.employeeName"/>
</template>
<template v-slot:cell(joinDate)="row" v-if="edit">
<b-form-input v-model="row.item.joinDate"/>
</template>
<template v-slot:cell(selectedDepartment)="row" v-if="edit">
<b-form-input v-model="row.item.selectedDepartment"/>
</template>
<template v-slot:cell(jobDescription)="row" v-if="edit">
<b-form-input v-model="row.item.jobDescription"/>
</template>
<template v-slot:cell(actions)="row">
<b-button @click="toggleEdit(row.index)">
{{ edit ? 'Save' : 'Edit' }}
</b-button>
</template>
</b-table>
</b-container>
`,
props: {
employees: {
type: Array,
required: true
}
},
data() {
return {
edit: false,
perPage: 3,
currentPage: 1,
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',
sortable: false,
}
]
}
},
computed: {
rows() {
return this.employees.length
}
},
methods: {
toggleEdit(index){
this.edit = !this.edit
}
}
})