Skip to content

Instantly share code, notes, and snippets.

@s-shin
Created June 9, 2014 16:09
Show Gist options
  • Select an option

  • Save s-shin/a39c9208b52f9c271664 to your computer and use it in GitHub Desktop.

Select an option

Save s-shin/a39c9208b52f9c271664 to your computer and use it in GitHub Desktop.
My first practice of vue.js
<!DOCTYPE html>
<html>
<head>
<title>Vue.js Test</title>
<style>
.btn {
background-color: #333;
color: #FFF;
border-radius: 3px;
cursor: pointer;
}
.btn:hover {
background-color: #666;
}
</style>
</head>
<body>
<form id="outline" action="">
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-repeat="section: sections">
<td>{{section.id}}<input name="sections_ids[]" type="hidden" value="{{section.id}}" /></td>
<td><input name="sections_title[]" type="text" value="{{section.title}}" /></td>
<td>
<span class="btn" v-on="click: deleteSection($event, section)">delete</span>
</td>
</tr>
</tbody>
</table>
<div>
<span class="btn" v-on="click: addSection">add</span>
</div>
<button>Submit</button>
</form>
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.10.5/vue.min.js"></script>
<script>
(function() {
new Vue({
el: "#outline",
data: {
id: 0,
sections: []
},
created: function() {
this.sections.push(
{
id: ++this.id,
title: "Introduction"
},
{
id: ++this.id,
title: "Background"
}
);
},
methods: {
addSection: function(e) {
e.preventDefault();
this.sections.push({
id: ++this.id,
title: ""
});
},
deleteSection: function(e, section) {
this.sections.$remove(section);
this.id = this.sections[this.sections.length-1].id;
}
}
});
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment