I am new to Vue.js and cannot open a modal window to update a record in a table with all users. I want to pass the user id to the function for further work with this user in the function. Any help!
Here is my AdminPanel.vue:
- html:
Here I am rendering a table with a list of users
<template>
<!--list users-->
<div class="all-users" id="el">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Список пользователей</div>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Roles</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr v-for="user in allUsers" :key="user.id">
<td>{{ user.id }}</td>
<td>{{ user.username }}</td>
<td>
<div v-for="role in user.roles" :key="role.id">
{{ role.name }}
</div>
</td>
<td>
<button type="button" class="btn btn-primary" @click="openModal(user.id)">Update</button>
</td>
<td>
<button class=”Search__button” @click="deleteUserById(user.id)">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!--modal-->
<div v-if="modalIsVisible">
<transition name="modal">
<div class="modal-mask">
<div class="modal-wrapper">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Изменить {{ user.username }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" @click="closeModal()">×</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" @click="closeModal()">Закрыть</button>
<button type="button" class="btn btn-primary">Сохранить</button>
</div>
</div>
</div>
</div>
</div>
</transition>
</div>
</div>
</template>
- js:
And here I am doing CRUD operations. For update, I want to call a function that will open a modal window and display the user data to change
<script>
import api from './backend-api'
export default {
name: 'AdminPanel',
data() {
return {
allUsers: [],
errors: [],
modalIsVisible: false,
updateUser: {
username: '',
password: '',
role: '',
id: 0
}
}
},
methods: {
getAllUsers() {
api.getAllUsers()
.then(response => {
this.allUsers = response.data
})
.catch(e => {
this.errors.push(e)
})
},
deleteUserById(id) {
api.deleteUserById(id)
.then(response => {
const index = this.allUsers.findIndex(user => user.id === id)
if (~index) {
this.allUsers.splice(index, 1)
}
})
},
openModal(id) {
this.modalIsVisible = true
api.getUser(id).then(response => {
this.updateUser.id = response.data.id
this.updateUser.username = response.data.username
this.updateUser.password = response.data.password
this.updateUser.role = response.data.roles[0].name
})
},
closeModal() {
this.modalIsVisible = false
}
},
created() {
this.getAllUsers()
}
}
</script>