I have this pretty simple Vue js page as below:
<html>
<head>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="main-container">
<div id="contact-table">
<table>
<thead>
<tr>
<th>Contact Name</th>
<th>Email</th>
</tr>
</thead>
<tbody v-for="contact in contacts">
<tr v-on:click="selectContact(contact.index)">
<td>
{{contact.name}}
</td>
<td>
{{contact.email}}
</td>
</tr>
</tbody>
</table>
</div>
<div id="contact-form">
<input type="text" v-model="contactName"/>
<input type="text" v-model="contactEmail"/>
<button type="button" v-on:click="updateContact">Update Contact</button>
</div>
</div>
</body>
<script>
var hub = {
contacts: [
{
name: "James",
email: "Jame@bond.com",
index: 0
},
{
name: "Mary",
email: "Mary@lamb.com",
index: 1
}
],
index: 0
};
var contactTable = new Vue({
el: "#contact-table",
data: {
contacts: hub.contacts
},
methods: {
selectContact: function(index) {
hub.index = index;
console.log(hub.index);
}
}
});
var contactForm = new Vue({
el: "#contact-form",
data: {
contactName: hub.contacts[hub.index].name,
contactEmail: hub.contacts[hub.index].email
},
methods: {
updateContact: function() {
hub.contacts[hub.index].name = this.contactName;
hub.contacts[hub.index].email = this.contactEmail;
}
}
});
</script>
</html>
Basically it contains two Vue parts - the table and the input form. By clicking the row in the table it should change the value in the input form and but clicking the Update contact button it should update the form details.
The second part is working well - however, when I clicked the table's row, although console.log tells me the hub.index is well updated, but the view in the input form is not.
I believe this should be a two-way binding here so I am just wondering where did I do wrong here.