I have this component in vue js with a property that binds value from input and i have a method to push the object to an array and i bind ive binded the array to a table so that everytime a user click the 'add' button it takes the input value and adds it to the table. When i press add it works but it also binds the value entered from the input, i need it to only push the value and to clear the input afterwards so that a fresh new details can be created.
<template>
<div>
<div id="patientDetails">
<div class="patient-add">
<div>
<label>Patient Name: </label>
<input type="text" v-model="patientActive.name"/>
</div>
<div>
<label>Patient Disease: </label>
<input type="text" v-model="patientActive.disease"/>
</div>
<div>
<button v-on:click="addPatientDetails()">Add +</button>
</div>
</div>
<table>
<thead>
<th>Name</th>
<th>Mobile</th>
</thead>
<tbody>
<tr v-for="p in patients">
<td>{{ p.name }}</td>
<td>{{ p.disease }}</td>
</tr>
</tbody>
</table>
</div>
</div>
and This is the Vue Script:
export default {
name: 'patient',
data(){
return {
patientActive:{
name: '',
disease: ''
},
patients: []
}
},
methods: {
addPatientDetails(){
var thispatient = this.patientActive;
if(this.validatePatient()){
this.patients.push(thispatient)
console.log(this.patients)
}
//this.clearPatient()
},
validatePatient: function(){
if(!this.patientActive.name){
alert('Name is Invalid or Empty')
return false
}
if(!this.patientActive.disease){
alert('Disease is Invalid or Empty')
return false
}
return true
},
clearPatient: function (){
this.patientActive.name = ''
this.patientActive.disease = ''
}
}
clearPatientfunction? It is clearing the inputs, isn't it?