這篇文章給大家分享的是有關(guān)Vuejs怎么實現(xiàn)搜索匹配功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

最近一直在看vue,查了很多資料,看了很多文檔和博客,大概半知半解了,然后利用所理解的知識寫了一個簡單的搜索匹配功能。
大概長這個樣子:

數(shù)據(jù)都是假的
代碼部分
(注意我引用的是本地vue.min.js文件,請注意文件路徑。)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue測試2</title>
<script type="text/javascript" src="vue.min.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
font-size: 14px;
font-family: "微軟雅黑";
}
#box{
width: 500px;
height: auto;
border: 1px solid #ccc;
margin: 50px auto;
padding: 10px;
}
.search{
width: 480px;
height: 100px;
text-align: center;
}
.searchBox{
width: 230px;
height: 40px;
outline: none;
text-indent: 10px;
margin-right: 20px;
}
.btn{
width: 100px;
height: 50px;
cursor: pointer;
font-size: 18px;
}
.goodsheet{
width: 500px;
height: auto;
border: 1px solid #eee;
}
.goodsheet tr td,
.goodsheet tr th{
width: 33%;
border: 1px solid #eee;
padding: 5px 10px;
text-align: left;
}
.goodsheet tr th span{
background: #ff9900;
padding: 0 6px;
color: #fff;
cursor: pointer;
}
</style>
</head>
<body>
<p id="box">
<p class="search">
<input type="text" class="searchBox" v-model="searchVal">
<button class="btn">搜 索</button>
</p>
<table class="goodsheet">
<tr>
<th>商品名</th>
<th>單價
<span @click="orderFn('price', false)">↑</span>
<span @click="orderFn('price', true)">↓</span>
</th>
<th>銷量
<span @click="orderFn('sales', false)">↑</span>
<span @click="orderFn('sales', true)">↓</span>
</th>
</tr>
<tr v-for='(item, key) in list'>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td>{{item.sales}}萬</td>
</tr>
</table>
</p>
<script type="text/javascript">
var myVueTest = new Vue({
el:'#box',
data:{
goodsList:[
//假數(shù)據(jù)
{name:"三星Galaxy Note8",price:5200,sales:2.6},
{name:"iphone5s",price:2500,sales:2.2},
{name:"iphone6",price:2800,sales:1.6},
{name:"iphone6s",price:3200,sales:2.9},
{name:"iphone7",price:3800,sales:12.6},
{name:"iphone7plus",price:4200,sales:2.1},
{name:"iphone8",price:5500,sales:10.6},
{name:"華為",price:4600,sales:7.6},
{name:"小米",price:1200,sales:32.6},
{name:"OPPOR11",price:3000,sales:1.2},
{name:"vivoX20",price:3250,sales:2.9}
],
searchVal:'', //默認輸入為空
letter:'', //默認不排序
original:false //默認從小到大排列
},
methods:{
orderFn(letter,original){
this.letter = letter; //排序字段 price or sales
this.original = original; //排序方式 up or down
}
},
//通過計算屬性過濾數(shù)據(jù)
computed:{
list: function(){
var _this = this;
//邏輯-->根據(jù)input的value值篩選goodsList中的數(shù)據(jù)
var arrByZM = [];//聲明一個空數(shù)組來存放數(shù)據(jù)
for (var i=0;i<this.goodsList.length;i++){
//for循環(huán)數(shù)據(jù)中的每一項(根據(jù)name值)
if(this.goodsList[i].name.search(this.searchVal) != -1){
//判斷輸入框中的值是否可以匹配到數(shù)據(jù),如果匹配成功
arrByZM.push(this.goodsList[i]);
//向空數(shù)組中添加數(shù)據(jù)
}
}
//邏輯-->升序降序排列 false: 默認從小到大 true:默認從大到小
//判斷,如果要letter不為空,說明要進行排序
if(this.letter != ''){
arrByZM.sort(function( a , b){
if(_this.original){
return b[_this.letter] - a[_this.letter];
}else{
return a[_this.letter] - b[_this.letter];
}
});
}
//一定要記得返回篩選后的數(shù)據(jù)
return arrByZM;
}
}
});
</script>
</body>
</html>其實核心算法還是用原生JS寫的,vue提供了很強大的數(shù)據(jù)綁定方法,但是如果只知道vue這個框架,沒有自己的核心思想的話,還是沒多大用處的,所以作者在官方文檔里面說希望我們是有一定JS基礎(chǔ)的。我還是覺得前端框架再多,最厲害的永遠是原生JS。
感謝各位的閱讀!關(guān)于Vuejs怎么實現(xiàn)搜索匹配功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
當前文章:Vuejs怎么實現(xiàn)搜索匹配功能-創(chuàng)新互聯(lián)
鏈接URL:http://chinadenli.net/article38/shpsp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站設計、企業(yè)建站、網(wǎng)站設計公司、網(wǎng)站維護、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容