【vue學習】vue 組件 實例 自動提示的搜索框


基於element-ui

輸入內容,會自動從服務器獲取建議,然后顯示

 

 

 

 

主要有兩個個方法

querySearchAsync,傳入查詢參數和一個回調函數,根據查詢參數,返回一個數組,value表示提示列表顯示的信息,還可以放入對象

handleSelect,當提示條目被選中時 觸發,會傳入,提示的對象數組,可以取出之前放入的對象,傳遞對象而不是對象id,可以減少查詢次數

 1 <template>
 2   <div class="wrapper">
 3     <el-autocomplete size="small"
 4                      v-model="msg"
 5                      :fetch-suggestions="querySearchAsync"
 6                      placeholder="請輸入內容..."
 7                      @select="handleSelect">
 8       <i slot="suffix" class="el-input__icon el-icon-close" @click="msg=''"></i>
 9     </el-autocomplete>
10   </div>
11 </template>
12  
13 <script>
14   import Vue from 'vue'
15   import {Button, Select, Input, Autocomplete} from 'element-ui'
16   import 'element-ui/lib/theme-chalk/index.css'
17   import {get_hot, search_movie} from "../api/movie_api";
18  
19   Vue.component(Button.name, Button)
20   Vue.component(Select.name, Select)
21   Vue.component(Input.name, Input)
22   Vue.component(Autocomplete.name, Autocomplete)
23  
24   export default {
25     name: "search",
26     data() {
27       return {
28         movie_list: [],
29         msg: '',
30       };
31     },
32     components: {},
33     methods: {
34       clear() {
35         console.log('clear')
36       },
37       async loadAll() {
38         let list = await get_hot()
39         // console.log('hot list', list)
40         return list
41       },
42       async querySearchAsync(queryString, cb) {
43  
44         if (this.msg.length == 0) {
45           this.movie_list = await get_hot()
46         } else {
47           this.movie_list = await search_movie(this.msg)
48         }
49         let results = []
50         for (let i of  this.movie_list) {
51           // console.log(i)
52           results.push({
53             value: i['alt_title'],
54             movie_id: i['id'],
55           })
56         }
57         cb(results)
58       },
59  
60       handleSelect(item) {
61         // console.log('點擊的電影是', item['movie_id']);
62         this.$emit('change_movie_id', item['movie_id'])
63       }
64     },
65     mounted() {
66       this.movie_list = this.loadAll();
67     }
68   }
69 </script>
70  
71 <style scoped>
72  
73   .wrapper {
74     display: flex;
75     justify-content: center;
76     /*margin: 5px;*/
77   }
78  
79 </style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM