I have a parent component name is StudentComponent
and in this, I have a modal which creates student and update also, It's work fine when modal in the parent component, but when I separate the modal component it's own, and call it in the parent it's not working and when I click edit button it's open but data not manipulate in modal, here is my code below...
StudentComponent (Parent)
<template>
<div id="student">
<span style="position: absolute; right: 1rem; top: .5rem;">
<button type="button" class="btn btn-success" @click="create()">
Add New <i class="fa fa-plus"></i>
</button>
</span>
<div class="table-responsive">
<table class="table table-bordered table-hover">
<tbody>
<tr class="text-center" v-show="students.length" v-for="(student, index) in students"
:key="student.id">
<td>{{ index+1 }}</td>
<td>{{ student.name }}</td>
<td>{{ student.email }}</td>
<td>{{ student.phone }}</td>
<td>{{ student.address }}</td>
<td>
<button @click="edit(student)" type="button" class="btn btn-success btn-sm">
<i class="fa fa-edit"></i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
<!--my modal-->
<student-modal
:edit="editMode"
/>
</div>
</template>
<script>
import ModalComponent from './partial/ModalComponent';
export default {
components:{
"student-modal" : ModalComponent,
},
data() {
return {
editMode : false,
form: new Form({
id: '',
name: '',
email: '',
phone: '',
address: '',
})
}
},
mounted() {
this.getStudents();
},
methods : {
getStudents() {
this.$Progress.start();
axios
.get('/api/students?page=' + this.pagination.current_page)
.then(response => {
this.students = response.data.data;
this.pagination = response.data.meta;
this.$Progress.finish();
})
.catch(e => {
console.log(e);
this.$Progress.fail();
})
},
create() {
this.editMode = false;
$('#createStudent').modal('show');
this.clearForm();
},
store() {
this.$Progress.start();
this.form.busy = true;
this.form.post('/api/students')
.then(response => {
this.getStudents();
//$('#createStudent').modal('hide');
if (this.form.successful) {
this.clearForm();
this.$Progress.finish();
this.$snotify.success('Student create successfully', 'Success')
} else {
this.$Progress.fail();
this.$snotify.error('Something wrong, try again.!', 'Error')
}
})
.catch(e => {
this.$Progress.fail();
console.log(e)
})
},
edit(student) {
this.editMode = true;
this.clearForm();
this.form.fill(student);
$('#createStudent').modal('show');
},
update() {
this.$Progress.start();
this.form.busy = true;
this.form.patch('/api/students/' + this.form.id)
.then(response => {
this.getStudents();
$('#createStudent').modal('hide');
if (this.form.successful) {
this.clearForm();
this.$Progress.finish();
this.$snotify.success('Student update successfully', 'Success')
} else {
this.$Progress.fail();
this.$snotify.error('Something wrong, try again.!', 'Error')
}
})
.catch(e => {
this.$Progress.fail();
console.log(e)
})
},
}
}
</script>
ModalComponent (Child) I'm using this same modal for creating and update
<template>
<div id="studentModal">
<div class="modal fade" id="createStudent" tabindex="-1" role="dialog" aria-
labelledby="studentModalTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="studentModalTitle">{{ edit ? 'Update' : 'Create' }}
Student</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form @submit.prevent="edit ? update() : store()" @keydown="form.onKeydown($event)">
<div class="modal-body">
<alert-error :form="form"/>
<div class="form-group">
<label for="name" class="font-weight-bold">Name</label>
<input type="text" v-model="form.name" class="form-control" :class="{
'is-invalid': form.errors.has('name') }" id="name" name="name"
placeholder="Your Name">
<has-error :form="form" field="name"></has-error>
</div>
<div class="form-group">
<label for="email" class="font-weight-bold">Email</label>
<input type="email" v-model="form.email" class="form-control" :class="{
'is-invalid': form.errors.has('email') }" name="email" id="email"
placeholder="Email hare">
<has-error :form="form" field="email"></has-error>
</div>
<div class="form-group">
<label for="phone" class="font-weight-bold">Phone</label>
<input type="tel" v-model="form.phone" class="form-control" :class="{
'is-invalid': form.errors.has('phone') }" name="phone" id="phone"
placeholder="Phone number">
<has-error :form="form" field="phone"></has-error>
</div>
<div class="form-group">
<label for="address" class="font-weight-bold">Address</label>
<textarea class="form-control" v-model="form.address" :class="{ 'is-
invalid': form.errors.has('address') }" name="address" id="address"
placeholder="Your address"></textarea>
<has-error :form="form" field="address"></has-error>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-
dismiss="modal">Close</button>
<button :disabled="form.busy" type="submit" class="btn btn-primary">
{{ edit ? 'Update' : 'Create' }}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "ModalComponent",
props : {
edit : Boolean,
},
data() {
return {
form: new Form({
id: '',
name: '',
email: '',
phone: '',
address: '',
})
}
}
}
</script>
How can I create and update this data to parent ?