Vue 2和Bootstrap 4的自动完成/提前输入组件

本文介绍了vue-bootstrap-typeahead,一个基于Bootstrap 4和Vue 2的简单预输入/自动完成组件。内容包括安装、基本用法、如何与API配合使用、属性设置、事件监听以及插槽功能的详细说明,并提供了示例和演示。
摘要由CSDN通过智能技术生成

vue-bootstrap-typeahead (vue-bootstrap-typeahead)

A simple list-group based typeahead/autocomplete using Bootstrap 4 and Vue 2.

使用Bootstrap 4和Vue 2的基于列表组的简单预输入/自动完成功能。

vue-bootstrap-typeahead

安装 (Installation)

> npm i vue-bootstrap-typeahead --save

用法 (Usage)

Import and register the component

导入并注册组件

import VueBootstrapTypeahead from 'vue-bootstrap-typeahead'

// Global registration
Vue.component('vue-bootstrap-typeahead', VueBootstrapTypeahead)

// OR

// Local registration
export default {
    components: {
        VueBootstrapTypeahead
    }
}

基本用法 (Basic Usage)

The only required attribute is a data array.

唯一需要的属性是data数组。

<vue-bootstrap-typeahead 
  v-model="query"
  :data="['Canada', 'USA', 'Mexico']"
/>

使用API (Working with API's)

The typeahead does not fetch any data, for maximum flexibility it will only work with already loaded API responses in the form of arrays. The serializer attribute allows you to define a function to turn each array item in the response into a text string, which will appear in the results.

预先输入不会获取任何数据,为了获得最大的灵活性,它仅适用于数组形式的已加载API响应。 serializer属性允许您定义一个函数,将响应中的每个数组项转换为文本字符串,该字符串将出现在结果中。

<template>
  <vue-bootstrap-typeahead
    :data="addresses"
    v-model="addressSearch"
    size="lg"
    :serializer="s => s.text"
    placeholder="Type an address..."
    @hit="selectedAddress = $event"
  >
</template>

<script>
import _ from 'underscore'

const API_URL = 'https://api-url-here.com?query=:query'

export default {
  name: 'TestComponent',

  data() {
    return {
      addresses: [],
      addressSearch: '',
      selectedAddress: null
    }
  },

  methods: {
    async getAddresses(query) {
      const res = await fetch(API_URL.replace(':query', query))
      const suggestions = await res.json()
      this.addresses = suggestions.suggestions
    }
  },

  watch: {
    addressSearch: _.debounce(function(addr) { this.getAddresses(addr) }, 500)
  }
}
</script>

属性 (Attributes)

NameTypeDefaultDescription
dataArrayArray of data to be available for querying. Required
serializerFunctioninput => inputFunction used to convert the entries in the data array into a text string.
sizeStringSize of the input-group. Valid values: sm or lg
backgroundVariantStringBackground color for the autocomplete result list-group items. See valid values
textVariantStringText color for the autocomplete result list-group items. See valid values
inputClassStringClass to the added to the input tag for validation, etc.
maxMatchesNumber10Maximum amount of list items to appear.
minMatchingCharsNumber2Minimum matching characters in query before the typeahead list appears
prependStringText to be prepended to the input-group
appendStringText to be appended to the input-group
名称 类型 默认 描述
数据 Array 可用于查询的数据数组。 需要
序列化器 Function input => input 用于将data数组中的条目转换为文本字符串的函数。
尺寸 String input-group大小。 有效值: smlg
backgroundVariant String 自动完成结果list-group项目的背景颜色。 查看有效值
textVariant String 自动完成结果list-group项目的文本颜色。 查看有效值
inputClass String 将类添加到input标签以进行验证等。
maxMatches Number 10 列表项的最大数量。
minMatchingChars Number 2 出现预先输入列表之前查询中的最小匹配字符
前置 String 文本要放在input-group
附加 String 要添加到input-group文本

大事记 (Events)

NameDescription
hitTriggered when an autocomplete item is selected. The entry in the input data array that was selected is returned.
inputThe component can be used with v-model
名称 描述
hit 选择自动完成项时触发。 返回输入data数组中所选的条目。
input 该组件可以与v-model一起使用

插槽 (Slots)

There are prepend and append slots available for adding buttons or other markup. Overrides the prepend and append attributes.

prependappend插槽可用于添加按钮或其他标记。 覆盖prependappend属性。

范例/演示 (Examples/Demo)

Clone this repository and run npm run serve and navigate to http://localhost:8080 to launch some examples. The source is in src/views/Home.vue

克隆此存储库,然后运行npm run serve并导航至http:// localhost:8080以启动一些示例。 来源在src/views/Home.vue

翻译自: https://vuejsexamples.com/an-autocomplete-typeahead-component-for-vue-2-and-bootstrap-4/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值