Searchbar - 图1

Searchbar

Searchbar allows user to search through List View elements. Or it can be used as a visual UI component for your custom search realization.

Searchbar Layout

  1. <div class="searchbar-backdrop"></div>
  2. <form class="searchbar">
  3. <div class="searchbar-inner">
  4. <div class="searchbar-input-wrap">
  5. <input type="search" placeholder="Search">
  6. <i class="searchbar-icon"></i>
  7. <span class="input-clear-button"></span>
  8. </div>
  9. <span class="searchbar-disable-button">Cancel</span>
  10. </div>
  11. </form>

Where:

  • **<div class="searchbar-input-wrap">** - wrapper for search field and clear button
    • **<input type="search">** - search field
    • **<i class="searchbar-icon">** - search icon
    • **<span class="input-clear-button">** - button to clear field value and reset search results. Optional element
  • **<span class="searchbar-disable-button">** - Searchbar “Cancel” button that will deactivate Searchbar, reset search results and clear search field. Optional element
  • **<div class="searchbar-backdrop">** - semi transparent searchbar backdrop that becomes visible when we enable searchbar. It is recommended to place this element inside of scrollable page’s page-content

Searchbar Type

Now let’s see where we can place Searchbar in page structure. There are few options:

Fixed Searchbar

Fixed Searchbar is always visible on screen not depending on page scroll. In this case it must be placed according to one of the following rules:

  • It can be a direct child of page and if page has also fixed Navbar and/or Toolbar then it must be AFTER Navbar and Toolbar:

    1. <div class="page">
    2. <div class="navbar">...</div>
    3. <div class="toolbar">...</div>
    4. <!-- Searchbar goes after Navbar and Toolbar -->
    5. <form class="searchbar">...</form>
    6. <div class="page-content">
    7. <!-- Searchbar backdrop layer -->
    8. <div class="searchbar-backdrop"></div>
    9. <!-- page content here -->
    10. </div>
    11. </div>
  • It can be placed inside of Subnavbar which is inside of the Navbar (recommended way):

    1. <div class="page page-with-subnavbar">
    2. <div class="navbar">
    3. <div class="navbar-inner">
    4. ...
    5. <div class="subnavbar">
    6. <!-- Searchbar inside of Subnavbar -->
    7. <form class="searchbar">...</form>
    8. </div>
    9. </div>
    10. </div>
    11. <div class="page-content">
    12. <!-- Searchbar backdrop layer -->
    13. <div class="searchbar-backdrop"></div>
    14. <!-- page content here -->
    15. </div>
    16. </div>

Static Searchbar

In this case Searchbar is just part of the scrollable page content:

  1. <div class="page">
  2. <div class="navbar">...</div>
  3. <div class="page-content">
  4. <!-- Searchbar backdrop layer -->
  5. <div class="searchbar-backdrop"></div>
  6. <!-- Searchbar is part of scrollable page content -->
  7. <form class="searchbar">...</form>
  8. <!-- page content here -->
  9. </div>
  10. </div>

Expandable Searchbar

Expandable Searchbar is hidden when disabled and becomes visible when we enable it. Its layout is pretty strict, it must be placed inside of Navbar:

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-inner">
  4. <div class="left">...</div>
  5. <div class="title">...</div>
  6. <div class="right">
  7. <!-- Link to enable searchbar -->
  8. <a class="link icon-only searchbar-enable" data-searchbar=".searchbar">
  9. <i class="icon f7-icons ios-only">search_strong</i>
  10. <i class="icon material-icons md-only">search</i>
  11. </a>
  12. </div>
  13. <!-- Searchbar is a direct child of "navbar-inner" -->
  14. <form class="searchbar searchbar-expandable">
  15. <div class="searchbar-inner">
  16. <div class="searchbar-input-wrap">
  17. <input type="search" placeholder="Search"/>
  18. <i class="searchbar-icon"></i>
  19. <span class="input-clear-button"></span>
  20. </div>
  21. <span class="searchbar-disable-button">Cancel</span>
  22. </div>
  23. </form>
  24. </div>
  25. </div>
  26. <!-- Scrollable page content -->
  27. <div class="page-content">...</div>
  28. </div>

Where:

  • **<a class="link icon-only searchbar-enable" data-searchbar=".searchbar">** - link to enable/expand Searchbar. Optional or can be placed in any other place. **data-searchbar** attribute contains CSS selector of Searchbar to enable.
  • Searchbar has additional **searchbar-expandable** class. It is required for expandable Searchbar to work

Searchbar Behavior Classes

There are also several CSS classes that can be added to elements that will define their behavior when Searchbar is active:

  • **searchbar-hide-on-enable** - elements with such class on page will be hidden when searchbar is enabled
  • **searchbar-hide-on-search** - elements with such class on page will be hidden during search
  • **searchbar-not-found** - elements with such class are hidden by default and become visible when there is not any search results
  • **searchbar-found** - elements with such class are visible by default and become hidden when there is not any search results
  • **searchbar-ignore** - searchbar will not consider this elements in search results

For example:

  1. <div class="page">
  2. <div class="navbar">...</div>
  3. <div class="page-content">
  4. <div class="searchbar-backdrop"></div>
  5. <form class="searchbar">...</form>
  6. <!-- Following block title and block will be hidden on search -->
  7. <div class="block-title searchbar-hide-on-search">Some block title</div>
  8. <div class="block">Lorem ipsum dolor sit amet...</div>
  9. <!-- We do search in super heroes list so the following title and list must be visible on search -->
  10. <div class="block-title searchbar-found">Super Heroes</div>
  11. <div class="list simple-list searchbar-found">
  12. <ul>
  13. <li>Hulk</li>
  14. <li>Batman</li>
  15. <li>Superman</li>
  16. ...
  17. </ul>
  18. </div>
  19. <!-- This list will be visible when there is not any search results -->
  20. <div class="list simple-list searchbar-not-found">
  21. <ul>
  22. <li>Nothing found</li>
  23. </ul>
  24. </div>
  25. </div>
  26. </div>

Searchbar App Methods

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

app.searchbar.create(parameters)Initialize Searchbar with parameters
  • parameters - object - object with Searchbar parameters
  • Method returns initialized Searchbar instance
app.searchbar.destroy(el)Destroy Searchbar instance
  • el - HTMLElement or string (with CSS Selector) or object. Searchbar element or Searchbar instance to destroy.
app.searchbar.get(el)Get Searchbar instance by HTML element
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.clear(el)Clear Searchbar text input
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.enable(el)Enable Searchbar
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.disable(el)Disable Searchbar
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.toggle(el)Toggle Searchbar: enable if it was disabled, or disable if it was enabled
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance
app.searchbar.search(el, query)get Searchbar instance by HTML element
  • el - HTMLElement or string (with CSS Selector). Searchbar element.
  • Method returns Searchbar’s instance

Searchbar Parameters

Let’s look on list of all available parameters we need to create/init Searchbar:

ParameterTypeDefaultDescription
elstring
HTMLElement
CSS selector or HTML element of searchbar element (form class=”searchbar”)
inputElstring
HTMLElement
CSS selector or HTML element of searchbar input element. By default (if not passed) will try to look for input type=”search” inside of searchbar
disableButtonbooleantrueEnables disable button
disableButtonElstring
HTMLElement
CSS selector or HTML element of searchbar disable button. By default (if not passed) will try to look for element with search-disable-button class inside of searchbar
searchContainerstring
HTMLElement
CSS selector or HTML element of list block to search in
searchInstringCSS selector of List View element’s field where we need to search. Usually we search through element titles, in this case we need to pass .item-title. It is also possible to pass few elements for search like .item-title, .item-text
searchItemstringliCSS selector of single search item. If we do a search in List View, then it must be a single list element li
searchGroupstring.list-groupCSS selector of group element. Used when hideGroups enabled to hide groups. If we do a search in List View, then it usually a list group.
searchGroupTitlestring.list-group-title, .item-dividerCSS selector of group titles and dividers. Used when hideDividers enabled to hide group titles and dividers. If we do a search in List View, then it usually a list group title or list item divider.
foundElstring
HTMLElement
.searchbar-foundCSS selector or HTMLElement of searchbar “found” element to make it hidden when there is no search results
notFoudElstring
HTMLElement
.searchbar-not-foundCSS selector or HTMLElement of searchbar “not-found” element to make it visible when there is no search results
hideOnEnableElstring
HTMLElement
.searchbar-hide-on-enableCSS selector or HTMLElement of elements to be hidden when searchbar enabled
hideOnSearchElstring
HTMLElement
.searchbar-hide-on-searchCSS selector or HTMLElement of elements to be hidden on searchbar search
backdropbooleantrueEnables searchbar backdrop element
backdropElstring
HTMLElement
CSS selector or HTMLElement of searchbar backdrop element. If not passed and backdrop parameter is true then it will look for .searchbar-backdrop element. In case none found it will create one automatically
ignorestring.searchbar-ignoreCSS selector for items to be ignored by searchbar and always present in search results
customSearchbooleanfalseWhen enabled searchbar will not search through any of list blocks specified by searchContainer and you will be able to use custom search functionality, for example, for calling external APIs with search results and for displaying them manually
removeDiacriticsbooleanfalseEnable to remove/replace diacritics (á, í, ó, etc.) during search
hideDividersbooleantrueIf enabled, then search will consider item dividers and group titles and hide them if there are no found items right after them
hideGroupsbooleantrueIf enabled, then search will consider list view groups hide them if there are no found items inside of these groups
onobjectObject with events handlers. For example:
  1. var searchbar = app.searchbar.create({
  2. el: ‘.searchbar’,
  3. on: {
  4. enable: function () {
  5. console.log(‘Searchbar enabled’)
  6. }
  7. }
  8. })

Searchbar Methods & Properties

So to create Popup we have to call:

  1. var searchbar = app.searchbar.create({ /* parameters */ })

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

Properties
searchbar.paramsObject with passed initialization parameters
searchbar.queryCurrent search query (search input value)
searchbar.previousQueryPrevious search query (search input value)
searchbar.searchContainerSearchbar search container
searchbar.$searchContainerDom7 element with searchbar search container
searchbar.elSearchbar HTML element.
searchbar.$elDom7 element with searchbar HTML element.
searchbar.inputElSearchbar input HTML element
searchbar.$inputElDom7 element with searchbar input HTML element
searchbar.enabledBoolean value that represents is searchbar enabled or disabled
searchbar.expandableBoolean value that represents is searchbar expandable or not
Methods
searchbar.search(query);Force searchbar to search passed query
searchbar.enable();Enable/activate searchbar
searchbar.disable();Disable/deactivate searchbar
searchbar.toggle();Toggle searchbar
searchbar.clear();Clear search query and update results
searchbar.destroy();Destroy searchbar instance
searchbar.on(event, handler)Add event handler
searchbar.once(event, handler)Add event handler that will be removed after it was fired
searchbar.off(event, handler)Remove event handler
searchbar.off(event)Remove all handlers for specified event
searchbar.emit(event, …args)Fire event on instance

Searchbar Events

Searchbar will fire the following DOM events on searchbar element and events on app and searchbar instance:

DOM Events

EventTargetDescription
searchbar:searchSearchbar Element<form class=”searchbar”>Event will be triggered during search (search field change). Event detail (e.detail) contains search query
searchbar:clearSearchbar Element<form class=”searchbar”>Event will be triggered when user clicks on Searchbar’s “clear” element (a href=”#” class=”searchbar-clear”). Event detail (e.detail) contains previous (before clear) search query
searchbar:enableSearchbar Element<form class=”searchbar”>Event will be triggered when Searchbar becomes active/enabled
searchbar:disableSearchbar Element<form class=”searchbar”>Event will be triggered when Searchbar becomes disabled/inactive - when user clicks on “Cancel” button (a href=”searchbar-cancel”) or on “searchbar-overlay” element
searchbar:beforedestroySearchbar Element<form class=”searchbar”>Event will be triggered right before Searchbar instance will be destroyed

App and Searchbar Instance Events

Searchbar instance emits events on both self instance and app instance. App instance events has same names prefixed with searchbar.

EventTargetArgumentsDescription
searchsearchbar(searchbar, query, previousQuery)Event will be triggered during search (search field change). As an argument event handler receives searchbar instance, current query and previous query
searchbarSearchapp
clearsearchbar(searchbar, previousQuery)Event will be triggered when user clicks on Searchbar’s “clear” element . As an argument event handler receives searchbar instance and previous (before clear) query
searchbarClearapp
enablesearchbar(searchbar)Event will be triggered when Searchbar becomes active/enabled. As an argument event handler receives searchbar instance
searchbarEnableapp
disablesearchbar(searchbar)Event will be triggered when Searchbar becomes inactive/disabled. As an argument event handler receives searchbar instance
searchbarDisableapp
beforeDestroysearchbar(searchbar)Event will be triggered right before Searchbar instance will be destroyed
searchbarBeforeDestroyapp

Searchbar Auto Initialization

If you don’t need to use Searchbar API and your Searchbar is inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional **searchbar-init** class to searchbar element, and all required parameters can be passed using **data-** attributes:

  1. <div class="page">
  2. <div class="navbar">...</div>
  3. <div class="page-content">
  4. <div class="searchbar-backdrop"></div>
  5. <!-- Searchbar with "searchbar-init" class for auto initialization and searchContainer, searchIn parameters passed in data- attributes -->
  6. <form class="searchbar searchbar-init" data-search-container=".search-here">
  7. ...
  8. </form>
  9. <div class="list simple-list search-list searchbar-found">
  10. <ul>
  11. <li>Hulk</li>
  12. <li>Batman</li>
  13. <li>Superman</li>
  14. ...
  15. </ul>
  16. </div>
  17. ...
  18. </div>
  19. </div>

Parameters that used in camelCase, for example searchContainer, in data- attributes should be used in kebab-case as data-search-container

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-inner">
  4. <div class="title">Search Bar</div>
  5. <div class="subnavbar">
  6. <!-- Searchbar with auto init -->
  7. <form class="searchbar">
  8. <div class="searchbar-inner">
  9. <div class="searchbar-input-wrap">
  10. <input type="search" placeholder="Search">
  11. <i class="searchbar-icon"></i>
  12. <span class="input-clear-button"></span>
  13. </div>
  14. <span class="searchbar-disable-button">Cancel</span>
  15. </div>
  16. </form>
  17. </div>
  18. </div>
  19. </div>
  20. <div class="page-content">
  21. <!-- Searchbar backdrop -->
  22. <div class="searchbar-backdrop"></div>
  23. <!-- hide-on-search element -->
  24. <div class="block searchbar-hide-on-search">
  25. <p>This block will be hidden on search. Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
  26. </div>
  27. <!-- search target list -->
  28. <div class="list searchbar-found">
  29. <ul>
  30. <li class="item-content">
  31. <div class="item-inner">
  32. <div class="item-title">Acura</div>
  33. </div>
  34. </li>
  35. <li class="item-content">
  36. <div class="item-inner">
  37. <div class="item-title">Audi</div>
  38. </div>
  39. </li>
  40. <li class="item-content">
  41. <div class="item-inner">
  42. <div class="item-title">BMW</div>
  43. </div>
  44. </li>
  45. ...
  46. <li class="item-content">
  47. <div class="item-inner">
  48. <div class="item-title">Volvo</div>
  49. </div>
  50. </li>
  51. </ul>
  52. </div>
  53. <!-- Nothing found message -->
  54. <div class="block searchbar-not-found">
  55. <div class="block-inner">Nothing found</div>
  56. </div>
  57. </div>
  58. </div>
  1. var app = new Framework7();
  2. // create searchbar
  3. var searchbar = app.searchbar.create({
  4. el: '.searchbar',
  5. searchContainer: '.list',
  6. searchIn: '.item-title',
  7. on: {
  8. search(sb, query, previousQuery) {
  9. console.log(query, previousQuery);
  10. }
  11. }
  12. });