JSFiddle - React, Tailwind, and code Playground
HTML
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/0.1.17/vue-resource.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.7/vue.js"></script>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<h1>Vue demo</h1>
<div id="app">
<table class="table table-hover" v-cloak>
<thead>
<tr>
<th class="text-right" @click="sortBy('id')">序号</th>
<th class="text-right" @click="sortBy('name')">名</th>
<th class="text-right" @click="sortBy('author')">作者</th>
<th class="text-right" @click="sortBy('price')">价格</th>
<th class="text-right">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="book in books | orderBy sortparam">
<td class="text-right">{{book.id}}</td>
<td class="text-right">{{book.name}}</td>
<td class="text-right">{{book.author}}</td>
<td class="text-right">{{book.price}}</td>
<template v-if="book.id%2==0">
<td class="text-right">
<button type="button" class="btn btn-success" @click="delBook(book)">删除</button>
</td>
</template>
<template v-else>
<td class="text-right">
<button type="button" class="btn btn-danger" @click="delBook(book)">删除</button>
</td>
</template>
</tr>
...
CSS
[v-cloak]{
display: none;
}
JavaScript
new Vue({
el: '#app',
ready: function() {
this.$http.get('https://api.myjson.com/bins/r8mm', function(data) {
this.$set('books', data);
}).error(function(data, status, request) {
console.log('fail' + status + "," + request);
})
},
data: {
sortparam: '',
book: {
id: 0,
author: '',
name: '',
price: ''
},
books: ''
},
computed: {
sum: function() {
var result = 0;
for (var i = 0; i < this.books.length; i++) {
result = Number(this.books[i].price) + result;
};
return result;
}
},
methods: {
addBook: function() {
this.book.id = this.books.length + 1;
this.books.push(this.book);
this.book = '';
},
delBook: function(book) {
this.books.$remove(book);
},
sortBy: function(sortparam) {
this.sortparam = sortparam;
}
}
});