vue 搜索历史

搜索后保存搜索历史,点击搜索历史再去搜索

<!-- searchBox. -->  
<div class="search_row">
   <div class="search-box">
       <i class='iconfont icon-sousuo'></i>
       <input type="text" 
         v-model="inputMsg" 
         ref='input' class='box' 
         :placeholder='placeholder'>
       <i @click='clearMsg' v-show='inputMsg' class='iconfont icon-quxiao'></i>
   </div>
   <button @click="searchAll()">搜索</button>
 </div>
<!-- 历史记录 -->
<div class="search_history">
   <div class="top">
    <h3>历史记录</h3>
    <i class="iconfont icon-delete" @click="deleteHistory"></i>
  </div>
  <ul class="search_list" >
     <li v-for="(item,index) in historyList" :key="index" @click="hotSearch(item)">{{item}}        
     </li>
   </ul>
  </div>

 <script>
        var vm = new Vue({
            el: '#app',
            data: {
                inputMsg:'',
                placeholder:'搜索关键词...',
                shopList:[1],//店铺列表
                commodityList:[],//商品列表
                historyList:[]//搜索历史列表
            },
            created:function(){
                var that = this;
                //获取localstorage
                let storage=window.localStorage;
                if(storage.getItem('searchWord') !== null){
                    that.historyList = JSON.parse(storage.getItem('searchWord'));
                }
                
                // that.historyWord = JSON.parse(localStorage.getItem("searchWord")||'[]');
                // that.historyList=localStorage.getItem('historyList'||'[]');
                // that.historyList = that.historyWord.split(',');  //将字符串转成数组

                // that.historyList = that.historyWord;
                console.log(that.historyList,'that.historyList')
            },
            methods: {
                clearMsg(){
                    vm.inputMsg = '';
                },
                
                //清除历史记录
                deleteHistory(){
                    vm.historyList.splice(0);
                    localStorage.clear();
                    console.log(vm.historyList,'deleteHistory');
                },
                //搜索
                searchAll:function(){
                    console.log('sousosu');
                    var that = vm;
                    var val = that.inputMsg.trim();
                    if(val != ''){
                        let storage = window.localStorage;
                        if(storage.getItem('searchWord') == null){
                            that.historyList.unshift(val);
                            storage.setItem('searchWord',JSON.stringify(that.historyList));
                        }else{
                            console.log(that.historyList,'historyList');
                            if(that.historyList.indexOf(val) != -1){
                                that.historyList.splice(that.historyList.indexOf(val), 1);
                                that.historyList.unshift(val);
                            }else{
                                that.historyList.unshift(val);
                            }

                            if(that.historyList.length > 5){
                                that.historyList.pop();
                            }
                            storage.setItem('searchWord',JSON.stringify(that.historyList));
                        }
                    }
                    //2
                    // if (that.historyList.length > 0) { // 有数据的话 判断
                    //         if (that.historyList.indexOf(val) !== -1) { // 有相同的,先删除 再添加 
                    //         that.historyList.splice(that.historyList.indexOf(val), 1)
                    //         that.historyList.unshift(val)
                    //         } else { // 没有相同的 添加
                    //         that.historyList.unshift(val)
                    //         }
                    //     } else { // 没有数据 添加
                    //         that.historyList.unshift(val)
                    //     }
                    //     if (that.historyList.length > 6) { // 保留六个值
                    //         that.historyList.pop()
                    //     }
                    //     localStorage.setItem('searchWord', JSON.stringify(that.historyList));
                    //3  

                },
                hotSearch:function(item){
                    vm.inputMsg=item;
                    vm.searchAll();
                },
                
            },
        });
    </script>

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Vue表单的历史记录搜索,你可以使用localStorage来存储历史搜索记录,然后在表单中展示这些记录。以下是一个简单的实现示例: 1. 在Vue组件的data中添加一个history数组,用于存储历史搜索记录。 ``` data() { return { history: [] }; } ``` 2. 在表单中添加一个输入框和一个按钮,用于进行搜索。在输入框中添加一个v-model指令,将输入框的值绑定到组件的searchText属性上。按钮的点击事件调用search方法,将搜索内容存储到history数组中。 ``` <template> <div> <input type="text" v-model="searchText" /> <button @click="search">Search</button> </div> </template> <script> export default { data() { return { history: [], searchText: '' }; }, methods: { search() { if (this.searchText) { this.history.push(this.searchText); localStorage.setItem('history', JSON.stringify(this.history)); // perform search } } } }; </script> ``` 3. 在组件的created钩子中,读取localStorage中的历史记录,并将其赋值给history数组。 ``` created() { const history = localStorage.getItem('history'); if (history) { this.history = JSON.parse(history); } } ``` 4. 在表单下方添加一个历史记录列表,展示历史搜索记录。使用v-for指令遍历history数组,将每个搜索记录展示为一个列表项。为了方便用户点击历史记录进行搜索,给每个列表项添加一个@click事件,将点击的记录赋值给searchText属性,并调用search方法进行搜索。 ``` <template> <div> <input type="text" v-model="searchText" /> <button @click="search">Search</button> <ul> <li v-for="record in history" @click="searchText = record; search">{{ record }}</li> </ul> </div> </template> ``` 这样,就可以实现Vue表单的历史记录搜索了。每次进行搜索时,都会将搜索内容存储到localStorage中,下次打开页面时,历史记录会自动加载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值