<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.wrapper {
width: 800px;
margin: 20px auto;
}
.operation {
margin-bottom: 10px;
text-align: center;
line-height: 20px;
font-size: 18px;
}
.operation input {
padding: 5px;
border: 1px solid deepskyblue;
}
.operation button {
border-radius: 3px;
background-color: deepskyblue;
}
.search {
text-align: left;
line-height: 20px;
font-size: 18px;
}
.search input {
padding: 5px;
border: 1px solid deeppink;
}
#tb{
width: 800px;
border-collapse: collapse;
margin: 20px auto;
}
#tb th{
background-color: #0094ff;
color:white;
font-size: 16px;
padding: 5px;
text-align: center;
border: 1px solid black;
}
#tb td{
padding: 5px;
text-align: center;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="app" class="wrapper">
<div class="operation">
<input type="text" placeholder="請輸入編號" v-model="newGoods.id">
<input type="text" placeholder="請輸入名稱" v-model="newGoods.name">
<button type="button" v-on:click="addGoods">添加數(shù)據(jù)</button>
</div>
<div class="search">
<input type="text" placeHolder="請輸入篩選內(nèi)容" v-model="searchKey">
</div>
<table id="tb">
<tr>
<th>編號</th>
<th>名稱</th>
<th>創(chuàng)建時間</th>
<th>操作</th>
</tr>
<!-- 沒有數(shù)據(jù)才顯示, 有數(shù)據(jù)隱藏 -->
<tr v-if="goodsList.length == 0">
<td colspan="4">列表無數(shù)據(jù)</td>
</tr>
<!-- 渲染商品列表 -->
<!-- 現(xiàn)在給它多加一個判斷邏輯, 只有[商品名稱與搜索值相同]或者[搜索值為空]的的時候, 才渲染這個標簽 -->
<tr v-for="item in goodsList" v-if="item.name == searchKey || searchKey == ''">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.ctime | date }}</td>
<td>
<!-- @符號是v-on的簡寫方式 -->
<a href="#" @click="deleteGoods(item.id)">刪除</a>
</td>
</tr>
</table>
</div>
<script src="vue.js"></script>
<script>
// 實現(xiàn)一個處理日期的過濾器
Vue.filter('date', function(time) {
console.log(this)
var date = new Date(time);
return `${ date.getFullYear() }-${ date.getMonth() + 1 }-${ date.getDate() }`;
});
new Vue({
el: '#app',
data: {
// 商品列表
goodsList: [
{ id: 1, name: '法拉利', ctime: new Date() },
{ id: 2, name: '瑪莎拉蒂', ctime: new Date() },
{ id: 3, name: '蘭博基尼', ctime: new Date() }
],
// 新商品數(shù)據(jù)
newGoods: {
id: '',
name: ''
},
// 搜索的內(nèi)容
searchKey: ''
},
methods: {
// 點擊添加按鈕, 拿到兩個表單的值, 組成一個商品對象push到商品列表中, 視圖會自動更新
addGoods() {
// 添加商品
this.goodsList.push({
id: this.newGoods.id,
name: this.newGoods.name,
ctime: new Date()
});
// 清空表單數(shù)據(jù)
this.newGoods.id = '';
this.newGoods.name = '';
},
// 刪除商品, 需要拿到要刪除商品的id
deleteGoods(id) {
// 方式一: 遍歷商品列表依次與id比較, 找到要刪除的商品刪除
// for(var i = 0; i < this.goodsList.length; i++) {
// if(this.goodsList[i].id == id) {
// this.goodsList.splice(i, 1);
// }
// }
// 方式二: 使用filter方法實現(xiàn)
// this.goodsList = this.goodsList.filter(function(goods, i) {
// return goods.id != id; // 不刪除的商品留下
// });
this.goodsList = this.goodsList.filter(goods => goods.id != id);
},
}
});
</script>
</body>
</html>
01_23.實現(xiàn)數(shù)據(jù)篩選
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門哈误,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人躏嚎,你說我怎么就攤上這事蜜自。” “怎么了卢佣?”我有些...
- 文/不壞的土叔 我叫張陵重荠,是天一觀的道長。 經(jīng)常有香客問我虚茶,道長戈鲁,這世上最難降的妖魔是什么仇参? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮婆殿,結(jié)果婚禮上诈乒,老公的妹妹穿的比我還像新娘。我一直安慰自己婆芦,他們只是感情好怕磨,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著消约,像睡著了一般肠鲫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上或粮,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼命浴!你這毒婦竟也來了娄猫?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布始藕,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏伍派。R本人自食惡果不足惜江耀,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望诉植。 院中可真熱鬧祥国,春花似錦、人聲如沸倍踪。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽建车。三九已至扩借,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間缤至,已是汗流浹背潮罪。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 有的時候統(tǒng)計工作特別繁瑣,如果數(shù)據(jù)量少狮荔,簡單的篩選還可以完成相應(yīng)的工作胎撇。當數(shù)據(jù)非常龐大的時候,僅僅靠人工篩選很難準...