【问题标题】:autocomplete for input field in quasar V2类星体 V2 中输入字段的自动完成
【发布时间】:2023-01-21 17:39:05
【问题描述】:

如何为类星体中的文本字段实现自动完成(内容提议)功能?基本上是在文本字段中输入,然后在输入 2 个字符后根据网络服务的内容提出建议。

旧版本中似乎有一个组件,但我在当前文档中找不到参考:
https://v0-17.quasar-framework.org/components/autocomplete.html

q-select 也有自动完成处理,但 q-input 没有自动完成处理:
https://quasar.dev/vue-components/select#native-attributes-with-use-input

使用:

  • Vue 3.0.0
  • 类星体 2.6.0

【问题讨论】:

  • 你最后找到什么了吗?
  • 我最终使用了 Quasar-Select,似乎这是推荐的方式:quasar.dev/vue-components/select#filtering-and-autocomplete
  • @flavio.donze 这就是我要找的!您可能想回答(并接受)您自己的问题,以便其他人可以通过投票(或不投票)来同意。
  • 做到了,谢谢。

标签: javascript vue.js autocomplete vuejs3 quasar-framework


【解决方案1】:

实现自动完成功能的官方方法不是使用文本字段QInput,而是使用QSelect组件。

Filtering and autocomplete | Quasar Framework

这是 QSelect 文档的示例:

<template>
  <div class="q-pa-md">
    <div class="q-gutter-md row">
      <q-select
        filled
        :model-value="model"
        use-input
        hide-selected
        fill-input
        input-debounce="0"
        :options="options"
        @filter="filterFn"
        @input-value="setModel"
        hint="Text autocomplete"
        style="width: 250px; padding-bottom: 32px"
      >
        <template v-slot:no-option>
          <q-item>
            <q-item-section class="text-grey">
              No results
            </q-item-section>
          </q-item>
        </template>
      </q-select>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'

const stringOptions = [
  'Google', 'Facebook', 'Twitter', 'Apple', 'Oracle'
].reduce((acc, opt) => {
  for (let i = 1; i <= 5; i++) {
    acc.push(opt + ' ' + i)
  }
  return acc
}, [])

export default {
  setup () {
    const model = ref(null)
    const options = ref(stringOptions)

    return {
      model,
      options,

      filterFn (val, update, abort) {
        update(() => {
          const needle = val.toLocaleLowerCase()
          options.value = stringOptions.filter(v => v.toLocaleLowerCase().indexOf(needle) > -1)
        })
      },

      setModel (val) {
        model.value = val
      }
    }
  }
}
</script>

取决于用例惰性过滤可能是解决方案,例如从 REST 服务读取内容:

<template>
  <div class="q-pa-md">
    <div class="q-gutter-md">
      <q-select
        filled
        v-model="model"
        use-input
        hide-selected
        fill-input
        input-debounce="0"
        label="Lazy filter"
        :options="options"
        @filter="filterFn"
        @filter-abort="abortFilterFn"
        style="width: 250px"
        hint="With hide-selected and fill-input"
      >
        <template v-slot:no-option>
          <q-item>
            <q-item-section class="text-grey">
              No results
            </q-item-section>
          </q-item>
        </template>
      </q-select>

      <q-select
        filled
        v-model="model"
        use-input
        use-chips
        input-debounce="0"
        label="Lazy filter"
        :options="options"
        @filter="filterFn"
        @filter-abort="abortFilterFn"
        style="width: 250px"
        hint="With use-chips"
      >
        <template v-slot:no-option>
          <q-item>
            <q-item-section class="text-grey">
              No results
            </q-item-section>
          </q-item>
        </template>
      </q-select>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'

const stringOptions = [
  'Google', 'Facebook', 'Twitter', 'Apple', 'Oracle'
]

export default {
  setup () {
    const options = ref(stringOptions)

    return {
      model: ref(null),
      options,

      filterFn (val, update, abort) {
        // call abort() at any time if you can't retrieve data somehow

        setTimeout(() => {
          update(() => {
            if (val === '') {
              options.value = stringOptions
            }
            else {
              const needle = val.toLowerCase()
              options.value = stringOptions.filter(v => v.toLowerCase().indexOf(needle) > -1)
            }
          })
        }, 1500)
      },

      abortFilterFn () {
        // console.log('delayed filter aborted')
      }
    }
  }
}
</script>

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多