- Framework7教程
- Framework7 - 主页
- Framework7 - 概述
- Framework7 - 环境
- Framework7 组件
- Framework7 - 布局
- Framework7 - 导航栏
- Framework7 - 工具栏
- Framework7 - 搜索栏
- Framework7 - 状态栏
- Framework7 - 侧面板
- Framework7 - 内容块
- Framework7 - 布局网格
- Framework7 - 覆盖层
- Framework7 - 预加载器
- Framework7 - 进度条
- Framework7 - 列表视图
- Framework7 - 手风琴
- Framework7 - 卡片
- Framework7 - 芯片
- Framework7 - 按钮
- Framework7 - 操作按钮
- Framework7 - 表格
- Framework7 - 选项卡
- Framework7 - Swiper 滑块
- Framework7 - 照片浏览器
- Framework7 - 自动完成
- Framework7 - 选择器
- Framework7 - 日历
- Framework7 - 刷新
- Framework7 - 无限滚动
- Framework7 - 消息
- Framework7 - 消息栏
- Framework7 - 通知
- Framework7 - 延迟加载
- Framework7 样式
- Framework7 - 颜色主题
- Framework7 - 细线
- Framework7 模板
- Framework7 - 模板概述
- Framework7 - 自动编译
- Framework7 - Template7 页面
- Framework7 快速点击
- Framework7 - 活动状态
- Framework7 - 点击保持事件
- Framework7 - 触摸波纹
- Framework7 有用的资源
- Framework7 - 快速指南
- Framework7 - 有用的资源
- Framework7 - 讨论
Framework7 - 自动完成
描述
Autocomplete 是 Framework7 的移动友好型和触摸优化组件,可以作为下拉菜单或独立方式。您可以使用 JavaScript 方法创建和初始化 Autocomplete 实例 -
myApp.autocomplete(parameters)
其中参数是用于初始化自动完成实例的必需对象。
自动完成参数
下表列出了 Framework7 中可用的自动完成参数 -
| 序列号 | 参数及说明 | 类型 | 默认 |
|---|---|---|---|
| 1 | 打开 它定义了如何打开可用作下拉菜单、弹出窗口或页面的自动完成功能。 |
细绳 | 页 |
| 2 | 来源 它使用自动完成实例、搜索查询和渲染函数来通过数组传递匹配的项目。 |
功能(自动完成、查询、渲染) | - |
| 3 | 价值属性 它指定匹配项目对象的键的项目值。 |
细绳 | ID |
| 4 | 限制 它显示每个查询自动完成中有限数量的项目。 |
数字 | - |
| 5 | 预加载器 预加载器可用于通过将其设置为 true 来指定自动完成布局。 |
布尔值 | 错误的 |
| 6 | 预加载颜色 它指定预加载器颜色。默认情况下,颜色为“黑色”。 |
细绳 | - |
| 7 | 价值 使用默认选定值定义数组。 |
大批 | - |
| 8 | 文本属性 它指定匹配的项目对象的键的项目值,可以用作显示选项的标题。 |
细绳 | 文本 |
独立自动完成参数
下表列出了 Framework7 中可用的独立自动完成参数 -
| 序列号 | 参数及说明 | 类型 | 默认 |
|---|---|---|---|
| 1 | 开场白 它是字符串或 HTML 元素参数,它将打开独立的自动完成页面。 |
字符串或 HTMLElement | - |
| 2 | 弹出关闭文本 它用于关闭自动完成弹出窗口。 |
细绳 | '关闭' |
| 3 | 后台文本 当自动完成作为页面打开时,它提供反向链接。 |
细绳 | '后退' |
| 4 | 页面标题 它指定自动完成页面标题。 |
细绳 | - |
| 5 | 搜索栏占位符文本 它指定搜索栏占位符文本。 |
细绳 | '搜索' |
| 6 | 搜索栏取消文本 它定义搜索栏取消按钮文本。 |
细绳 | '取消' |
| 7 | 未找到文本 当没有找到匹配的元素时,它会显示文本。 |
细绳 | '什么都没找到' |
| 8 | 多种的 它允许通过将其设置为 true 来选择多项选择。 |
布尔值 | 错误的 |
| 9 | 导航栏主题 它指定导航栏的颜色主题。 |
细绳 | - |
| 10 | 返回选择 当用户选择值时,将通过将其设置为 true 来关闭自动完成。 |
布尔值 | 错误的 |
| 11 | 形式主题 它指定表单的颜色主题。 |
细绳 | - |
下拉自动完成参数
下表列出了 Framework7 中可用的下拉自动完成参数 -
| 序列号 | 参数及说明 | 类型 | 默认 |
|---|---|---|---|
| 1 | 输入 它是用于文本输入的字符串或 HTML 元素。 |
字符串或 HTMLElement | - |
| 2 | 下拉占位符文本 它指定下拉占位符文本。 |
细绳 | - |
| 3 | 选择时更新输入值 您可以通过将其设置为 true 来更新 select 上的输入值。 |
布尔值 | 真的 |
| 4 | 展开输入 您可以通过将item-input设置为 true 来扩展列表视图中的文本输入,以使下拉菜单期间全屏显示。 |
布尔值 | 错误的 |
自动完成回调函数
下表列出了 Framework7 中可用的下拉自动完成参数 -
| 序列号 | 参数及说明 | 类型 | 默认 |
|---|---|---|---|
| 1 | 改变时 每当自动完成值更改时,都会执行此回调函数。 |
函数(自动完成、值) | - |
| 2 | 打开时 每当自动完成打开时,都会执行此回调函数。 |
功能(自动完成) | - |
| 3 | 关闭时 每当自动完成关闭时,都会执行此回调函数。 |
功能(自动完成) | - |
自动完成模板
下表列出了 Framework7 中可用的下拉自动完成参数 -
| 序列号 | 参数及说明 | 类型 | 默认 |
|---|---|---|---|
| 1 | 导航栏模板 它是独立的自动完成导航栏模板。 |
细绳 | - |
| 2 | 项目模板 它是独立的 template7 表单项。 |
细绳 | - |
| 3 | 下拉模板 这是 template7 下拉模板。 |
细绳 | - |
| 4 | 下拉项目模板 它是 template7 下拉列表项。 |
细绳 | - |
| 5 | dropdown占位符模板 它是 template7 下拉占位符项。 |
细绳 | - |
默认模板
以下是上述定义的模板参数的默认模板代码片段 -
导航栏模板
<div class = "navbar {{#if navbarTheme}}theme-{{navbarTheme}}{{/if}}">
<div class = "navbar-inner">
<div class = "left sliding">
{{#if material}}
<a href = "#" class = "link {{#if inPopup}}close-popup{{else}}back{{/if}} icon-only">
<i class = "icon icon-back"></i>
</a>
{{else}}
<a href = "#" class = "link {{#if inPopup}}close-popup{{else}}back{{/if}}">
<i class = "icon icon-back"></i>
{{#if inPopup}}
<span>{{popupCloseText}}</span>
{{else}}
<span>{{backText}}</span>
{{/if}}
</a>
{{/if}}
</div>
<div class = "center sliding">{{pageTitle}}</div>
{{#if preloader}}
<div class = "right">
<div class = "autocomplete-preloader preloader
{{#if preloaderColor}}
preloader-{{preloaderColor}}
{{/if}}">
</div>
</div>
{{/if}}
</div>
</div>
项目模板
<li>
<label class = "label-{{inputType}} item-content">
<input type = "{{inputType}}" name = "{{inputName}}" value = "{{value}}" {{#if selected}}checked{{/if}}>
{{#if material}}
<div class = "item-media">
<i class = "icon icon-form-{{inputType}}"></i>
</div>
<div class = "item-inner">
<div class = "item-title">{{text}}</div>
</div>
{{else}}
{{#if checkbox}}
<div class = "item-media">
<i class = "icon icon-form-checkbox"></i>
</div>
{{/if}}
<div class = "item-inner">
<div class = "item-title">{{text}}</div>
</div>
{{/if}}
</label>
</li>
下拉模板
<div class = "autocomplete-dropdown">
<div class = "autocomplete-dropdown-inner">
<div class = "list-block">
<ul></ul>
</div>
</div>
{{#if preloader}}
<div class = "autocomplete-preloader preloader
{{#if preloaderColor}}
preloader-{{preloaderColor}}
{{/if}}">
{{#if material}}
{{materialPreloaderHtml}}
{{/if}}
</div>
{{/if}}
</div>
下拉项目模板
<li>
<label class = "{{#unless placeholder}}label-radio{{/unless}} item-content" data-value = "{{value}}">
<div class = "item-inner">
<div class = "item-title">{{text}}</div>
</div>
</label>
</li>
dropdown占位符模板
<li class = "autocomplete-dropdown-placeholder">
<div class = "item-content">
<div class = "item-inner">
<div class = "item-title">{{text}}</div>
</div>
</label>
</li>
自动完成方法
下表指定了 Framework7 中可用的自动完成方法 -
| 序列号 | 方法与说明 |
|---|---|
| 1 | myAutocomplete.params 定义随对象传递的初始化参数。 |
| 2 | myAutocomplete.value 它定义具有选定值的数组。 |
| 3 | myAutocomplete.opened 如果设置为 true,则会打开“自动完成”。 |
| 4 | myAutocomplete.dropdown 它指定自动完成下拉列表的实例。 |
| 5 | myAutocomplete.popup 它指定自动完成弹出窗口的实例。 |
| 6 | myAutocomplete.page 它指定自动完成页面的实例。 |
| 7 | myAutocomplete.pageData 它定义自动完成页面数据。 |
| 8 | myAutocomplete.searchbar 它定义自动完成搜索栏实例。 |
自动完成属性
下表指定了 Framework7 中可用的自动完成方法 -
| 序列号 | 属性及说明 |
|---|---|
| 1 | myAutocomplete.open() 它会打开自动完成功能,可用作下拉菜单、弹出菜单或页面。 |
| 2 | myAutocomplete.close() 它关闭自动完成。 |
| 3 | myAutocomplete.showPreloader() 它显示自动完成预加载器。 |
| 4 | myAutocomplete.hidePreloader() 它隐藏了自动完成预加载器。 |
| 5 | myAutocomplete.destroy() 它会破坏自动完成预加载器实例并删除所有事件。 |
例子
以下示例演示了隐藏在 Framework7 中的自动完成参数的使用 -
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1,
maximum-scale = 1, minimum-scale = 1, user-scalable = no, minimal-ui" />
<meta name = "apple-mobile-web-app-capable" content = "yes" />
<meta name = "apple-mobile-web-app-status-bar-style" content = "black" />
<title>Autocomplete</title>
<link rel = "stylesheet"
href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.min.css" />
<link rel = "stylesheet"
href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.colors.min.css" />
</head>
<body>
<div class = "views">
<div class = "view view-main">
<div class = "pages">
<div data-page = "home" class = "page navbar-fixed">
<div class = "navbar">
<div class = "navbar-inner">
<div class = "left"> </div>
<div class = "center">Autcomplete</div>
<div class = "right"> </div>
</div>
</div>
<div class = "page-content">
<div class = "content-block-title">Simple Dropdown Autocomplete</div>
<div class = "list-block">
<ul>
<li class = "item-content">
<div class = "item-title label">Country</div>
<div class = "item-input">
<input type = "text" placeholder = "Country" id = "autocomplete-dropdown">
</div>
</li>
</ul>
</div>
<div class = "content-block-title">Dropdown With Input Expand</div>
<div class = "list-block">
<ul>
<li class = "item-content">
<div class = "item-title label">Country</div>
<div class = "item-input">
<input type = "text" placeholder = "Country" id = "autocomplete-dropdown-expand">
</div>
</li>
</ul>
</div>
<div class = "content-block-title">Dropdown With All Values</div>
<div class = "list-block">
<ul>
<li class = "item-content">
<div class = "item-title label">Country</div>
<div class = "item-input">
<input type = "text" placeholder = "Country" id = "autocomplete-dropdown-all">
</div>
</li>
</ul>
</div>
<div class = "content-block-title">Dropdown With Placeholder</div>
<div class = "list-block">
<ul>
<li class = "item-content">
<div class = "item-title label">Country</div>
<div class = "item-input">
<input type = "text" placeholder = "Country" id = "autocomplete-dropdown-placeholder">
</div>
</li>
</ul>
</div>
<div class = "content-block-title">Simple Standalone Autocomplete</div>
<div class = "list-block">
<ul>
<li>
<a href = "#" id = "autocomplete-standalone" class = "item-link item-content autocomplete-opener">
<input type = "hidden">
<div class = "item-inner">
<div class = "item-title">Favorite Country</div>
<div class = "item-after"></div>
</div>
</a>
</li>
</ul>
</div>
<div class = "content-block-title">Popup Standalone Autocomplete</div>
<div class = "list-block">
<ul>
<li>
<a href = "#" id = "autocomplete-standalone-popup" class = "item-link item-content autocomplete-opener">
<input type = "hidden">
<div class = "item-inner">
<div class = "item-title">Favorite Country</div>
<div class = "item-after"></div>
</div>
</a>
</li>
</ul>
</div>
<div class = "content-block-title">Multiple Values Standalone Autocomplete</div>
<div class = "list-block">
<ul>
<li>
<a href = "#" id = "autocomplete-standalone-multiple" class = "item-link item-content autocomplete-opener">
<input type = "hidden">
<div class = "item-inner">
<div class = "item-title">Favorite Countries</div>
<div class = "item-after"></div>
</div>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script type = "text/javascript"
src = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/js/framework7.min.js"></script>
<script>
var myApp = new Framework7();
var $$ = Dom7;
var mainView = myApp.addView('.view-main');
// Countries data array
var countries = ('India Africa Australia NewZealand England WestIndies Scotland Zimbabwe Srilanka Bangladesh').split(' ');
// Simple Dropdown
var autocompleteDropdownSimple = myApp.autocomplete ({
input: '#autocomplete-dropdown',
openIn: 'dropdown',
source: function (autocomplete, query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// You can find matched items
for (var i = 0; i < countries.length; i++) {
if (countries[i].toLowerCase().indexOf(query.toLowerCase()) >= 0)
results.push(countries[i]);
}
// Display the items by passing array with result items
render(results);
}
});
// Dropdown with input expand
var autocompleteDropdownExpand = myApp.autocomplete ({
input: '#autocomplete-dropdown-expand',
openIn: 'dropdown',
expandInput: true, // expandInput used as item-input in List View will be expanded to full screen wide
//during dropdown
source: function (autocomplete, query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Find matched items
for (var i = 0; i < countries.length; i++) {
if (countries[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(countries[i]);
}
// Display the items by passing array with result items
render(results);
}
});
// Dropdown with all values
var autocompleteDropdownAll = myApp.autocomplete ({
input: '#autocomplete-dropdown-all',
openIn: 'dropdown',
source: function (autocomplete, query, render) {
var results = [];
// You can find matched items
for (var i = 0; i < countries.length; i++) {
if (countries[i].toLowerCase().indexOf(query.toLowerCase()) >= 0)
results.push(countries[i]);
}
// Display the items by passing array with result items
render(results);
}
});
// Dropdown with placeholder
var autocompleteDropdownPlaceholder = myApp.autocomplete ({
input: '#autocomplete-dropdown-placeholder',
openIn: 'dropdown',
dropdownPlaceholderText: 'Type as "India"',
source: function (autocomplete, query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// You can find matched items
for (var i = 0; i < countries.length; i++) {
if (countries[i].toLowerCase().indexOf(query.toLowerCase()) >= 0)
results.push(countries[i]);
}
// Display the items by passing array with result items
render(results);
}
});
// Simple Standalone
var autocompleteStandaloneSimple = myApp.autocomplete ({
openIn: 'page', //open in page
opener: $$('#autocomplete-standalone'), //link that opens autocomplete
backOnSelect: true, //go back after we select something
source: function (autocomplete, query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// You can find matched items
for (var i = 0; i < countries.length; i++) {
if (countries[i].toLowerCase().indexOf(query.toLowerCase()) >= 0)
results.push(countries[i]);
}
// Display the items by passing array with result items
render(results);
},
onChange: function (autocomplete, value) {
// Here add the item text value to item-after
$$('#autocomplete-standalone').find('.item-after').text(value[0]);
// You can add item value to input value
$$('#autocomplete-standalone').find('input').val(value[0]);
}
});
// Standalone Popup
var autocompleteStandalonePopup = myApp.autocomplete ({
openIn: 'popup', // Opens the Autocomplete in page
opener: $$('#autocomplete-standalone-popup'), // It will open standalone autocomplete popup
backOnSelect: true, //After selecting item, then go back to page
source: function (autocomplete, query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// You can find matched items
for (var i = 0; i < countries.length; i++) {
if (countries[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(countries[i]);
}
// Display the items by passing array with result items
render(results);
},
onChange: function (autocomplete, value) {
// Here add the item text value to item-after
$$('#autocomplete-standalone-popup').find('.item-after').text(value[0]);
// You can add item value to input value
$$('#autocomplete-standalone-popup').find('input').val(value[0]);
}
});
// Multiple Standalone
var autocompleteStandaloneMultiple = myApp.autocomplete ({
openIn: 'page', //Opens the Autocomplete in page
opener: $$('#autocomplete-standalone-multiple'), //link that opens autocomplete
multiple: true, //Allow multiple values
source: function (autocomplete, query, render) {
var results = [];
if (query.length === 0) {
render(results);
return;
}
// You can find matched items
for (var i = 0; i < countries.length; i++) {
if (countries[i].toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(countries[i]);
}
// Display the items by passing array with result items
render(results);
},
onChange: function (autocomplete, value) {
// Here add the item text value to item-after
$$('#autocomplete-standalone-multiple').find('.item-after').text(value.join(', '));
// You can add item value to input value
$$('#autocomplete-standalone-multiple').find('input').val(value.join(', '));
}
});
</script>
</body>
</html>
输出
让我们执行以下步骤来看看上面给出的代码是如何工作的 -
将上面给定的 HTML 代码保存为服务器根文件夹中的autocomplete.html文件。
以 http://localhost/autocomplete.html 打开此 HTML 文件,输出如下所示。
该示例提供简单下拉列表中的值自动完成、包含所有值的下拉列表、带占位符的下拉列表、独立自动完成等。