vue点击弹框搜索列表功能

先看下开发完成的功能

本来想借助element-ui里的 el-selectel-input组合来做 但是在input框里面输入内容不能用上下键选择到具体搜出来的列表

还有一个问题是当用上下键操作时移到最后一行 再回到第一行的时候它会类似锚点一样定位到第一行 从而把input框卷入到不可见的区域去了

所以觉得这样的体验不是很好,自己就重新做了一个 已经上传了到npm上去了

  • npm下载地址
npm install --save v-select-search
复制代码
  • 引用
import 'v-select-search/lib/v-select-search.css';
import vSelectSearch from 'v-select-search';

Vue.use(vSelectSearch);
复制代码
  • demo

<template>
  <div style="width:250px;overflow:hidden;margin:250px auto;">
    <ct-select
      v-model="text"
      @getSearchName="getName">
      <ct-option
        v-for="(item, index) of dataList"
        :key="index"
        :value="item.value"
        :label="item.label">
      </ct-option>
    </ct-select>
  </div>
</template>

<script type="text/javascript">
  import axios from 'axios';
  export default {
      name: '',
      data() {
          return{
              text: '',
              dataList:[],
          }
      },
      mounted() {
        axios.get('/v2/book/search?q=vue&alt=json&start=1&count=45')
        .then((data)=>{
            this.dataList = [];
            for(let v of data.data.books) {
              this.dataList.push({
                value: v.id,
                label: v.title
              })
            }
        })
        .catch(function(error){
            console.log(error);
        });
      },
      methods: {
        getTextHandler() {
          console.log(this.text);
        },
        getName(val) {
            axios({
              method:'POST',
              url:'/v2/book/search',
              data:{
                  q: val,
                  alt:'json',
                  start: 1,
                  count: 15
              }
            }).then((data)=>{
                this.dataList = [];
                for(let v of data.data.books) {
                  this.dataList.push({
                    value: v.id,
                    label: v.title
                  })
                }
            })
            .catch(function(error){
                console.log(error);
            });
        },
      }
  }
</script>

复制代码
  • 基本配置
参数功能默认值
getSearchName获取搜索文本
data数据格式 [{label: '飞机', value: 1}]
visibleInput是否隐藏搜索框false
autoQuery是否输入后就触发true
width设置输入框宽度值210
delay请求延时间隔(autoQuery为false时)500

git地址

转载于:https://juejin.im/post/5c3c5f25f265da61483bd213

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Vue案例,演示如何在Vue项目中使用百度地图和Element UI组件来实现在弹框中选择地点的功能。 首先,我们需要在项目中引入百度地图和Element UI组件。可以通过在index.html中引入百度地图API和在main.js中引入Element UI组件来完成这一步。 ```html <!-- index.html --> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=YOUR_APP_KEY"></script> <!-- main.js --> import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 接着,我们可以创建一个包含地图和搜索框的组件MapSelector.vue,用于显示地图和搜索框,并提供选点和搜索功能。 ```html <!-- MapSelector.vue --> <template> <div> <div id="map"></div> <el-input placeholder="请输入地址" v-model="searchText" @keyup.enter.native="search"></el-input> </div> </template> <script> export default { data() { return { map: null, marker: null, searchText: '' } }, mounted() { this.initMap() }, methods: { initMap() { this.map = new BMap.Map("map") this.map.centerAndZoom(new BMap.Point(116.404, 39.915), 15) this.map.enableScrollWheelZoom(true) this.map.addEventListener("click", this.handleClick) }, handleClick(e) { this.map.clearOverlays() this.marker = new BMap.Marker(e.point) this.map.addOverlay(this.marker) this.selectedPoint = e.point }, search() { const local = new BMap.LocalSearch(this.map, { onSearchComplete: (results) => { if (local.getStatus() === BMAP_STATUS_SUCCESS) { const point = results.getPoi(0).point this.map.centerAndZoom(point, 15) this.marker = new BMap.Marker(point) this.map.addOverlay(this.marker) this.selectedPoint = point } } }) local.search(this.searchText) }, confirm() { this.$emit('select-point', this.selectedPoint) } } } </script> <style scoped> #map { width: 100%; height: 500px; } </style> ``` 这个组件包含一个地图和一个搜索框,使用了Baidu Map API来实现地图的显示和选点功能,使用了Element UI的Input组件来实现搜索框的显示和搜索功能。当用户在地图上点击时,会触发handleClick方法,该方法会在地图上添加一个标记,并将选中的点保存在selectedPoint变量中。当用户输入地址并按下回车键时,会触发search方法,该方法会使用Baidu Map API的LocalSearch类来搜索地点,并将搜索结果在地图上显示出来。当用户点击确定按钮时,会触发confirm方法,该方法会将选中的点通过事件select-point传递出去给父组件。 接着,我们可以在父组件中使用MapSelector组件,并将其放在一个Element UI的Dialog组件中,用于显示在弹框中选择地点的界面。 ```html <!-- App.vue --> <template> <div> <el-button type="primary" @click="dialogVisible = true">选择地点</el-button> <el-dialog title="选择地点" :visible.sync="dialogVisible" width="800px" @close="dialogVisible = false"> <map-selector @select-point="handleSelectPoint"></map-selector> <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="confirm">确 定</el-button> </span> </el-dialog> </div> </template> <script> import MapSelector from './components/MapSelector.vue' export default { components: { MapSelector }, data() { return { dialogVisible: false, selectedPoint: null } }, methods: { handleSelectPoint(point) { this.selectedPoint = point }, confirm() { // 处理选中的点 console.log(this.selectedPoint) this.dialogVisible = false } } } </script> ``` 这个父组件包含一个按钮,用于触发显示地图选择器的对话框,以及一个对话框,用于显示MapSelector组件。当用户选中一个点时,MapSelector组件会通过事件select-point将选中的点传递给父组件,父组件会将该点保存到selectedPoint变量中。当用户点击确定按钮时,会触发confirm方法,该方法会处理选中的点,并关闭对话框。 以上就是一个简单的Vue案例,演示如何使用百度地图和Element UI组件来实现在弹框中选择地点的功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值