1.添加
<script src="./lib/vue-2.4.0.js"></script>
<link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
<body>
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加</h3>
</div>
<div class="panel-body form-inline">
<label>
ID:<input type="text" class="form-control" v-model="id">
</label>
<label>
Name:<input type="text" class="form-control" v-model="name">
</label>
<!-- 在Vue中,使用事件綁定機制,爲元素指定處理函數的時候,如果加了小括號,就可以給函數傳參了 -->
<input type="button" value="添加" class="btn btn-primary" @click="add()">
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>ID</th><th>Name</th> <th>Ctime</th><th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list" :key="item.id">
<td>{{item.id}}</td>
<td v-text="item.name"></td>
<td>{{item.ctime}}</td>
<td><a href="">刪除</a></td>
</tr>
</tbody>
</table>
</div>
<script>
var vm=new Vue({
el:'#app',
data:{
id:"",
name:"",
keywords: '', // 搜索的關鍵字
list:[
{id:1,name:'chenchen',ctime:new Date()},
{id:2,name:'oldchen',ctime:new Date()}
]
},
methods:{
add(){ // 添加的方法
// console.log('ok')
// 分析:
// 1. 獲取到 id 和 name ,直接從 data 上面獲取
// 2. 組織出一個對象
// 3. 把這個對象,調用 數組的 相關方法,添加到 當前 data 上的 list 中
// 4. 注意:在Vue中,已經實現了數據的雙向綁定,每當我們修改了 data 中的數據,Vue會默認監聽到數據的改動,自動把最新的數據,應用到頁面上;
// 5. 當我們意識到上面的第四步的時候,就證明大家已經入門Vue了,我們更多的是在進行 VM中 Model 數據的操作,同時,在操作Model數據的時候,指定的業務邏輯操作;
let people = {id:this.id,name:this.name,ctime:new Date()}//取值
this.list.push(people)//執行
this.id=this.name=''//清空
},
}
});
</script>
2.刪除
刪除記錄的實現邏輯:點擊刪除鏈接,獲取要刪除記錄的id,然後調用數組的splice方法來移除記錄,
數組常用的循環方法比較
循環方法 | 說明 |
---|---|
forEach | 不可終止循環 |
some | 返回true終止循環 |
findIndex | 返回true可以終止循環,返回滿足條件的索引 |
filter | 過濾數組,返回過濾後的數組 |
通過數組的some方法來循環判斷
<a href="" @click.prevent="del(item.id)">刪除</a>
del(id) { // 根據Id刪除數據
// 分析:
// 1. 如何根據Id,找到要刪除這一項的索引
// 2. 如果找到索引了,直接調用 數組的 splice 方法
/* this.list.some((item, i) => {
if (item.id == id) {
this.list.splice(i, 1)
// 在 數組的 some 方法中,如果 return true,就會立即終止這個數組的後續循環
return true;
}
}) */
var index = this.list.findIndex(item => {
if (item.id == id) {
return true;
}
})
// console.log(index)
this.list.splice(index, 1)
},
效果
3.搜索
<label>
搜索名稱關鍵字:
<input type="text" class="form-control" v-model="keywords">
</label>
<tbody>
<!-- 之前, v-for 中的數據,都是直接從 data 上的list中直接渲染過來的 -->
<!-- 現在, 我們自定義了一個 search 方法,同時,把 所有的關鍵字,通過傳參的形式,傳遞給了 search 方法 -->
<!-- 在 search 方法內部,通過 執行 for 循環, 把所有符合 搜索關鍵字的數據,保存到 一個新數組中,返回 -->
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{ item.id }}</td>
<td v-text="item.name"></td>
<td>{{ item.ctime }}</td>
<td>
<a href="" @click.prevent="del(item.id)">刪除</a>
</td>
</tr>
</tbody>
search(keywords) { // 根據關鍵字,進行數據的搜索
/* var newList = []
this.list.forEach(item => {
if (item.name.indexOf(keywords) != -1) {
newList.push(item)
}
})
return newList */
// 注意: forEach some filter findIndex 這些都屬於數組的新方法,
// 都會對數組中的每一項,進行遍歷,執行相關的操作;
return this.list.filter(item => {
// if(item.name.indexOf(keywords) != -1)
// 注意 : ES6中,爲字符串提供了一個新方法,叫做 String.prototype.includes('要包含的字符串')
// 如果包含,則返回 true ,否則返回 false
// contain
if (item.name.includes(keywords)) {
return item
}
效果:
forEach some filter findIndex區別??
源碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lib/vue-2.4.0.js"></script>
<link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
<!-- 需要用到Jquery嗎??? -->
</head>
<body>
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加品牌</h3>
</div>
<div class="panel-body form-inline">
<label>
Id:
<input type="text" class="form-control" v-model="id">
</label>
<label>
Name:
<input type="text" class="form-control" v-model="name">
</label>
<!-- 在Vue中,使用事件綁定機制,爲元素指定處理函數的時候,如果加了小括號,就可以給函數傳參了 -->
<input type="button" value="添加" class="btn btn-primary" @click="add()">
<label>
搜索名稱關鍵字:
<input type="text" class="form-control" v-model="keywords">
</label>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<!-- 之前, v-for 中的數據,都是直接從 data 上的list中直接渲染過來的 -->
<!-- 現在, 我們自定義了一個 search 方法,同時,把 所有的關鍵字,通過傳參的形式,傳遞給了 search 方法 -->
<!-- 在 search 方法內部,通過 執行 for 循環, 把所有符合 搜索關鍵字的數據,保存到 一個新數組中,返回 -->
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{ item.id }}</td>
<td v-text="item.name"></td>
<td>{{ item.ctime }}</td>
<td>
<a href="" @click.prevent="del(item.id)">刪除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script>
// 創建 Vue 實例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {
id: '',
name: '',
keywords: '', // 搜索的關鍵字
list: [
{ id: 1, name: '奔馳', ctime: new Date() },
{ id: 2, name: '寶馬', ctime: new Date() }
]
},
methods: {
add() { // 添加的方法
// console.log('ok')
// 分析:
// 1. 獲取到 id 和 name ,直接從 data 上面獲取
// 2. 組織出一個對象
// 3. 把這個對象,調用 數組的 相關方法,添加到 當前 data 上的 list 中
// 4. 注意:在Vue中,已經實現了數據的雙向綁定,每當我們修改了 data 中的數據,Vue會默認監聽到數據的改動,自動把最新的數據,應用到頁面上;
// 5. 當我們意識到上面的第四步的時候,就證明大家已經入門Vue了,我們更多的是在進行 VM中 Model 數據的操作,同時,在操作Model數據的時候,指定的業務邏輯操作;
var car = { id: this.id, name: this.name, ctime: new Date() }
this.list.push(car)
this.id = this.name = ''
},
del(id) { // 根據Id刪除數據
// 分析:
// 1. 如何根據Id,找到要刪除這一項的索引
// 2. 如果找到索引了,直接調用 數組的 splice 方法
/* this.list.some((item, i) => {
if (item.id == id) {
this.list.splice(i, 1)
// 在 數組的 some 方法中,如果 return true,就會立即終止這個數組的後續循環
return true;
}
}) */
var index = this.list.findIndex(item => {
if (item.id == id) {
return true;
}
})
// console.log(index)
this.list.splice(index, 1)
},
search(keywords) { // 根據關鍵字,進行數據的搜索
/* var newList = []
this.list.forEach(item => {
if (item.name.indexOf(keywords) != -1) {
newList.push(item)
}
})
return newList */
// 注意: forEach some filter findIndex 這些都屬於數組的新方法,
// 都會對數組中的每一項,進行遍歷,執行相關的操作;
return this.list.filter(item => {
// if(item.name.indexOf(keywords) != -1)
// 注意 : ES6中,爲字符串提供了一個新方法,叫做 String.prototype.includes('要包含的字符串')
// 如果包含,則返回 true ,否則返回 false
// contain
if (item.name.includes(keywords)) {
return item
}
})
// return newList
}
}
});
</script>
</body>
</html>