vue搜索功能實現(vue+element)


template的代碼:

//搜索框
  <el-input placeholder="請輸入..." v-model="keyword" ></el-input>
  <button @click="search">搜索</button>
//內容
<div v-for=(item,index) in list :key=index>{{item.title}}</div>
<div v-if="isShowTip">沒有搜索到匹配結果</div>

data初始化:

  data() {
    return {
      keyword: "",
      list: [],
      searchList: [],
      isShowTip: false,
    };
  },

methods:

  search() {
      this.isShowTip = false;
      if (this.keyword == "") {
        this.$toast("請輸入相關內容搜索");
        return;
      }
      this.$axios.get( "../../json/getAll.json")
        .then((res) => {

this.list= res.data.data; }) .then(() => { this.searchList = []; this.list.forEach((item) => { if (item.title.indexOf(this.keyword) > -1) { this.searchList.push(item); } }); if (this.searchList.length == 0) { this.isShowTip = true; } this.searchList.map((item) => { item.title= this.brightKeyword(item.title); }); }); }, brightKeyword(val) { let keyword = this.keyword; if (val.indexOf(keyword) !== -1) { return val.replace(keyword, `${keyword}`); } else { return val; } },

 


免責聲明!

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



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