vue下拉搜索

  
<template>
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <input class="form-control" type="text" placeholder='请输入搜索的内容' v-model='textInput' @keyup='search($event)'
             @keydown.down='change()' @keydown.up.provent='up()'>
      <ul class="list-group">
        <li class="list-group-item" v-for='(v, k) in redHtml' :class='{bg: k == index}' v-html="v" @click='enter(k)' @mouseenter='mouseEnter(k)' @mouseleave='mouseLeave(k)'>{{v}}</li>
      </ul>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'BoxInput',
    data() {
      return {
        textInput: '',
        textOutput: [],
        index: -1,     //索引 实现上下选择
        showRed: false
      }
    },
    computed: {
      // 计算属性的 getter
      redHtml: function () {
        // `this` 指向 vm 实例
        let this_ = this
        return this.textOutput.map(function (str) {
          let reger = new RegExp("(" + this_.textInput + ")", "gi")
          return str.replace(reger, "<span style=\"color: red\">" + this_.textInput + "</span>")
        })
      }
    },
    watch: {
      textInput: function () {
        //跨域调用百度搜索接口
        this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {
          params: {wd: this.textInput},
          jsonp: 'cb'
        })
          .then(function (res) {
            this.textOutput = res.data.s
          }, function (res) {
            alert('failed');
          });
      }
    },
    methods: {
      search: function (e) {
        // 当按上下键时返回
        if (e.keyCode == 38 || e.keyCode == 40) return
        // 按回车时搜索
        if (e.keyCode == 13) {
          // 新的页面打开
          this.info = this.textInput
          window.open('https://www.baidu.com/s?wd=' + this.info)
          this.info = ''
        }
      },
      //按下键往下选择
      change: function () {
        this.index++
        this.info = this.textOutput[this.index]  //输入框显示选择的内容
        if (this.index == this.textOutput.length) this.index = 0  //当选到最后一个时索引变为0
      },
      // 按上键往上选择
      up: function () {
        this.index--
        this.info = this.textOutput[this.index]
        if (this.index == -1) this.index = this.textOutput.length - 1
      },
      // 鼠标点击事件
      enter: function (k) {
        this.index = k
        this.info = this.textOutput[k]
        window.open('https://www.baidu.com/s?wd=' + this.info)
        this.info = ''
      },
      // 鼠标进入事件
      mouseEnter: function (k) {
        this.index = k
      },
      // 鼠标离开事件
      mouseLeave: function (k) {
        this.index = k
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .bg {
    background-color: rgba(63, 255, 40, 0.5)
  }
  li:hover {
    cursor:pointer;
  }
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 以下是一个简单的uniapp下拉搜索框的Vue3写法: template部分: ```html <template> <div> <input v-model="searchText" type="text" placeholder="输入关键字搜索" @input="search" /> <ul> <li v-for="(item, index) in searchResult" :key="index">{{ item }}</li> </ul> </div> </template> ``` script部分: ```javascript <script> import { ref } from 'vue' export default { name: 'Search', setup() { const searchText = ref('') const searchResult = ref([]) const search = () => { // 在此处调用接口或处理数据 // 将结果存储在 searchResult 中 } return { searchText, searchResult, search } } } </script> ``` 以上代码中,使用了Vue3的新特性ref来定义searchText和searchResult变量,以及search方法。其中,search方法可以在输入框输入内容被触发,可以在此处调用接口或进行数据处理,并将结果存储在searchResult中,最终在模板中渲染出搜索结果。 ### 回答2: 在Uniapp中使用Vue3编写下拉搜索框,可以按照以下步骤进行操作: 1. 首先,安装uni-ui插件,该插件提供了下拉搜索框组件:uni-load-more。 2. 在需要使用下拉搜索框的页面或组件中,引入uni-load-more组件: ```vue <template> <view> <uni-load-more ref="loadmore" :loadmore-method="loadmore" :is-no-more="isNoMore"> <view slot="content"> <!--下拉搜索框内容区域--> </view> </uni-load-more> </view> </template> <script> import uniLoadMore from '@dcloudio/uni-ui/lib/uni-load-more/uni-load-more.vue'; export default { components: { uniLoadMore }, data() { return { isNoMore: false // 是否还有更多数据供加载 } }, methods: { loadmore() { // 加载更多数据的方法 } } } </script> ``` 3. 在`data`中定义`isNoMore`变量来控制是否还有更多数据供加载,在`loadmore`方法中实现加载更多数据的逻辑。 4. 在`<view slot="content">`标签内添加下拉搜索框的内容区域。 5. 根据具体需求,可以在`<template>`标签中添加样式来美化下拉搜索框。 ### 回答3: 在uniapp中实现下拉搜索框可以使用Vue3的写法,具体步骤如下: 1. 创建一个Vue组件,命名为SearchBox,使用组件化的思想将下拉搜索框封装起来。 2. 在SearchBox组件中,使用template标签编写组件的HTML结构,包含一个输入框和一个下拉列表。使用input事件监听用户输入的关键字,并通过v-model绑定到data中的keyword变量上。 3. 在data中定义一个列表变量list,用于存储搜索结果列表。在methods中编写一个search方法,当用户输入关键字,通过接口请求后端数据,并将返回的结果赋值给list变量。 4. 在组件的HTML结构中,使用v-if指令判断当list有数据显示下拉列表,使用v-for指令遍历list,展示每一条搜索结果。 5. 使用@focus事件监听输入框的获得焦点事件,当输入框获得焦点,显示下拉列表。 6. 使用@click事件监听下拉列表项的点击事件,当用户点击某个搜索结果,将该搜索结果赋值给keyword变量,并触发一个事件,将选中的结果发送到父组件。 7. 在父组件中使用SearchBox组件,并监听SearchBox的事件,获取选中搜索结果,并进行后续处理,如显示详细信息或进行页面跳转等。 以上是使用Vue3编写uniapp下拉搜索框的步骤,通过封装组件的方式可以实现代码的复用,提高开发效率。可以根据具体的需求对组件进行扩展,添加更多的交互功能和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故事只若初见

坚持就是胜利

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值