オートコンプリート
Framework7には、モバイルフレンドリーでタッチ最適化されたオートコンプリートコンポーネントが付属しています。
オートコンプリートは、スタンドアロンモードまたはドロップダウンとして使用できます。
オートコンプリートのアプリメソッド
オートコンプリートは、JavaScriptを使用してのみ作成および初期化できます。関連するアプリのメソッドを使用する必要があります
app.autocomplete.create(parameters)- オートコンプリートインスタンスを作成します
- parameters - object。オートコンプリートのパラメータを持つオブジェクト
このメソッドは、作成されたオートコンプリートのインスタンスを返します
app.autocomplete.destroy(el)- オートコンプリートインスタンスを破棄します
- el - HTMLElement または string (CSSセレクターを使用) または object。破棄するオートコンプリートインスタンス。
app.autocomplete.get(el)- HTML要素でオートコンプリートインスタンスを取得します
- el - HTMLElement または string (CSSセレクターを使用)。オートコンプリート要素。
このメソッドは、オートコンプリートのインスタンスを返します
app.autocomplete.open(el)- オートコンプリートを開きます
- el - HTMLElement または string (CSSセレクターを使用)。開くオートコンプリート要素。
このメソッドは、オートコンプリートのインスタンスを返します
app.autocomplete.close(el)- オートコンプリートを閉じます
- el - HTMLElement または string (CSSセレクターを使用)。閉じるオートコンプリート要素。
このメソッドは、オートコンプリートのインスタンスを返します
例:
var autocomplete = app.autocomplete.create({
inputEl: '#autocomplete-dropdown',
openIn: 'dropdown',
source: function (query, render) {
...
}
});
オートコンプリートのパラメータ
利用可能なすべてのパラメータのリストを見てみましょう
パラメータ | タイプ | デフォルト | 説明 |
---|---|---|---|
openIn | string | page | オートコンプリートを開く方法を定義します。page 、popup (スタンドアロンの場合)、またはdropdown を指定できます |
source | function (query, render) | 検索query と、一致したアイテムの配列を渡す必要があるrender 関数を受け入れる関数 | |
limit | number | クエリごとにオートコンプリートで表示されるアイテムの最大数を制限します | |
preloader | boolean | false | true に設定すると、オートコンプリートレイアウトにプリローダーが含まれます |
preloaderColor | string | プリローダーの色、デフォルトの色のいずれか | |
value | array | デフォルトで選択された値の配列 | |
valueProperty | string | id | アイテム値を表す一致したアイテムオブジェクトのキーの名前 |
textProperty | string | text | 表示オプションのタイトルとして使用されるアイテム表示値を表す、一致したアイテムオブジェクトのキーの名前 |
スタンドアロンのオートコンプリートパラメータ | |||
requestSourceOnOpen | boolean | false | 有効にすると、オートコンプリートを開いたときにsource 関数に渡されたものがリクエストされます |
openerEl | string HTMLElement | スタンドアロンのオートコンプリートページまたはポップアップをクリック時に開くリンクのCSSセレクターまたはHTMLElementを持つ文字列 | |
popupCloseLinkText | string | 閉じる | ポップアップとして開いたときの「閉じる」ボタンのデフォルトテキスト |
pageBackLinkText | string | 戻る | ページとして開いたときの「戻る」リンクのデフォルトテキスト |
pageTitle | string | オートコンプリートページのタイトル。何も指定されておらず、渡されたopenerEl がリストビューのアイテムの場合、item-title 要素のテキスト値が使用されます | |
popupPush | boolean | false | オートコンプリートポップアップが、開いたときに背後にあるビューをプッシュできるようにします |
popupSwipeToClose | boolean | 未定義 | スワイプでオートコンプリートポップアップを閉じる機能を有効にします。指定されていない場合は、アプリのポップアップswipeToClose パラメータを継承します |
sheetPush | boolean | false | オートコンプリートシートが、開いたときに背後にあるビューをプッシュできるようにします |
sheetSwipeToClose | boolean | 未定義 | スワイプでオートコンプリートシートを閉じる機能を有効にします。指定されていない場合は、アプリのシートswipeToClose パラメータを継承します |
searchbarPlaceholder | string | 検索... | サーチバーのプレースホルダーテキスト |
searchbarDisableText | string | キャンセル | サーチバーの「キャンセル」ボタンのテキスト |
searchbarSpellcheck | boolean | false | サーチバーの入力要素にspellcheck 属性の値を設定します |
notFoundText | string | 見つかりませんでした | 一致するものが見つからなかったときに表示されるテキスト |
multiple | boolean | false | true に設定すると、複数の選択が可能になります |
closeOnSelect | boolean | false | true に設定すると、ユーザーが値を選択したときにオートコンプリートが閉じます。multiple が有効な場合は使用できません |
autoFocus | boolean | false | true に設定すると、オートコンプリートを開いたときに検索フィールドが自動的にフォーカスされます |
animate | boolean | true | false に設定すると、アニメーションなしでスタンドアロンのオートコンプリートが開きます |
navbarColorTheme | string | ナビバーのカラーテーマ。デフォルトのカラーテーマのいずれか | |
formColorTheme | string | フォーム(チェックボックスまたはラジオ)のカラーテーマ。デフォルトのカラーテーマのいずれか | |
routableModals | boolean | false | 開いているオートコンプリートモーダル(openIn: 'popup' の場合)をルーター履歴に追加します。これにより、ルーター履歴を戻ることでオートコンプリートを閉じ、現在のルートをオートコンプリートモーダルに設定できます |
url | string | select/ | 現在のルートとして設定されるスタンドアロンのオートコンプリートURL |
view | object | スタンドアロンのオートコンプリートを使用する場合、初期化されたビューインスタンスへのリンク。指定されていない場合は、デフォルトでメインビューで開かれます。 | |
ドロップダウンオートコンプリートパラメータ | |||
inputEl | string HTMLElement | 関連するテキスト入力のCSSセレクターまたはHTMLElementを持つ文字列 | |
inputEvents | string | input | オートコンプリートのアクションとソースリクエストを処理するために使用される入力イベントを構成できます。たとえば、中国語の文字の構成でキーボードを使用する場合は、change keyup compositionend に変更できます |
highlightMatches | boolean | true | オートコンプリートの結果で一致箇所を強調表示します |
typeahead | boolean | false | タイプアヘッドを有効にし、一致する最初のアイテムで入力値を事前に入力します |
dropdownPlaceholderText | string | ドロップダウンプレースホルダーテキストを指定します | |
updateInputValueOnSelect | boolean | true | true の場合、関連する入力の値も更新されます |
dropdownContainerEl | string HTMLElement | デフォルトでは、ドロップダウンは親のページコンテンツ要素に追加されます。ここにドロップダウン要素を追加する別の要素を指定できます | |
レンダー関数 | |||
renderDropdown | function(items) | オートコンプリートドロップダウンをレンダリングする関数。ドロップダウンHTML文字列を返す必要があります | |
renderPage | function(items) | オートコンプリートページをレンダリングする関数。ページHTML文字列を返す必要があります | |
renderPopup | function(items) | オートコンプリートポップアップをレンダリングする関数。ポップアップHTML文字列を返す必要があります | |
renderItem | function(item, index) | 単一のオートコンプリートをレンダリングする関数。アイテムHTML文字列を返す必要があります | |
renderSearchbar | function | サーチバーをレンダリングする関数。サーチバーHTML文字列を返す必要があります | |
renderNavbar | function | ナビバーをレンダリングする関数。ナビバーHTML文字列を返す必要があります | |
イベント | |||
on | object | イベントハンドラーを持つオブジェクト。例えば
|
以下のすべてのパラメータは、すべてのオートコンプリートのデフォルトを設定するために、autocomplete
プロパティの下のグローバルアプリパラメータで使用できることに注意してください。例えば
var app = new Framework7({
autocomplete: {
openIn: 'popup',
animate: false,
}
});
オートコンプリートのメソッドとプロパティ
オートコンプリートを初期化すると、変数(上記の例のautocomplete
変数など)に、役立つメソッドとプロパティを持つ初期化されたインスタンスがあります
プロパティ | |
---|---|
autocomplete.params | 渡された初期化パラメータを持つオブジェクト |
autocomplete.value | 選択されたアイテムの配列 |
autocomplete.opened | オートコンプリートが現在開いている場合はtrue |
autocomplete.openerEl | オートコンプリートオープナー要素のHTML要素(初期化時に渡された場合) |
autocomplete.$openerEl | オートコンプリートオープナー要素のDom7インスタンス(初期化時に渡された場合) |
autocomplete.inputEl | オートコンプリート入力のHTML要素(初期化時に渡された場合) |
autocomplete.$inputEl | オートコンプリート入力のDom7インスタンス(初期化時に渡された場合) |
autocomplete.$dropdownEl | オートコンプリートドロップダウンのDom7インスタンス |
autocomplete.url | オートコンプリートURL(url パラメータで渡されたもの) |
autocomplete.view | オートコンプリートビュー(view パラメータで渡されたもの)または見つかった親ビュー |
autocomplete.el | オートコンプリートコンテナのHTML要素:ドロップダウン要素、またはポップアップ要素、またはページ要素。オートコンプリートが開いている場合に利用可能 |
autocomplete.$el | オートコンプリートコンテナのDom7インスタンス:ドロップダウン要素、またはポップアップ要素、またはページ要素。オートコンプリートが開いている場合に利用可能 |
autocomplete.searchbar | オートコンプリートページのサーチバーインスタンス |
メソッド | |
autocomplete.open() | オートコンプリート(ドロップダウン、ページ、またはポップアップ)を開きます |
autocomplete.close() | オートコンプリートを閉じます |
autocomplete.preloaderShow() | オートコンプリートプリローダーを表示します |
autocomplete.preloaderHide() | オートコンプリートプリローダーを非表示にします |
autocomplete.destroy() | オートコンプリートインスタンスを破棄し、すべてのイベントを削除します |
autocomplete.on(event, handler) | イベントハンドラーを追加します |
autocomplete.once(event, handler) | 発生後に削除されるイベントハンドラーを追加します |
autocomplete.off(event, handler) | イベントハンドラーを削除します |
autocomplete.off(event) | 指定されたイベントのすべてのハンドラーを削除します |
autocomplete.emit(event, ...args) | インスタンスでイベントを発火します |
オートコンプリートイベント
オートコンプリートインスタンスは、自身のインスタンスとアプリのインスタンスの両方でイベントを発行します。アプリインスタンスのイベントには、autocomplete
というプレフィックスが付いた同じ名前が付けられています。
イベント | ターゲット | 引数 | 説明 |
---|---|---|---|
change | autocomplete | autocomplete, value | オートコンプリートの値が変更されたときにイベントがトリガーされます。返される value は、選択された項目の配列です。 |
autocompleteChange | app | ||
open | autocomplete | autocomplete | オートコンプリートがオープニングアニメーションを開始したときにイベントがトリガーされます。イベントハンドラは、引数としてオートコンプリートインスタンスを受け取ります。 |
autocompleteOpen | app | ||
opened | autocomplete | autocomplete | オートコンプリートがオープニングアニメーションを完了した後にイベントがトリガーされます。イベントハンドラは、引数としてオートコンプリートインスタンスを受け取ります。 |
autocompleteOpened | app | ||
close | autocomplete | autocomplete | オートコンプリートがクロージングアニメーションを開始したときにイベントがトリガーされます。イベントハンドラは、引数としてオートコンプリートインスタンスを受け取ります。 |
autocompleteClose | app | ||
closed | autocomplete | autocomplete | オートコンプリートがクロージングアニメーションを完了した後にイベントがトリガーされます。イベントハンドラは、引数としてオートコンプリートインスタンスを受け取ります。 |
autocompleteClosed | app | ||
beforeDestroy | autocomplete | autocomplete | オートコンプリートインスタンスが破棄される直前にイベントがトリガーされます。イベントハンドラは、引数としてオートコンプリートインスタンスを受け取ります。 |
autocompleteBeforeDestroy | app |
CSS変数
以下は、関連するCSS変数(CSSカスタムプロパティ)のリストです。
コメントアウトされた変数はデフォルトでは指定されておらず、その値はこの場合のフォールバック先となることに注意してください。
:root {
--f7-autocomplete-dropdown-placeholder-color: #a9a9a9;
--f7-autocomplete-dropdown-preloader-size: 20px;
--f7-autocomplete-dropdown-font-size: var(--f7-list-font-size);
/*
--f7-autocomplete-dropdown-selected-bg-color: rgba(var(--f7-theme-color-rgb), 0.2);
*/
}
.ios {
--f7-autocomplete-dropdown-box-shadow: 0px 3px 3px rgba(0, 0, 0, 0.2);
--f7-autocomplete-dropdown-text-matching-font-weight: 600;
--f7-autocomplete-dropdown-bg-color: #fff;
--f7-autocomplete-dropdown-text-color: #000;
--f7-autocomplete-dropdown-text-matching-color: #000;
}
.ios .dark,
.ios.dark {
--f7-autocomplete-dropdown-bg-color: #1c1c1d;
--f7-autocomplete-dropdown-text-color: #fff;
--f7-autocomplete-dropdown-text-matching-color: #fff;
}
.md {
--f7-autocomplete-dropdown-box-shadow: none;
--f7-autocomplete-dropdown-text-matching-font-weight: 500;
--f7-autocomplete-dropdown-text-matching-color: #000;
}
.md .dark,
.md.dark {
--f7-autocomplete-dropdown-text-matching-color: #fff;
}
.md,
.md .dark,
.md [class*='color-'] {
--f7-autocomplete-dropdown-bg-color: var(--f7-md-surface-2);
--f7-autocomplete-dropdown-text-color: var(--f7-md-on-surface);
}
例
<template>
<div class="page">
<div class="navbar">
<div class="navbar-bg"></div>
<div class="navbar-inner sliding">
<div class="title">Autocomplete</div>
<div class="subnavbar">
<form class="searchbar" id="searchbar-autocomplete">
<div class="searchbar-inner">
<div class="searchbar-input-wrap">
<input type="search" placeholder="Search" />
<i class="searchbar-icon"></i>
<span class="input-clear-button"></span>
</div>
<span class="searchbar-disable-button">Cancel</span>
</div>
</form>
</div>
</div>
</div>
<div class="page-content">
<div class="block-title">Dropdown Autocomplete</div>
<div class="block">
<p>Dropdown autocomplete is good to use as a quick and simple solution to provide more options in addition to
free-type value.</p>
</div>
<div class="list list-strong-ios list-outline-ios">
<div class="block-header">Simple Dropdown Autocomplete</div>
<ul>
<li class="item-content item-input">
<div class="item-inner">
<div class="item-title item-label">Fruit</div>
<div class="item-input-wrap">
<input type="text" placeholder="Fruit" id="autocomplete-dropdown" />
</div>
</div>
</li>
</ul>
</div>
<div class="list list-strong-ios list-outline-ios">
<div class="block-header">Dropdown With All Values</div>
<ul>
<li class="item-content item-input">
<div class="item-inner">
<div class="item-title item-label">Fruit</div>
<div class="item-input-wrap">
<input type="text" placeholder="Fruit" id="autocomplete-dropdown-all" />
</div>
</div>
</li>
</ul>
</div>
<div class="list list-strong-ios list-outline-ios">
<div class="block-header">Dropdown With Placeholder</div>
<ul>
<li class="item-content item-input">
<div class="item-inner">
<div class="item-title item-label">Fruit</div>
<div class="item-input-wrap">
<input type="text" placeholder="Fruit" id="autocomplete-dropdown-placeholder" />
</div>
</div>
</li>
</ul>
</div>
<div class="list list-strong-ios list-outline-ios">
<div class="block-header">Dropdown With Typeahead</div>
<ul>
<li class="item-content item-input">
<div class="item-inner">
<div class="item-title item-label">Fruit</div>
<div class="item-input-wrap">
<input type="text" placeholder="Fruit" id="autocomplete-dropdown-typeahead" />
</div>
</div>
</li>
</ul>
</div>
<div class="list list-strong-ios list-outline-ios">
<div class="block-header">Dropdown With Ajax-Data</div>
<ul>
<li class="item-content item-input">
<div class="item-inner">
<div class="item-title item-label">Language</div>
<div class="item-input-wrap">
<input type="text" placeholder="Language" id="autocomplete-dropdown-ajax" />
</div>
</div>
</li>
</ul>
</div>
<div class="list list-strong-ios list-outline-ios">
<div class="block-header">Dropdown With Ajax-Data + Typeahead</div>
<ul>
<li class="item-content item-input">
<div class="item-inner">
<div class="item-title item-label">Language</div>
<div class="item-input-wrap">
<input type="text" placeholder="Language" id="autocomplete-dropdown-ajax-typeahead" />
</div>
</div>
</li>
</ul>
</div>
<div class="block-title">Standalone Autocomplete</div>
<div class="block">
<p>Standalone autocomplete provides better mobile UX by opening it in a new page or popup. Good to use when you
need to get strict values without allowing free-type values.</p>
</div>
<div class="list list-strong list-outline-ios">
<div class="block-header">Simple Standalone Autocomplete</div>
<ul>
<li>
<a id="autocomplete-standalone" class="item-link item-content autocomplete-opener">
<input type="hidden" />
<div class="item-inner">
<div class="item-title">Favorite Fruite</div>
<div class="item-after"></div>
</div>
</a>
</li>
</ul>
</div>
<div class="list list-strong list-outline-ios">
<div class="block-header">Popup Autocomplete</div>
<ul>
<li>
<a id="autocomplete-standalone-popup" class="item-link item-content autocomplete-opener">
<input type="hidden" />
<div class="item-inner">
<div class="item-title">Favorite Fruite</div>
<div class="item-after"></div>
</div>
</a>
</li>
</ul>
</div>
<div class="list list-strong list-outline-ios">
<div class="block-header">Multiple Values</div>
<ul>
<li>
<a id="autocomplete-standalone-multiple" class="item-link item-content autocomplete-opener">
<input type="hidden" />
<div class="item-inner">
<div class="item-title">Favorite Fruite</div>
<div class="item-after"></div>
</div>
</a>
</li>
</ul>
</div>
<div class="list list-strong list-outline-ios">
<div class="block-header">With Ajax-Data</div>
<ul>
<li>
<a id="autocomplete-standalone-ajax" class="item-link item-content autocomplete-opener">
<input type="hidden" />
<div class="item-inner">
<div class="item-title">Language</div>
<div class="item-after"></div>
</div>
</a>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
export default (props, { $, $f7, $onMounted, $onBeforeUnmount }) => {
const fruits = 'Apple Apricot Avocado Banana Melon Orange Peach Pear Pineapple'.split(' ');
let searchbar;
let autocompleteDropdownSimple;
let autocompleteDropdownAll;
let autocompleteDropdownPlaceholder;
let autocompleteDropdownTypeahead;
let autocompleteDropdownAjax;
let autocompleteDropdownAjaxTypeahead;
let autocompleteStandaloneSimple;
let autocompleteStandalonePopup;
let autocompleteStandaloneMultiple;
let autocompleteStandaloneAjax;
$onBeforeUnmount(() => {
searchbar.destroy();
autocompleteDropdownSimple.destroy();
autocompleteDropdownAll.destroy();
autocompleteDropdownPlaceholder.destroy();
autocompleteDropdownTypeahead.destroy();
autocompleteDropdownAjax.destroy();
autocompleteDropdownAjaxTypeahead.destroy();
autocompleteStandaloneSimple.destroy();
autocompleteStandalonePopup.destroy();
autocompleteStandaloneMultiple.destroy();
autocompleteStandaloneAjax.destroy();
})
$onMounted(() => {
autocompleteDropdownSimple = $f7.autocomplete.create({
inputEl: '#autocomplete-dropdown',
openIn: 'dropdown',
source: function (query, render) {
console.log(query);
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
}
});
// Dropdown with all values
autocompleteDropdownAll = $f7.autocomplete.create({
inputEl: '#autocomplete-dropdown-all',
openIn: 'dropdown',
source: function (query, render) {
var results = [];
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
}
});
// Dropdown with placeholder
autocompleteDropdownPlaceholder = $f7.autocomplete.create({
inputEl: '#autocomplete-dropdown-placeholder',
openIn: 'dropdown',
dropdownPlaceholderText: 'Try to type "Apple"',
source: function (query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
}
});
// Dropdown with typeahead
autocompleteDropdownTypeahead = $f7.autocomplete.create({
inputEl: '#autocomplete-dropdown-typeahead',
openIn: 'dropdown',
dropdownPlaceholderText: 'Try to type "Pineapple"',
typeahead: true,
source: function (query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) === 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
}
});
// Dropdown with ajax data
autocompleteDropdownAjax = $f7.autocomplete.create({
inputEl: '#autocomplete-dropdown-ajax',
openIn: 'dropdown',
preloader: true, //enable preloader
/* If we set valueProperty to "id" then input value on select will be set according to this property */
valueProperty: 'name', //object's "value" property name
textProperty: 'name', //object's "text" property name
limit: 20, //limit to 20 results
dropdownPlaceholderText: 'Try "JavaScript"',
source: function (query, render) {
var autocomplete = this;
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Show Preloader
autocomplete.preloaderShow();
// Do Ajax request to Autocomplete data
fetch(`./js/autocomplete-languages.json?query=${query}`)
.then((res) => res.json())
.then((data) => {
// Find matched items
for (let i = 0; i < data.length; i += 1) {
if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) >= 0)
results.push(data[i]);
}
// Hide Preoloader
autocomplete.preloaderHide();
// Render items by passing array with result items
render(results);
});
}
});
// Dropdown with ajax data
autocompleteDropdownAjaxTypeahead = $f7.autocomplete.create({
inputEl: '#autocomplete-dropdown-ajax-typeahead',
openIn: 'dropdown',
preloader: true, //enable preloader
/* If we set valueProperty to "id" then input value on select will be set according to this property */
valueProperty: 'name', //object's "value" property name
textProperty: 'name', //object's "text" property name
limit: 20, //limit to 20 results
typeahead: true,
dropdownPlaceholderText: 'Try "JavaScript"',
source: function (query, render) {
var autocomplete = this;
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Show Preloader
autocomplete.preloaderShow();
// Do Ajax request to Autocomplete data
fetch(`./js/autocomplete-languages.json?query=${query}`)
.then((res) => res.json())
.then((data) => {
// Find matched items
for (let i = 0; i < data.length; i += 1) {
if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) >= 0)
results.push(data[i]);
}
// Hide Preoloader
autocomplete.preloaderHide();
// Render items by passing array with result items
render(results);
});
}
});
// Simple Standalone
autocompleteStandaloneSimple = $f7.autocomplete.create({
openIn: 'page', //open in page
openerEl: '#autocomplete-standalone', //link that opens autocomplete
closeOnSelect: true, //go back after we select something
source: function (query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
},
on: {
change: function (value) {
console.log(value);
// Add item text value to item-after
$('#autocomplete-standalone').find('.item-after').text(value[0]);
// Add item value to input value
$('#autocomplete-standalone').find('input').val(value[0]);
},
},
});
// Standalone Popup
autocompleteStandalonePopup = $f7.autocomplete.create({
openIn: 'popup', //open in page
openerEl: '#autocomplete-standalone-popup', //link that opens autocomplete
closeOnSelect: true, //go back after we select something
source: function (query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
},
on: {
change: function (value) {
// Add item text value to item-after
$('#autocomplete-standalone-popup').find('.item-after').text(value[0]);
// Add item value to input value
$('#autocomplete-standalone-popup').find('input').val(value[0]);
},
},
});
// Multiple Standalone
autocompleteStandaloneMultiple = $f7.autocomplete.create({
openIn: 'page', //open in page
openerEl: '#autocomplete-standalone-multiple', //link that opens autocomplete
multiple: true, //allow multiple values
source: function (query, render) {
var autocomplete = this;
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
},
on: {
change: function (value) {
// Add item text value to item-after
$('#autocomplete-standalone-multiple').find('.item-after').text(value.join(', '));
// Add item value to input value
$('#autocomplete-standalone-multiple').find('input').val(value.join(', '));
}
}
});
// Standalone With Ajax
autocompleteStandaloneAjax = $f7.autocomplete.create({
openIn: 'page', //open in page
openerEl: '#autocomplete-standalone-ajax', //link that opens autocomplete
multiple: true, //allow multiple values
valueProperty: 'id', //object's "value" property name
textProperty: 'name', //object's "text" property name
limit: 50,
preloader: true, //enable preloader
source: function (query, render) {
var autocomplete = this;
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Show Preloader
autocomplete.preloaderShow();
// Do Ajax request to Autocomplete data
fetch(`./js/autocomplete-languages.json?query=${query}`)
.then((res) => res.json())
.then((data) => {
// Find matched items
for (let i = 0; i < data.length; i += 1) {
if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) >= 0)
results.push(data[i]);
}
// Hide Preoloader
autocomplete.preloaderHide();
// Render items by passing array with result items
render(results);
});
},
on: {
change: function (value) {
var itemText = [],
inputValue = [];
for (var i = 0; i < value.length; i++) {
itemText.push(value[i].name);
inputValue.push(value[i].id);
}
// Add item text value to item-after
$('#autocomplete-standalone-ajax').find('.item-after').text(itemText.join(', '));
// Add item value to input value
$('#autocomplete-standalone-ajax').find('input').val(inputValue.join(', '));
},
},
});
// Searchbar Autocomplete
autocompleteSearchbar = $f7.autocomplete.create({
openIn: 'dropdown',
inputEl: '#searchbar-autocomplete input[type="search"]',
dropdownPlaceholderText: 'Type "Apple"',
source: function (query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(fruits[i]);
}
// Render items by passing array with result items
render(results);
}
})
searchbar = $f7.searchbar.create({
el: '#searchbar-autocomplete',
customSearch: true,
on: {
search: function (sb, query) {
console.log(query);
}
}
})
})
return $render;
}
</script>