## 刪除功能
~~~
<td><button @click="delBook(index)">刪除</button></td>
~~~
~~~
methods:{
delBook:function(idx){//刪除當前這本書
this.books.splice(idx,1);
}
}
~~~
## 添加新書
js
~~~
<script>
var vm = new Vue({
el: '#app',
data: {
newBook:{
id:0,
name:'',
author:'',
price:'',
tag:''
}
},
methods:{
addBook:function(){//添加新書
var maxId=0;
for(var i=0;i<this.books.length;i++){
if(maxId<this.books[i].id){
maxId=this.books[i].id;
}
}
this.newBook.id=maxId+1;
this.books.push(this.newBook);
this.newBook={};
}
}
})
</script>
~~~
html
~~~
<div class="add">
<h2>添加新書</h2>
<div class="form-group">
<p>書名:<input type="text" v-model="newBook.name" /></p>
<p>作者:<input type="text" v-model="newBook.author"/></p>
<p>價格:<input type="text" v-model="newBook.price"/></p>
<p>標簽:<input type="text" v-model="newBook.tag"/></p>
<p><button @click="addBook">添加</button></p>
</div>
</div>
~~~
## 完整代碼
>[info]附完整代碼
~~~
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>前端研習社-圖書管理系統</title>
<style>
h1{
text-align: center;
}
table,
td,
th {
border-collapse: collapse;
border-spacing: 0
}
table {
width: 600px;
margin: 0 auto;
text-align: center;
}
td,
th {
border: 1px solid #bcbcbc;
padding: 5px 10px;
}
th {
background: #42b983;
font-size: 1.2rem;
font-weight: 400;
color: #fff;
cursor: pointer;
}
tr:nth-of-type(odd) {
background: #fff;
}
tr:nth-of-type(even) {
background: #eee;
}
.add{
width: 400px;
padding:10px 50px;
margin: 10px auto;
background: #ccc;
border-radius: 5px;
}
h2{
text-align: center;
}
p{
height: 50px;
line-height: 50px;
}
input{
width: 300px;
height: 50px;
line-height: 50px;
font-size:20px;
padding-left:10px ;
}
p button{
float: right;
width: 100px;
height: 50px;
line-height: 50px;
font-size:20px;
border-radius: 5px;
}
</style>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<h1>圖書管理系統</h1>
<table>
<thead>
<tr>
<th>序號</th>
<th>書名</th>
<th>作者</th>
<th>價格</th>
<th>標簽</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="book in books">
<td>{{ book.id }}</td>
<td>{{ book.name }}</td>
<td>{{ book.author }}</td>
<td>{{ book.price }}</td>
<td>{{ book.tag }}</td>
<td><button @click="delBook(index)">刪除</button></td>
</tr>
</tbody>
</table>
<div class="add">
<h2>添加新書</h2>
<div class="form-group">
<p>書名:<input type="text" v-model="newBook.name" /></p>
<p>作者:<input type="text" v-model="newBook.author"/></p>
<p>價格:<input type="text" v-model="newBook.price"/></p>
<p>標簽:<input type="text" v-model="newBook.tag"/></p>
<p><button @click="addBook">添加</button></p>
</div>
</div>
</div>
</body>
<script>
var vm = new Vue({
el: '#app',
data: {
books:[
{id:1,name:'《三國演義》',author:'羅貫中',price:'99.99',tag:'經典'},
{id:2,name:'《紅樓夢》',author:'曹雪芹',price:'88',tag:'推薦'},
{id:3,name:'《水滸傳》',author:'施耐庵',price:'77',tag:'熱銷'},
{id:4,name:'《西游記》',author:'吳承恩',price:'60',tag:'經典'}
],
newBook:{
id:0,
name:'',
author:'',
price:'',
tag:''
}
},
methods:{
addBook:function(){//添加新書
var maxId=0;
for(var i=0;i<this.books.length;i++){
if(maxId<this.books[i].id){
maxId=this.books[i].id;
}
}
this.newBook.id=maxId+1;
this.books.push(this.newBook);
this.newBook={};
},
delBook:function(idx){//刪除當前這本書
this.books.splice(idx,1);
}
}
})
</script>
</html>
~~~
- 前端新手村
- 前言
- 第1章 遇見Vue.js
- 第一個Vue.js程序
- vue嘗鮮
- 第2章 概念理解
- 漸進式框架
- 虛擬DOM
- MVVM模式
- MVX模式是什么
- 第3章 Vue基礎概覽
- 第4章 Vue內置指令詳解
- vue-text
- vue-html
- v-show
- v-if
- v-else
- v-else-if
- v-for
- v-on
- v-bind
- v-model
- v-pre
- v-cloak
- v-once
- 第5章 基礎demo小練習
- 圖書管理系統
- 頁面布局
- 列表渲染
- 功能實現
- 基于BootStrap+Vuejs實現用戶信息表
- 功能描述
- 布局實現
- 星座判斷
- 第6章 組件
- 什么是組件
- 使用組件
- Prop
- 自定義事件
- 使用Slot分發內容
- 動態組件
- 雜項
- 第7章-過渡
- 過渡效果
- 概述
- 單元素/組件的過渡
- 初始渲染的過渡
- 多個元素的過渡
- 多個組件的過渡
- 列表過渡
- 可復用的過渡
- 動態過渡
- 過渡狀態
- 狀態動畫與watcher
- 動態狀態轉換
- 通過組件組織過渡
- Render函數
- 基礎
- createElement參數
- 使用JavaScript代替模板功能
- JSX
- 函數化組件
- 模板編譯
- 自定義指令
- 簡介
- 鉤子函數
- 鉤子函數參數
- 函數簡寫
- 對象字面量
- Vuex狀態管理
- Vuex是什么?
- Vuex的安裝
- Vuex起步
- data的替代品-State和Getter
- 測試Getter
- Action-操作的執行者
- 測試Action
- 只用Mutation修改狀態
- 測試Mutations
- Vuex的基本結構
- 子狀態和模塊
- 用服務分離外部操作
- Vue-router
- Vue-router是什么
- Vue-router安裝
- 基本用法1
- 基本用法2
- Vue-cli
- Vue中的Node.js
- Vue中的npm、cnpm
- Vue中的webpack
- 安裝
- 基本使用
- 模板
- 全局API
- Vue.extend
- Vue.nextTick
- Vue.set
- Vue.delete
- Vue.directive
- Vue.filter
- Vue.component
- Vue.use
- Vue.mixin
- Vue.compile
- 附錄
- 相關網站
- 尤雨溪
- 第10章 webpack
- webpack安裝
- webpack基本使用
- webpack命令行
- webpack配置文件
- 單頁面應用SPA
- 第1章 Vue.js簡介
- 1.1 Vue.js簡介
- 1.1.1 Vue.js是什么
- 1.1.2 為什么要用Vue.js
- 1.1.3 Vue.js的發展歷史
- 1.1.4 Vue.js與其他框架的區別
- 1.2 如何使用Vue.js
- 1.2.1 第一個Vue.js程序
- 1.2.2 Vue.js小嘗鮮
- 1.3 概念詳解
- 1.3.1 什么是漸進式框架
- 1.3.2 虛擬DOM是什么
- 1.3.3 如何理解MVVM
- 第2章 基礎概覽
- 2.1 Vue實例
- 2.1.1 構造器
- 2.1.2 屬性與方法
- 2.1.3 實例生命周期
- 2.1.4 生命周期圖示
- 2.2 模板語法
- 2.2.1 插值
- 2.2.2 指令
- 2.2.3 過濾器
- 2.2.4 縮寫
- 第3章 Class與Style的綁定
- 第4章 模板渲染
- 第5章 事件詳解
- 第6章 表單控件綁定
- 第7章 指令詳解
- 7.1 內部指令
- 7.2 自定義指令
- 7.3 指令的高級選項
- 第8章 計算屬性
- 第9章 過濾器
- 第10章 組件
- 10.1 什么是組件
- 10.2 注冊組件
- 10.3 組件選項
- 10.4 組件間的通信
- 10.5 內容分發
- 10.6 動態組件