vue實現實時搜索顯示功能
更新時間:2022年04月18日 11:24:55 作者:Amnesia?
這篇文章主要為大家詳細介紹了vue實現實時搜索顯示功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現實時搜索顯示的具體代碼,供大家參考,具體內容如下
<template> //綁定搜索的關鍵字 <input type="text" class="form-control" placeholder="搜索" v-model="filterInput"/> ?<table ?class="table table-striped"> ? ? ? <thead> ? ? ? ? <tr> ? ? ? ? ? <th>姓名</th> ? ? ? ? ? <th>電話</th> ? ? ? ? ? <th>郵箱</th> ? ? ? ? ? <th></th> ? ? ? ? </tr> ? ? ? </thead> ? ? ? <tbody> ? ? ? ? <!-- 遍歷搜索的東西,觸發(fā)filterBy方法遍歷的時候和搜索框內容進行匹配 例如name--> ? ? ? ? <!-- 如果不搜索,遍歷的就是所有數據 --> ? ? ? ? <tr v-for="(item,index) in filterBy(customer,filterInput)" :key="index"> ? ? ? ? ? <td>{{item.name}}</td> ? ? ? ? ? <td>{{item.phone}}</td> ? ? ? ? ? <td>{{item.email}}</td> ? ? ? ? ? <!-- 通過對應的id查看詳情 ?拼接id--> ? ? ? ? ? <!-- 在details中通過攜帶id發(fā)送后臺請求數據:to是因為to現在的值是變量要綁定,如果是單純的字符串就不需要綁定--> ? ? ? ? ? <td> ? ? ? ? ? ? <!-- <router-link class="btn btn-default" :to="'/customer/'+item[index]._id" style="backgroundcolor:blue ">查看詳情</router-link> --> ? ? ? ? ? ? <!-- <router-link class="btn btn-default" :to="'/customer/'+item._id" style="backgroundcolor:blue " >查看詳情</router-link> --> ? ? ? ? ? ? <div class="btn btn-default" style="backgroundcolor:blue" @click="handleclick(item)">查看詳情</div> ? ? ? ? ? </td> ? ? ? ? </tr> ? ? ? </tbody> ? ? </table> ? ?? </template> <script> export default { ? name: "customers", ? data() { ? ? return { ? ? ? customer: [], ? ? ? filterInput:"", ? ? ? childrenmag:'' ? ? }; ? }, ? ? methods: { ? ? // 異步請求數據 ? ? async fetchCustomers() { ? ? ? const res = await this.$http.get("/users"); ? ? ? this.customer = res.data; ? ? }, ? ? filterBy(customers, inputvalue) { ? ? ? // filter方法遍歷整個數組 ? ? ? return customers.filter(customer => { ? ? ? ? // 注意match不能遍里數字,true,false ? ? ? ? return customer.name.match(inputvalue); ? ? ? }); ? ? } ? ? } </script>
filterBy方法查找對應關鍵字的那條數據。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
element-ui中el-cascader動態(tài)加載和默認值詳解
vue+elementUI項目中el-cascader級聯選擇器使用頻率非常高,下面這篇文章主要給大家介紹了關于element-ui中el-cascader動態(tài)加載和默認值的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05教你如何開發(fā)Vite3插件構建Electron開發(fā)環(huán)境
這篇文章主要介紹了如何開發(fā)Vite3插件構建Electron開發(fā)環(huán)境,文中給大家提到了如何讓 Vite 加載 Electron 的內置模塊和 Node.js 的內置模塊,需要的朋友可以參考下2022-11-11