`

移动端安卓和IOS开发框架Framework7教程-搜索栏

阅读更多

搜索栏允许用户在列表元素中搜索,或者它可以用来作为UI组件,放到你自己的搜索实现中。

搜索栏布局

搜索栏应该放到“.page”内,“.page-content”前:

  1. <div class="page">
  2.     <!-- Search bar -->
  3.     <form class="searchbar">
  4.         <div class="searchbar-input">
  5.             <input type="search" placeholder="Search">
  6.             <a href="#" class="searchbar-clear"></a>
  7.         </div>
  8.         <a href="#" class="searchbar-cancel">Cancel</a>
  9.     </form>
  10.     
  11.     <!-- Search bar overlay-->
  12.     <div class="searchbar-overlay"></div>
  13.     
  14.     <!-- Page content -->
  15.     <div class="page-content">
  16.         <div class="content-block searchbar-not-found">
  17.                 Nothing found
  18.         </div>
  19.  
  20.         <div class="list-block list-block-search searchbar-found">
  21.             <ul>
  22.                 ... list view items ...
  23.             </ul>
  24.         </div>
  25.     </div>
  26. </div>
复制

其中:

  • form class="searchbar" - 主要的搜索栏容器。它不必是个form元素,但推荐如此。
    • div class="searchbar-input" - 搜索区域和取消按钮的容器
      • input type="search" - search field
      • a class="searchbar-clear" - button to clear field value and reset search results. Optional element
    • a class="searchbar-cancel" - searchbar Cancel button that will deactivate searchbar, reset search results and clear search field. Optional element
  • div class="searchbar-overlay" - Add this element right after search bar to enable dark overlay over page content when search bar is active. Optional element.
  • "list-block-search" - list block where we are going to search elements.
  • "searchbar-found" - element with this class will be displayed if searchbar find elements that match to search query. Visible by default. Optional element.
  • "searchbar-not-found" - element with this class will be displayed if searchbar doesn't find elements that match to search query. Hidden by default. Optional element.

Initialize Searchbar with JavaScript

Now, when we have Searchbar' HTML, we need to initialize it. We need to use related App's method:

myApp.searchbar(searchbarContainer, parameters) - initialize searchbar with options

  • searchbarContainer - HTMLElement or string (with CSS Selector) of Searchbar container HTML element. Required.
  • parameters - object - object with Searchbar parameters. Optional.
  • Method returns initialized Searchbar instance

For example:

  1. var mySearchbar = app.searchbar('.searchbar', {
  2.     searchList: '.list-block-search',
  3.     searchIn: '.item-title'
  4. });
复制
Note that Searchbar container should be in DOM on a moment of initialization. So if you use it not on home page, you need to initialize it within pageInit event or callback

Searchbar Parameters

Let's look on list of all available parameters:

Parameter Type Default Description Callbacks
searchList string or HTMLElement   CSS selector or HTML element of list block to search
searchIn string '.item-title' CSS selector of List View element's field where we need to search. Usually we search through element titles ('.item-title'). It is also to pass few elements for search like '.item-title, .item-text'
found string or HTMLElement   CSS selector or HTMLElement of searchbar "found" element. If not specified, searchbar will look for .searchbar-found element on page
notFoud string or HTMLElement   CSS selector or HTMLElement of searchbar "not-found" element. If not specified, searchbar will look for .searchbar-not-found element on page
overlay string or HTMLElement   CSS selector or HTMLElement of searchbar overlay. If not specified, searchbar will look for .searchbar-overlay element on page
ignore string '.searchbar-ignore' CSS selector for items to be ignored by searchbar and always present in search results
customSearch boolean false When enabled searchbar will not search through any of list blocks specified by `searchList` and you will be able to use custom search functionality, for example, for calling external APIs with search results and for displaying them manually
removeDiacritics boolean false Enable to remove/replace diacritics (á, í, ó, etc.) during search
hideDividers boolean true If enabled, then search will consider item dividers and group titles and hide them if there are no found items right after them
hideGroups boolean true If enabled, then search will consider list view groups hide them if there are no found items inside of these groups
onSearch function (s)   Callback function will be triggered during search (search field change).
onEnable function (s)   Callback will be triggered when Search Bar becomes active
onDisable function (s)   Callback will be triggered when Search Bar becomes inactive - when user clicks on "Cancel" button or on "searchbar-overlay" element
onClear function (s)   Callback will be triggered when user clicks on Search Bar's "clear" element

Searchbar Methods & Properties

After we initialize Searchbar we have its initialized instance in variable (like mySearchbar variable in example above) with helpful methods and properties:

PropertiesMethods
mySearchbar.params Object with passed initialization parameters
mySearchbar.query Current search query (search input value)
mySearchbar.searchList Dom7 element with search list block.
mySearchbar.container Dom7 element with searchbar container HTML element.
mySearchbar.input Dom7 element with searchbar input HTML element
mySearchbar.active Boolean value that represents is searchbar enabled or disabled
mySearchbar.search(query); Force searchbar to search passed query
mySearchbar.enable(); Enable/activate searchbar
mySearchbar.disable(); Disable/deactivate searchbar
mySearchbar.clear(); Clear search query and update results
mySearchbar.destroy(); Destroy searchbar instance

Initialize Searchbar with HTML

If you don't need to use Searchbar methods and properties you can initialize it using HTML without JavaScript. You can do that just by adding additional "searchbar-init" class to .searchbar. In this case we may pass required parameters using data- attributes.

  1. <div class="page">
  2.     <!-- Search bar with "searchbar-init" class for auto initialization -->
  3.     <form class="searchbar searchbar-init" data-search-list=".list-block-search" data-search-in=".item-title" data-found=".searchbar-found" data-not-found=".searchbar-not-found">
  4.         <div class="searchbar-input">
  5.             <input type="search" placeholder="Search">
  6.             <a href="#" class="searchbar-clear"></a>
  7.         </div>
  8.         <a href="#" class="searchbar-cancel">Cancel</a>
  9.     </form>
  10.     
  11.     <div class="searchbar-overlay"></div>
  12.     
  13.     <div class="page-content">
  14.         <div class="content-block searchbar-not-found">
  15.                 Nothing found
  16.         </div>
  17.  
  18.         <div class="list-block list-block-search searchbar-found">
  19.             <ul>
  20.                 ... list view items ...
  21.             </ul>
  22.         </div>
  23.     </div>
  24. </div>
复制

Parameters that used in camelCase, for example searchList, in data- attributes should be used as hypens-case as data-search-list

Access to Searchbar's Instance

If you initialize Searchbar using HTML it is still possible to access to Searchbar's instance. It is "f7Searchbar" property of searchbar's container HTML element:

  1. var mySearchbar = $$('.searchbar')[0].f7Searchbar;
  2.  
  3. // Now you can use it
  4. mySearchbar.search('Hello world');
复制
 

Search Bar Example

In this example we use searchbar with auto initialization

  1. <div data-page="home" class="page">
  2.   <!-- Search bar -->
  3.   <form data-search-list=".list-block-search" data-search-in=".item-title" class="searchbar searchbar-init">
  4.     <div class="searchbar-input">
  5.       <input type="search" placeholder="Search"><a href="#" class="searchbar-clear"></a>
  6.     </div><a href="#" class="searchbar-cancel">Cancel</a>
  7.   </form>
  8.  
  9.   <!-- Search bar overlay -->
  10.   <div class="searchbar-overlay"></div>
  11.  
  12.   <div class="page-content">
  13.     <!-- This block will be displayed if nothing found -->
  14.     <div class="content-block searchbar-not-found">
  15.       <div class="content-block-inner">Nothing found</div>
  16.     </div>
  17.  
  18.     <!-- This block will be displayed if anything found, and this list block is used a searbar target -->
  19.     <div class="list-block list-block-search searchbar-found">
  20.       <ul>
  21.         <li class="item-content">
  22.           <div class="item-inner">
  23.             <div class="item-title">Acura </div>
  24.           </div>
  25.         </li>
  26.         <li class="item-content">
  27.           <div class="item-inner">
  28.             <div class="item-title">Audi</div>
  29.           </div>
  30.         </li>
  31.         ...
  32.       </ul>
  33.     </div>
  34.   </div>
  35. </div>
复制

实例预览

搜索栏JavaScript事件

事件 目标 描述
搜索 在"data-search-list"<div class="list-block">中被指定的列表元素 在搜索过程中(搜索域改变)事件会被触发。事件包含搜索请求(e.detail.query)和找到的HTML元素(e.detail.foundItems)
清空搜索 在"data-search-list"<div class="list-block">中被指定的列表元素 当用户点击搜索栏“清空”按钮(a href="#" class="searchbar-clear")的时候,事件被触发
启用搜索 在"data-search-list"<div class="list-block">中被指定的列表元素 当搜索栏起作用时,事件被触发
禁用搜索 在"data-search-list"<div class="list-block">中被指定的列表元素 当搜索栏被禁用时 - 用户点击“取消”按钮或"searchbar-overlay"元素,事件被触发

 

1
1
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    iOS开发视频教程

    资源名称:iOS开发视频教程资源目录:【】iOS开发视频教程-第01讲-iOS历史介绍【】iOS开发视频教程-第02讲-XCode安装【】iOS开发视频教程-第03讲-UIView_PPT【】iOS开发视频教程-第04讲-UILabel【】iOS开发视频教程-...

    HTML移动端框架Framework7.zip

    Framework7 是免费开源的 HTML 移动端框架,用来开发混合移动端应用或者 iOS 7 的 Web 应用,并且带有 iOS 7 的原生外观和感觉。Framework7 也是独立的原型应用工具。Framework7 使用 Javascript,CSS 和 HTML 来...

    《iOS开发零基础入门教程》(40集)

    资源名称:《iOS开发零基础入门教程》(40集)资源目录:【】传智播客《iOS开发零基础入门教程》1.1【】传智播客《iOS开发零基础入门教程》1.2【】传智播客《iOS开发零基础入门教程》1.3【】传智播客《iOS开发零基础...

    framework7-icons, Framework7的免费iOS图标字体.zip

    framework7-icons, Framework7的免费iOS图标字体 Framework7图标Framework7的高级和免费ios图标字体。这种字体可以用在 Framework7插件中,但是你可以在任何地方看到它适合它,个人或者商业。 它是免费的使用和许可...

    Framework7Vue使用Framework7和Vue构建完整的iOS和Android应用程序

    Framework7是一款免费、开源的移动应用框架,旨在为开发者提供构建iOS和Android应用的全套工具。它提供了丰富的组件库,包括导航、表单、视图、滑块等,使开发者能够轻松实现原生应用的界面和交互效果。 二、Vue.js...

    ios开发 AES-128-ECB加密

    之前遇到的需求,ios上没有类似的实现。搞了好久在网上搜了很多资料都不能解决最后找到了一个c/c++的底层源码,才实现了最后整理了下上传了。是个ios的demo很详细你定能看懂的。运行在xcode环境下,关键算法的文件是...

    PyPI 官网下载 | pyobjc-framework-Cocoa-6.0.1.tar.gz

    Objective-C是苹果公司开发的一种面向对象的编程语言,广泛应用于MacOS和iOS平台的软件开发。Cocoa是Apple为这两种操作系统提供的原生开发框架,包含了大量用于构建图形用户界面、处理事件、管理文件系统等任务的...

    iPhone开发入门到精通视频教程

    iPhone开发入门到精通视频教程资源目录:【】iOS开发源码系列---工具【】iOS开发源码系列---应用【】iOS开发源码系列---游戏【】iOS开发源码系列---类库与框架【】iOS开发真机测试与发布【】iOS开发视频教程01~05...

    UI第一章:无限互联iOS开发教程课件-- iPhone开发入门

    ### UI第一章:无限互联iOS开发教程课件 -- iPhone开发入门 #### iOS系统概述与架构 - **iOS系统**:作为苹果公司专为iPhone、iPod touch及iPad设计的操作系统,iOS自诞生以来就以其简洁易用的特点受到全球用户的...

    IOS应用源码Demo-iOS framework 制作教程【非静态包】-毕设学习.zip

    提供的【应用】★★★★-iOS framework 制作教程【非静态包】源码示例,应该包含了完整的框架创建过程和相关代码,你可以通过阅读和运行这个项目来加深理解。在实践中,你可以尝试修改源代码,添加新的功能,或者...

    Framework7用于构建iOS和Android应用全功能移动HTML框架

    Framework7是一款强大的、免费的HTML5移动应用框架,专为构建具有原生iOS和Android界面风格的混合式移动应用而设计。它允许开发者使用Web技术(如HTML, CSS, JavaScript)来创建高度交互且功能丰富的应用程序,同时...

    手机移动端电商,IOS,Android

    总之,手机移动端电商在iOS和Android平台上的实现涉及多方面的技术和策略。从H5页面的构建到原生功能的集成,再到用户体验的优化,每一个环节都需要精心设计和实施。随着技术的不断发展,未来的移动端电商将更加智能...

    Framework7是一个用于构建iOS和Android应用的全功能HTML框架

    Framework7 是免费开源的 HTML 移动端框架,用来开发混合移动端应用或者 iOS 7 的 Web 应用,并且带有 iOS 7 的原生外观和感觉。Framework7 也是独立的原型应用工具。

    移动端兼容安卓IOS的高仿支付密码输入功能

    移动端兼容安卓IOS的高仿支付密码输入功能。 类似支付宝,微信的支付密码功能。

    iOS开发框架

    本资源“iOS开发框架”显然是一个专为学习和开发iOS应用而设计的集合,其中包含了多个有用的工具和示例项目。 1. **CTools使用.rtf**: 这个文件可能是关于`CTools`的使用指南,`CTools`通常是指一套用于iOS开发的...

    ios开发-使用mpass移动开发框架在ios端抓包hook脚本.zip

    "ios开发-使用mpass移动开发框架在ios端抓包hook脚本.zip"是一个针对iOS开发者的重要资源,它包含了一套利用mpass移动开发框架在iOS设备上实现抓包功能的hook脚本。本文将详细介绍如何使用这个框架和脚本来实现iOS端...

    WebRTC.framework iOS Debug arm64与x86-64架构

    iOS Debug环境 arm64与x86_64架构 WebRTC.framework Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit dynamically linked shared library x86_64] [arm64] Mach-O 64-bit dynamically linked ...

    appium-ios-device-自动化-手机自动化-移动端IOS自动化-自动化测试-ios真机驱动

    Appium 是一个流行的开源自动化测试框架,用于移动应用测试,支持iOS和Android平台。在iOS自动化测试领域,Appium 提供了对真实设备和模拟器的控制能力,使得开发者和测试工程师可以编写跨平台的自动化测试脚本。...

    road-ios-framework, 道路- 快速 Objective C 应用程序开发.zip

    road-ios-framework, 道路- 快速 Objective C 应用程序开发 一组重用组件利用额外的维度属性定向程序 。##Components基础类的属性。反射和辅助扩展支持核心 。服务 implementation服务定位器 Pattern的实现,集中...

Global site tag (gtag.js) - Google Analytics