美文网首页
Vuetify 实现搜索框

Vuetify 实现搜索框

作者: 草帽lufei | 来源:发表于2020-03-05 21:52 被阅读0次
本地效果

实现思路

使用 Vuetify 中的 v-menu 组件实现,控制光标焦点,在输入框获取的焦点时弹出联想词汇菜单,支持上下按键选中内容,菜单位置,样式按需调整即可

数据获取方面通过监听输入框内容变化,调用对应接口获取数据

注意

如果产品功能要求极高的性能,要加防抖和节流处理

<template>
  <div v-on:keyup.enter="search">
    <v-menu offset-y>
      <template v-slot:activator="{ on }">
        <v-text-field
          solo
          hide-details
          label="请输入关键词"
          append-icon="search"
          v-model="text"
          class="input-search"
          autocomplete="off"
          v-on="on"
          ref="search"
        ></v-text-field>
      </template>
      <v-list v-if="items.length > 0" class="border-list" dense>
        <v-list-item v-for="(item, index) in items" :key="index" @click="itemClick(item)">
          <v-list-item-title>{{ item.name }}</v-list-item-title>
        </v-list-item>
      </v-list>
    </v-menu>
  </div>
</template>
<script>
export default {
  data () {
    return {
      text: '',
      showSelect: true,
      items: []
    }
  },
  watch: {
    text: 'inputHandle'
  },
  methods: {
    itemClick (item) {
      this.text = item.name
      this.$refs.search.blur()
      // this.$router.push() 
    },
    inputHandle (text) {
      if (text.trim() !== '') {
        this.showSelect = true
        setTimeout(() => {
          this.getEntity()
        }, 300)
      }
    },
    getEntity () {
      // 请求接口更新 items 数据
      this.items = [
        {
          key: '1234',
          name: '1234'
        },
        {
          key: 'abc',
          name: 'abc'
        },
        {
          key: 'def',
          name: 'def'
        },
        {
          key: 'ccc',
          name: 'ccc'
        },
        {
          key: 'ccc',
          name: 'ccc'
        },
        {
          key: 'ccc',
          name: 'ccc'
        }
      ]
    },
    search () {
      this.$refs.search.blur()
      console.log(this.text)
      // this.$router.push()
    }
  }
}
</script>
<style scoped>
.input-search {
  width: 40%;
  margin: auto;
}
.width-20-percent {
  width: 20%;
}
.img-div {
  margin: 16vh 0 40px 0;
  text-align: center;
}
.v-menu__content {
  box-shadow: none !important;
}
.border-list {
  border: 1px solid #eee !important;
}
</style>

相关文章

  • Vuetify 实现搜索框

    实现思路 使用 Vuetify 中的 v-menu 组件实现,控制光标焦点,在输入框获取的焦点时弹出联想词汇菜单,...

  • 【javaweb】搜索框跳转百度搜索

    需求 自己输入框搜索,跳转到百度页面搜索。 思路 jsp实现搜索框,传递搜索内容到servlet servlet获...

  • Servlet+Ajax

    Servlet+Ajax 实现搜索框智能提示

  • 百度语音搜索

    语音搜索。先实现搜索界面。 定义输入框:search_page.xml 这个搜索框可以手动输入,也可以点击语音图标...

  • 强势围观!JS仿智能搜索究竟有多牛逼?

    JS实现仿google、百度搜索框输入信息智能提示的实现方法 搜索框智能提示是一种全新的搜索方式,可以在进行关键词...

  • EditText实现搜索框

    1.在布局中添加EdidtextView和RecyclerView

    搜索框实现规则

    1、用户点击搜索框 2、当有关键字输入时,同时点亮删除按钮(如图3),删除搜索框原有的提示内容(有个别平台有关键词...

  • 搜索框页面实现

    效果图 1.html 2.css

  • 网页搜索框实现

    趁着热情未减,赶紧的写下自己的第一篇简书。今天要分享的是如何利用 html + css 写出一个令自己满意的搜索框...

  • iOS --- searchBar模糊搜索

    模糊搜索的实现思路:当搜索框开始编辑时对搜索框中的文本与后台给的资源相对比,包含搜索文本的展示在tableview...

网友评论

      本文标题:Vuetify 实现搜索框

      本文链接:https://www.haomeiwen.com/subject/qkxqlhtx.html