智能选择
智能选择允许您轻松地将常规表单选择转换为具有分组单选或复选框输入的动态页面。您可以在许多原生iOS应用程序中看到此功能
智能选择器布局
智能选择布局相当简单。它是一个众所周知的列表视图链接,带有<select>
内部,以及额外的smart-select
类在item-link
:
<div class="list">
<ul>
<!-- Smart select item -->
<li>
<!-- Additional "smart-select" class -->
<span class="item-link smart-select">
<!-- select -->
<select name="fruits">
<option value="apple" selected>Apple</option>
<option value="pineapple">Pineapple</option>
...
</select>
<div class="item-content">
<div class="item-inner">
<!-- Select label -->
<div class="item-title">Fruit</div>
<!-- Selected value, not required -->
<div class="item-after">Apple</div>
</div>
</div>
</span>
</li>
<!-- Another smart selects or list view elements -->
...
</ul>
</div>
注意智能选择仅在初始化的视图中使用,因为它使用路由来加载智能选择页面或打开模态!
自定义选项图标
我们可以通过在data-option-icon
元素上的属性来指定智能选择页面的列表视图(选项)图标:<option>
elements:
data-option-icon
- 如果它只是一个字符串,则将创建一个具有此类的图标。如果它是f7:icon_name
格式,则将创建F7-Icons图标。如果是md:icon_name
格式,则将创建Material Icons图标data-option-icon-ios
- 与data-option-icon
但仅在iOS主题激活时应用data-option-icon-md
- 与data-option-icon
但仅在MD主题激活时应用
例如:
<option data-option-icon="my-icon">
将产生<i class="icon my-icon"></i>
列表项图标<option data-option-icon="f7:house_fill">
将产生<i class="icon f7-icons">house_fill</i>
列表项图标<option data-option-icon="material:home">
将产生<i class="icon material-icons">home</i>
列表项图标<option data-option-icon-ios="f7:house" data-option-icon-md="material:home">
将产生:<i class="icon f7-icons">house</i>
- 当iOS主题激活时<i class="icon material-icons">home</i>
- 当MD主题激活时
自定义选项颜色和图像
我们还可以指定智能选择页面的列表视图元素的图像或颜色。我们需要在智能选择data-option-image
上使用额外的属性(以设置所有选项的默认图像)或在上<select>
(to set default image for all options) or on <option>
上使用属性以在单个选项上设置图像或图标。
对于单个选项,我们也可以使用data-option-color
和data-option-class
属性来添加特定的选项颜色或CSS类以进行附加样式。
<li>
<span class="item-link smart-select">
<select name="fruits">
<option value="apple" selected data-option-image="https://cdn.framework7.io/placeholder/abstract-29x29-1.jpg">Apple</option>
<option value="pineapple" data-option-image="https://cdn.framework7.io/placeholder/abstract-29x29-2.jpg">Pineapple</option>
<option value="pear" data-option-color="orange" data-option-image="https://cdn.framework7.io/placeholder/abstract-29x29-3.jpg">Pear</option>
...
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Fruit</div>
</div>
</div>
</span>
</li>
多重选择和<optgroup>
我们还可以使用<optgroup>
进行多重选择和分组选项。因此,如果我们向我们的选择添加multiple
属性,则智能选择页面上的单选按钮将自动转换为复选框:
<li>
<span class="item-link smart-select">
<!-- "multiple" attribute for multiple select-->
<select name="car" multiple>
<!-- options grouped within "optgroup" tag-->
<optgroup label="Japanese">
<option value="honda" selected>Honda</option>
<option value="lexus">Lexus</option>
...
</optgroup>
<optgroup label="German">
<option value="audi" selected>Audi</option>
<option value="bmw">BMW</option>
...
</optgroup>
<optgroup label="American">
<option value="cadillac">Cadillac</option>
<option value="chrysler">Chrysler</option>
...
</optgroup>
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Car</div>
</div>
</div>
</span>
</li>
多重选择和maxlength
使用多重选择,我们还可以使用maxlength属性来限制可能的选定项数:
<li>
<span class="item-link smart-select">
<!-- "maxlength" attribute to limit number of possible selected items -->
<!-- we won't allow to select more than 3 items -->
<select name="car" multiple maxlength="3">
<optgroup label="Japanese">
<option value="honda" selected>Honda</option>
<option value="lexus">Lexus</option>
...
</optgroup>
<optgroup label="German">
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
...
</optgroup>
<optgroup label="American">
<option value="cadillac">Cadillac</option>
<option value="chrysler">Chrysler</option>
...
</optgroup>
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Car</div>
</div>
</div>
</span>
</li>
不同的显示值
使用data-display-as
属性在选项上,我们可以以不同的方式显示选定值:
<li>
<span class="item-link smart-select">
<select name="days">
<option value="monday" selected data-display-as="Mon">Monday</option>
<option value="tuesday" data-display-as="Tue">Tuesday</option>
<option value="wednesday" data-display-as="Wed">Wednesday</option>
<option value="thursday" data-display-as="Thu">Thursday</option>
<option value="friday" data-display-as="Fri">Friday</option>
<option value="saturday" data-display-as="Sat">Saturday</option>
<option value="sunday" data-display-as="Sun">Sunday</option>
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Day</div>
</div>
</div>
</span>
</li>
智能选择器应用方法
让我们看看与智能选择相关的方法:
app.smartSelect.create(参数)- 创建智能选择实例
- 参数 - 对象. 智能选择参数对象
方法返回创建的智能选择的实例
app.smartSelect.destroy(smartSelectEl)- 销毁智能选择实例
- smartSelectEl - HTMLElement或字符串(使用 CSS 选择器) 或对象. 要销毁的智能选择元素或智能选择实例。
app.smartSelect.get(smartSelectEl)- 通过HTML元素获取智能选择实例
- smartSelectEl - HTMLElement或字符串(使用CSS选择器)。智能选择元素。
方法返回智能选择的实例
app.smartSelect.open(smartSelectEl)- 打开智能选择
- smartSelectEl - HTMLElement或字符串(使用CSS选择器)。要打开的智能选择元素。
方法返回智能选择的实例
app.smartSelect.close(smartSelectEl)- 关闭智能选择
- smartSelectEl - HTMLElement或字符串(使用CSS选择器)。要关闭的智能选择元素。
方法返回智能选择的实例
智能选择参数
现在让我们看看创建智能选择时需要的一组可用参数:
参数 | 类型 | 默认 | 描述 |
---|---|---|---|
el | HTMLElement | 智能选择元素。如果您已经在HTML中拥有智能选择元素并希望使用此元素创建新实例,则可能很有用 | |
view | 对象 | 初始化的视图实例的链接,这是智能选择正常工作所必需的。默认情况下,如果未指定,它将在父视图中打开。 | |
valueEl | HTMLElement | 要插入选定值的视觉元素。如果未传递,则它将查找<div class="item-after"> 元素之前插入元素 | |
setValueText | 布尔值 | true | 当启用时,智能选择将自动将值文本插入指定的“valueEl”中,格式由“formatValueText”返回 |
formatValueText | function(values) | 自定义函数来格式化智能选择文本值,该值显示在列表项(在<div class="item-after"> )中values 是当前值的数组 | |
openIn | 字符串 | page | 定义如何打开智能选择。可以是page 或popup 或popover 或sheet |
popupPush | 布尔值 | false | 启用智能选择弹出窗口在打开时将视图/推到后面 |
popupSwipeToClose | 布尔值 | undefined | 启用使用滑动手势关闭智能选择弹出窗口的能力。如果未指定,则继承应用的PopupswipeToClose parameter |
sheetPush | 布尔值 | false | 启用智能选择表在打开时将视图/推到后面 |
sheetSwipeToClose | 布尔值 | undefined | 启用使用滑动手势关闭智能选择表的能力。如果未指定,则继承应用的SheetswipeToClose parameter |
sheetBackdrop | 布尔值 | false | 启用智能选择表背景 |
pageTitle | 字符串 | 智能选择页面标题。如果未传递,则它将是<div class="item-title"> text | |
pageBackLinkText | 字符串 | Back | 智能选择页面后退链接文本 |
popupCloseLinkText | 字符串 | Close | 智能选择弹出窗口关闭链接文本 |
popupTabletFullscreen | 布尔值 | false | 当启用时,智能选择弹出窗口将在平板电脑上以全屏方式显示 |
sheetCloseLinkText | 字符串 | 完成 | 智能选择表关闭链接文本 |
searchbar | 布尔值 对象 | false | 在智能选择页面上启用搜索栏。如果作为对象传递,则应该是有效的搜索栏参数 |
searchbarPlaceholder | 字符串 | 搜索 | 搜索栏占位符文本 |
searchbarDisableText | 字符串 | 取消 | 搜索栏“取消”链接文本。仅在iOS主题中有效 |
searchbarSpellcheck | 布尔值 | false | 设置搜索栏输入元素的spellcheck 属性的值 |
appendSearchbarNotFound | 布尔值 字符串 HTMLElement | false | 添加包含内容块,当没有搜索栏结果时显示。 如果指定为字符串,则将添加:
如果指定为
如果 |
closeOnSelect | 布尔值 | false | 如果启用,则智能选择将在用户选择任何选项后自动关闭 |
virtualList | 布尔值 | false | 如果您的选择有很多(数百、数千)选项,请为智能选择启用虚拟列表 |
virtualListHeight | 数字 函数 | 虚拟列表项高度。如果number - 列表项高度(像素)。如果function ,则函数应返回项高度。 | |
scrollToSelectedItem | 布尔值 | false | 当启用时,它将在打开时滚动智能选择内容到第一个选定的项 |
formColorTheme | 字符串 | 智能选择页面的表单颜色主题。之一是默认颜色之一 | |
navbarColorTheme | 字符串 | 智能选择导航栏颜色主题。之一是默认颜色之一 | |
routableModals | 布尔值 | false | 将打开的智能选择模态框(当openIn 是popup , popover 或sheet )添加到路由历史中,这提供了通过在路由历史中后退来关闭智能选择的能力,并将当前路由设置为智能选择模态框。 |
url | 字符串 | select/ | 智能选择页面/模态框URL,将被设置为当前路由 |
cssClass | 字符串 | 要附加到智能选择容器的额外CSS类名(页面、弹出窗口、弹出层或工作表) | |
closeByBackdropClick | 布尔值 | true | 当启用时,智能选择模态框(弹出层、弹出窗口、工作表)将在背景点击时关闭 |
inputIconPosition | 字符串 | 'start' | 选择选项复选框或单选按钮图标位置。可以是'start' 或'end' 。也可以通过data-input-icon-position 属性 |
optionIcon | 字符串 | 要设置在所有选项上的选择选项图标。如果是字符串,则将创建具有此类的图标。如果是f7:icon_name 格式,则将创建F7-Icons图标。如果是md:icon_name 格式,则将创建Material Icons图标 | |
optionIconIos | 字符串 | 与optionIcon 但仅在iOS主题激活时应用 | |
optionIconMd | 字符串 | 与optionIcon 但仅在MD主题激活时应用 | |
渲染函数 | |||
renderSearchbar | 函数 | 用于渲染智能选择搜索栏下拉菜单的函数,必须返回搜索栏HTML字符串 | |
renderItem | function(item, index) | 用于渲染智能选择项的函数,必须返回项HTML字符串 | |
renderItems | function(items) | 用于渲染所有智能选择项的函数,必须返回所有项HTML字符串 | |
renderPage | function(items) | 用于渲染智能选择页面的函数,必须返回完整页面HTML字符串 | |
renderPopup | function(items) | 用于渲染智能选择弹出窗口的函数,必须返回完整弹出窗口HTML字符串 | |
renderSheet | function(items) | 用于渲染智能选择工作表的函数,必须返回完整工作表HTML字符串 | |
renderPopover | function(items) | 用于渲染智能选择弹出层的函数,必须返回完整弹出层HTML字符串 | |
事件 | |||
on | 对象 | 带有事件处理程序的对象。例如:
|
注意,所有以下参数都可以在smartSelect
用于为所有智能选择设置默认值的属性。例如:
var app = new Framework7({
smartSelect: {
pageTitle: 'Select Option',
openIn: 'popup',
}
});
智能选择器方法和属性
因此,要创建智能选择,我们必须调用:
var smartSelect = app.smartSelect.create({ /* parameters */ })
之后我们就有其初始化的实例(如smartSelect
示例中上面的变量)以及有用的方法和属性:
属性 | |
---|---|
smartSelect.app | 链接到全局应用实例 |
smartSelect.el | 智能选择HTML元素 |
smartSelect.$el | 具有智能选择HTML元素的Dom7实例 |
smartSelect.valueEl | 用于显示值的HTML元素 |
smartSelect.$valueEl | 具有用于显示值的HTML元素的Dom7实例 |
smartSelect.selectEl | 子选择元素<select> |
smartSelect.$selectEl | 具有子选择元素的Dom7实例 |
smartSelect.url | 智能选择URL(在url 参数中) |
smartSelect.view | 智能选择视图(在view 参数中)或找到的父视图 |
smartSelect.params | 智能选择参数 |
方法 | |
smartSelect.open() | 打开智能选择 |
smartSelect.close() | 关闭智能选择 |
smartSelect.getValue() | 返回智能选择值。在选择是multiple 时,它返回一个包含选定值的数组 |
smartSelect.setValue(newValue) | 设置新的智能选择值。在选择是multiple 时,它必须是一个包含新值的数组 |
smartSelect.unsetValue() | 清除智能选择值 |
smartSelect.scrollToSelectedItem() | 将智能选择内容滚动到第一个选定项(当打开时) |
smartSelect.destroy() | 销毁智能选择 |
smartSelect.on(事件上触发, 处理程序) | 添加事件处理程序 |
smartSelect.once(事件上触发, 处理程序) | 添加在触发后将被移除的事件处理程序 |
smartSelect.off(事件上触发, 处理程序) | 移除事件处理程序 |
smartSelect.off(事件上触发) | 移除指定事件的所有处理程序 |
smartSelect.emit(事件上触发, ...args) | 在实例上触发事件 |
智能选择器事件
智能选择将在智能选择元素上触发以下DOM事件,并在应用和智能选择实例上触发事件:
DOM 事件
事件 | Target | 描述 |
---|---|---|
smartselect:beforeopen | 智能选择元素<a class="item-link smart-select"> | 在智能选择打开之前将触发此事件。event.detail.prevent 包含一个函数,当调用时将阻止智能选择打开 |
smartselect:open | 智能选择元素<a class="item-link smart-select"> | 当智能选择页面(或模态框)开始其打开动画时将触发此事件 |
smartselect:opened | 智能选择元素<a class="item-link smart-select"> | 在智能选择页面(或模态框)完成其打开动画后将触发此事件 |
smartselect:close | 智能选择元素<a class="item-link smart-select"> | 当智能选择页面(或模态框)开始其关闭动画时将触发此事件 |
smartselect:closed | 智能选择元素<a class="item-link smart-select"> | 在智能选择页面(或模态框)完成其关闭动画后将触发此事件 |
smartselect:beforedestroy | 智能选择元素<a class="item-link smart-select"> | 在智能选择实例将被销毁之前将触发此事件 |
应用和智能选择器实例事件
智能选择实例在自身实例和应用程序实例上触发事件。应用程序实例事件具有相同名称,前缀为smartSelect
.
事件 | Target | 参数 | 描述 |
---|---|---|---|
beforeOpen | smartSelect | (smartSelect, prevent) | 在智能选择打开之前将触发此事件。作为参数,事件处理程序接收智能选择实例和当调用时将阻止智能选择打开的函数 |
smartSelectBeforeOpen | app | ||
open为前缀 | smartSelect | (smartSelect) | 当智能选择开始其打开动画时将触发此事件。作为参数,事件处理程序接收智能选择实例 |
smartSelectOpen | app | ||
opened | smartSelect | (smartSelect) | 在智能选择完成其打开动画后将触发此事件。作为参数,事件处理程序接收智能选择实例 |
smartSelectOpened | app | ||
close | smartSelect | (smartSelect) | 当智能选择开始其关闭动画时将触发此事件。作为参数,事件处理程序接收智能选择实例 |
smartSelectClose | app | ||
closed | smartSelect | (smartSelect) | 在智能选择完成其关闭动画后将触发此事件。作为参数,事件处理程序接收智能选择实例 |
smartSelectClosed | app | ||
beforeDestroy | smartSelect | (smartSelect) | 在智能选择实例将被销毁之前将触发此事件。作为参数,事件处理程序接收智能选择实例 |
智能选择器销毁前 | app |
智能选择器自动初始化
如果你不需要使用智能选择器API,并且你的智能选择器在页面内,并且在页面初始化时在DOM中呈现,那么它可以通过仅添加额外的smart-select-init
class:
<li>
<!-- Add smart-select-init class -->
<span class="item-link smart-select smart-select-init">
<!-- select -->
<select name="fruits">
<option value="apple" selected>Apple</option>
<option value="pineapple">Pineapple</option>
...
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Fruit</div>
<div class="item-after">Apple</div>
</div>
</div>
</span>
</li>
在这种情况下,如果你需要访问创建的智能选择器实例,你可以使用app.smartSelect.get
app method:
var smartSelect = app.smartSelect.get('.smart-select');
在使用自动初始化时,你可能需要传递额外的参数。在这种情况下,你可以通过智能选择器元素的data-
属性传递所有额外的参数:
<li>
<!-- Pass parameters as kebab-case data attributes -->
<span class="item-link smart-select smart-select-init" data-open-in="popup" data-virtual-list="true" data-page-back-link-text="Go back">
<!-- select -->
<select name="fruits">
<option value="apple" selected>Apple</option>
<option value="pineapple">Pineapple</option>
...
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Fruit</div>
<div class="item-after">Apple</div>
</div>
</div>
</span>
</li>
CSS 变量
以下是相关CSS 变量(CSS 自定义属性) 的列表。
注意,注释掉的变量默认未指定,其值在这种情况下是它们回退到的值。
:root {
/*
--f7-smart-select-sheet-bg: var(--f7-list-bg-color);
--f7-smart-select-sheet-toolbar-border-color: var(--f7-bars-border-color);
*/
}
示例
<div class="page">
<div class="navbar">
<div class="navbar-bg"></div>
<div class="navbar-inner sliding">
<div class="title">Smart Select</div>
</div>
</div>
<div class="page-content">
<div class="block">
Framework7 allows you to easily convert your usual form selects to dynamic pages with radios:
</div>
<div class="list list-strong-ios list-outline-ios list-dividers-ios">
<ul>
<li>
<a class="item-link smart-select smart-select-init">
<select name="fruits">
<option value="apple" selected>Apple</option>
<option value="pineapple">Pineapple</option>
<option value="pear">Pear</option>
<option value="orange">Orange</option>
<option value="melon">Melon</option>
<option value="peach">Peach</option>
<option value="banana">Banana</option>
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Fruit</div>
</div>
</div>
</a>
</li>
<li>
<a class="item-link smart-select smart-select-init" data-open-in="popup" data-searchbar="true"
data-searchbar-placeholder="Search car">
<select name="car" multiple>
<optgroup label="Japanese">
<option value="honda" selected>Honda</option>
<option value="lexus">Lexus</option>
<option value="mazda">Mazda</option>
<option value="nissan">Nissan</option>
<option value="toyota">Toyota</option>
</optgroup>
<optgroup label="German">
<option value="audi" selected>Audi</option>
<option value="bmw">BMW</option>
<option value="mercedes">Mercedes</option>
<option value="vw">Volkswagen</option>
<option value="volvo">Volvo</option>
</optgroup>
<optgroup label="American">
<option value="cadillac">Cadillac</option>
<option value="chrysler">Chrysler</option>
<option value="dodge">Dodge</option>
<option value="ford" selected>Ford</option>
</optgroup>
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Car</div>
</div>
</div>
</a>
</li>
<li>
<a class="item-link smart-select smart-select-init" data-open-in="sheet">
<select name="mac-windows">
<option value="mac" selected>Mac</option>
<option value="windows">Windows</option>
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Mac or Windows</div>
</div>
</div>
</a>
</li>
<li>
<a class="item-link smart-select smart-select-init" data-open-in="popover">
<select name="superhero" multiple>
<option value="Batman" selected>Batman</option>
<option value="Superman">Superman</option>
<option value="Hulk">Hulk</option>
<option value="Spiderman">Spiderman</option>
<option value="Ironman">Ironman</option>
<option value="Thor">Thor</option>
<option value="Wonder Woman">Wonder Woman</option>
</select>
<div class="item-content">
<div class="item-inner">
<div class="item-title">Super Hero</div>
</div>
</div>
</a>
</li>
</ul>
</div>
</div>
</div>