vue 簡單自動(dòng)補(bǔ)全的輸入框的示例
實(shí)現(xiàn)一個(gè)輸入框,輸入信息后顯示由后臺(tái)返回的數(shù)據(jù),供用戶選擇,之前用的elm的組件,不過那個(gè)有點(diǎn)大。。。簡單的情況下自己實(shí)現(xiàn)一個(gè)也能滿足要求。。。應(yīng)該吧。。。
主題包括一個(gè)input用于輸入,一個(gè)div用于展示數(shù)據(jù),div里面是數(shù)據(jù)項(xiàng)item
當(dāng)在input中按下回車時(shí),會(huì)根據(jù)信息去后臺(tái)獲取數(shù)據(jù),如果用戶點(diǎn)擊了別的地方,input失去焦點(diǎn),則提示的div也應(yīng)該收起來
bug:
在blur事件中,如果直接將isShow設(shè)置為false會(huì)出問題,先失去焦點(diǎn),顯示面板消失,所以你的點(diǎn)擊不會(huì)被監(jiān)聽到。。。設(shè)置一個(gè)計(jì)時(shí)器,在點(diǎn)擊之后10ms后將面板收起來,問題解決。。。
顯示div將內(nèi)容撐開,改變其他組件布局,設(shè)置div的屬性,即可,高度設(shè)為0,z-index很大,就不會(huì)改變其他組件位置
height: 0; z-index: 999;
<template> <div class="container"> <input v-model="msg" @keyup.enter="search" class="msg" @blur="blur"/> <div class="select-panel"> <div v-show="isShow" v-for="w in words" class="select-item" @click="click_item(w)">{{w['content']}}</div> </div> </div> </template>
簡單實(shí)現(xiàn)代碼
<template> <div class="container"> <input v-model="msg" @keyup.enter="search" class="msg" @blur="blur"/> <div class="select-panel"> <div v-show="isShow" v-for="w in words" class="select-item" @click="click_item(w)">{{w['content']}}</div> </div> </div> </template> <script> import {search_word} from "../api/word-api"; export default { name: "auto-complete", data() { return { msg: '', words: [], isShow: false } }, computed: {}, methods: { blur() { setTimeout(() => { this.isShow = false }, 200) }, async search() { console.log('search msg', this.msg) this.words = await search_word(this.msg) console.log(this.words) this.isShow = true }, click_item(w) { console.log('click word', w) this.$emit('add_word', w) } }, } </script> <style scoped> .container { display: flex; flex-grow: 0; flex-shrink: 0; box-sizing: border-box; } .msg { margin: 5px; } .select-panel { height: 0; z-index: 999; } .select-item { /*height: 0;*/ z-index: 999; margin: 1px; padding: 2px; background: #fff; opacity: 0.8; } </style>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)Input輸入框模糊查詢方法
- vue input輸入框模糊查詢的示例代碼
- vue element-ui實(shí)現(xiàn)input輸入框金額數(shù)字添加千分位
- vue實(shí)現(xiàn)驗(yàn)證碼輸入框組件
- vue.js 實(shí)現(xiàn)輸入框動(dòng)態(tài)添加功能
- vue中使用iview自定義驗(yàn)證關(guān)鍵詞輸入框問題及解決方法
- Vue 實(shí)現(xiàn)輸入框新增搜索歷史記錄功能
- vue表單驗(yàn)證之禁止input輸入框輸入空格
- VUE.js實(shí)現(xiàn)動(dòng)態(tài)設(shè)置輸入框disabled屬性
- vue自定義數(shù)字輸入框組件
相關(guān)文章
vue實(shí)現(xiàn)用v-bind給src和href賦值
這篇文章主要介紹了vue實(shí)現(xiàn)用v-bind給src和href賦值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue2中pinia刷新后數(shù)據(jù)丟失的問題解決
Pinia是一個(gè)Vue.js狀態(tài)管理庫,如果你在組件中修改了store中的數(shù)據(jù)并刷新了界面,Pinia會(huì)將store中的數(shù)據(jù)重置為初始值,從而導(dǎo)致數(shù)據(jù)丟失的問題,本文就來介紹一下問題解決,感興趣的可以了解一下2023-12-12vue全局實(shí)現(xiàn)數(shù)字千位分隔符格式
這篇文章主要為大家詳細(xì)介紹了vue全局實(shí)現(xiàn)數(shù)字千位分隔符格式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Vue報(bào)錯(cuò):TypeError:Cannot create property '
這篇文章主要介紹了Vue報(bào)錯(cuò):TypeError:Cannot create property 'xxx' on string 'xxxx'問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08vue在IIS服務(wù)器部署后路由無法跳轉(zhuǎn)
在IIS服務(wù)器上部署Vue項(xiàng)目時(shí),可能會(huì)遇到路由無法正常跳轉(zhuǎn)的問題,解決方法有兩種,下面就來具體介紹一下解決方法,感興趣的可以了解一下2024-10-10vue跨域問題:Access?to?XMLHttpRequest?at‘httplocalhost解決
在前端發(fā)出Ajax請(qǐng)求的時(shí)候,有時(shí)候會(huì)產(chǎn)生跨域問題,下面這篇文章主要給大家介紹了關(guān)于vue跨域問題:Access?to?XMLHttpRequest?at‘httplocalhost的解決辦法,需要的朋友可以參考下2023-01-01Vue服務(wù)端渲染和Vue瀏覽器端渲染的性能對(duì)比(實(shí)例PK )
這篇文章主要介紹了Vue服務(wù)端渲染和Vue瀏覽器端渲染的性能對(duì)比(實(shí)例PK ),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Vue3處理錯(cuò)誤邊界(error boundaries)的示例代碼
在開發(fā) Vue 3 應(yīng)用時(shí),處理錯(cuò)誤邊界(Error Boundaries)是一個(gè)重要的考量,在 Vue 3 中實(shí)現(xiàn)錯(cuò)誤邊界的方式與 React 等其他框架有所不同,下面,我們將深入探討 Vue 3 中如何實(shí)現(xiàn)錯(cuò)誤邊界,并提供一些示例代碼幫助理解什么是錯(cuò)誤邊界,需要的朋友可以參考下2024-10-10