Smart Select - 图1

Smart Select

Smart select allows you to easily convert your usual form selects to dynamic pages with grouped radio inputs. You can see such feature in many native iOS apps

Smart Select Layout

Smart select layout is pretty simple. It is a well known List View link with <select> inside and additional **smart-select** class on item-link:

  1. <div class="list">
  2. <ul>
  3. <!-- Smart select item -->
  4. <li>
  5. <!-- Additional "smart-select" class -->
  6. <a href="#" class="item-link smart-select">
  7. <!-- select -->
  8. <select name="fruits">
  9. <option value="apple" selected>Apple</option>
  10. <option value="pineapple">Pineapple</option>
  11. ...
  12. </select>
  13. <div class="item-content">
  14. <div class="item-inner">
  15. <!-- Select label -->
  16. <div class="item-title">Fruit</div>
  17. <!-- Selected value, not required -->
  18. <div class="item-after">Apple</div>
  19. </div>
  20. </div>
  21. </a>
  22. </li>
  23. <!-- Another smart selects or list view elements -->
  24. ...
  25. </ul>
  26. </div>

Note that smart select works only in initialized Views, because it used Router to load smart select pages or open modals!

Custom Option Icons, Colors and Images

You can also specify Smart Select page’ list view element custom icon, image or color. We need to use additional **data-option-icon**, **data-option-image** attributes on smart select <select> (to set default image for all options) or on <option> to set image or icon on single option.

For single option we may also use **data-option-color** and **data-option-class** attributes to add specific option color or css class for additional styling

  1. <li>
  2. <a href="#" class="item-link smart-select">
  3. <select name="fruits">
  4. <option value="apple" selected data-option-image="http://lorempixel.com/29/29/">Apple</option>
  5. <option value="pineapple" data-option-image="http://lorempixel.com/29/29/?2">Pineapple</option>
  6. <option value="pear" data-option-color="orange" data-option-image="http://lorempixel.com/29/29/?3">Pear</option>
  7. ...
  8. </select>
  9. <div class="item-content">
  10. <div class="item-inner">
  11. <div class="item-title">Fruit</div>
  12. </div>
  13. </div>
  14. </a>
  15. </li>

Multiple Select And

We can also use multiple select and group options using <optgroup>. So if we add multiple attribute to our select then radio buttons on smart select page will be automatically converted to checkboxes:

  1. <li>
  2. <a href="#" class="item-link smart-select">
  3. <!-- "multiple" attribute for multiple select-->
  4. <select name="car" multiple>
  5. <!-- options grouped within "optgroup" tag-->
  6. <optgroup label="Japanese">
  7. <option value="honda" selected>Honda</option>
  8. <option value="lexus">Lexus</option>
  9. ...
  10. </optgroup>
  11. <optgroup label="German">
  12. <option value="audi" selected>Audi</option>
  13. <option value="bmw">BMW</option>
  14. ...
  15. </optgroup>
  16. <optgroup label="American">
  17. <option value="cadillac">Cadillac</option>
  18. <option value="chrysler">Chrysler</option>
  19. ...
  20. </optgroup>
  21. </select>
  22. <div class="item-content">
  23. <div class="item-inner">
  24. <div class="item-title">Car</div>
  25. </div>
  26. </div>
  27. </a>
  28. </li>

Multiple Select and maxlength

With multiple select we can also use maxlength attribute to limit number of possible selected items:

  1. <li>
  2. <a href="#" class="item-link smart-select">
  3. <!-- "maxlength" attribute to limit number of possible selected items -->
  4. <!-- we won't allow to select more than 3 items -->
  5. <select name="car" multiple maxlength="3">
  6. <optgroup label="Japanese">
  7. <option value="honda" selected>Honda</option>
  8. <option value="lexus">Lexus</option>
  9. ...
  10. </optgroup>
  11. <optgroup label="German">
  12. <option value="audi">Audi</option>
  13. <option value="bmw">BMW</option>
  14. ...
  15. </optgroup>
  16. <optgroup label="American">
  17. <option value="cadillac">Cadillac</option>
  18. <option value="chrysler">Chrysler</option>
  19. ...
  20. </optgroup>
  21. </select>
  22. <div class="item-content">
  23. <div class="item-inner">
  24. <div class="item-title">Car</div>
  25. </div>
  26. </div>
  27. </a>
  28. </li>

Different Display Value

Using **data-display-as** attribute on options we can show selected value in different way:

  1. <li>
  2. <a href="#" class="item-link smart-select">
  3. <select name="days">
  4. <option value="monday" selected data-display-as="Mon">Monday</option>
  5. <option value="tuesday" data-display-as="Tue">Tuesday</option>
  6. <option value="wednesday" data-display-as="Wed">Wednesday</option>
  7. <option value="thursday" data-display-as="Thu">Thursday</option>
  8. <option value="friday" data-display-as="Fri">Friday</option>
  9. <option value="saturday" data-display-as="Sat">Saturday</option>
  10. <option value="sunday" data-display-as="Sun">Sunday</option>
  11. </select>
  12. <div class="item-content">
  13. <div class="item-inner">
  14. <div class="item-title">Day</div>
  15. </div>
  16. </div>
  17. </a>
  18. </li>

Smart Select App Methods

Let’s look at related App methods to work with Smart Select:

app.smartSelect.create(parameters)- create Smart Select instance

  • parameters - object. Object with smart select parameters

Method returns created Smart Select’s instance

app.smartSelect.destroy(smartSelectEl)- destroy Smart Select instance

  • smartSelectEl - HTMLElement or string (with CSS Selector) or object. Smart Select element or Smart Select instance to destroy.

app.smartSelect.get(smartSelectEl)- get Smart Select instance by HTML element

  • smartSelectEl - HTMLElement or string (with CSS Selector). Smart Select element.

Method returns Smart Select’s instance

app.smartSelect.open(smartSelectEl)- opens Smart Select

  • smartSelectEl - HTMLElement or string (with CSS Selector). Smart Select element to open.

Method returns Smart Select’s instance

app.smartSelect.close(smartSelectEl)- closes Smart Select

  • smartSelectEl - HTMLElement or string (with CSS Selector). Smart Select element to close.

Method returns Smart Select’s instance

Smart Select Parameters

Now let’s look at list of available parameters we need to create Smart Select:

ParameterTypeDefaultDescription
elHTMLElementSmart Select element. Can be useful if you already have Smart Select element in your HTML and want to create new instance using this element
viewobjectLink to initialized View instance which is required for Smart Select to work. By default, if not specified, it will be opened in parent View.
valueElHTMLElementVisual element where to insert selected value. If not passed then it will look for <div class=”item-after”> element
openInstringpageDefines how to open Smart Select. Can be page or popup or popover or sheet
pageTitlestringSmart select page title. If not passed then it will be the <div class=”item-title”> text
pageBackLinkTextstringBackSmart select Page back link text
popupCloseLinkTextstringCloseSmart select Popup close link text
popupTabletFullscreenbooleanfalseWhen enabled smart select popup will be displayed as full screen on tablets
sheetCloseLinkTextstringDoneSmart select Sheet close link text
searchbarboolean
object
falseEnables Searchbar on smart select page. If passed as object then it should be valid Searchbar parameters
searchbarPlaceholderstringSearchSearchbar placeholder text
searchbarDisableTextstringCancelSearchbar “cancel” link text. Has effect only in iOS theme
appendSearchbarNotFoundboolean
string
HTMLElement
falseAppends block with content that displayed when there are no Searchbar results.

If specified as string then it will append:

  1. <div class=”block searchbar-not-found>{{appendSearchbarNotFound}}</div>

If specified as true then:

  1. <div class=”block searchbar-not-found>Nothing found</div>

If HTMLElement passed then it will add that element.

closeOnSelectbooleanfalseIf enabled then smart select will be automatically closed after user selectes any option
virtualListbooleanfalseEnable Virtual List for smart select if your select has a lot (hundreds, thousands) of options
virtualListHeightnumber
function
Virtual list item height. If number - list item height in px. If function then function should return item height.
formColorThemestringSmart select page form color theme. One of the default colors
navbarColorThemestringSmart select navbar color theme. One of the default colors
routableModalsbooleantrueWill add opened smart select modal (when openIn is popup, popover or sheet) to router history which gives ability to close smart select by going back in router history and set current route to the smart select modal.
urlstringselect/Smart select page/modal URL that will be set as a current route
cssClassstringAdditional CSS class name to be set on Smart Select container (Page, Popup, Popover or Sheet)
Render functions
renderSearchbarfunctionFunction to render smart select searchbar dropdown, must return searchbar HTML string
renderItemfunction(item, index)Function to render smart select item, must return item HTML string
renderItemsfunction(items)Function to render all smart select items, must return all items HTML string
renderPagefunction(items)Function to render smart select page, must return full page HTML string
renderPopupfunction(items)Function to render smart select popup, must return full popup HTML string
renderSheetfunction(items)Function to render smart select sheet, must return full sheet HTML string
renderPopoverfunction(items)Function to render smart select popover, must return full popover HTML string
Events
onobjectObject with events handlers. For example:
  1. var smartSelect = app.smartSelect.create({
  2. on: {
  3. opened: function () {
  4. console.log(‘Smart select opened’)
  5. }
  6. }
  7. })

Note that all following parameters can be used in global app parameters under smartSelect property to set defaults for all smart selects. For example:

  1. var app = new Framework7({
  2. smartSelect: {
  3. pageTitle: 'Select Option',
  4. openIn: 'popup',
  5. }
  6. });

Smart Select Methods & Properties

So to create Smart Select we have to call:

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

After that we have its initialized instance (like smartSelect variable in example above) with useful methods and properties:

Properties
smartSelect.appLink to global app instance
smartSelect.elSmart select HTML element
smartSelect.$elDom7 instance with smart select HTML element
smartSelect.valueElHTML element used to display value
smartSelect.$valueElDom7 instance with HTML element used to display value
smartSelect.selectElChild select element <select>
smartSelect.$selectElDom7 instance with child select element
smartSelect.urlSmart Select URL (that was passed in url parameter)
smartSelect.viewSmart Select View (that was passed in view parameter) or found parent view
smartSelect.paramsSmart Select parameters
Methods
smartSelect.open()Open smart select
smartSelect.close()Close smart select
smartSelect.destroy()Destroy smart select
smartSelect.on(event, handler)Add event handler
smartSelect.once(event, handler)Add event handler that will be removed after it was fired
smartSelect.off(event, handler)Remove event handler
smartSelect.off(event)Remove all handlers for specified event
smartSelect.emit(event, …args)Fire event on instance

Smart Select Events

Smart Select will fire the following DOM events on smart select element and events on app and smart select instance:

DOM Events

EventTargetDescription
smartselect:openSmart Select Element<a class=”item-link smart-select”>Event will be triggered when Smart Select page (or modal) starts its opening animation
smartselect:openedSmart Select Element<a class=”item-link smart-select”>Event will be triggered after Smart Select page (or modal) completes its opening animation
smartselect:closeSmart Select Element<a class=”item-link smart-select”>Event will be triggered when Smart Select page (or modal) starts its closing animation
smartselect:closedSmart Select Element<a class=”item-link smart-select”>Event will be triggered after Smart Select page (or modal) completes its closing animation
smartselect:beforedestroySmart Select Element<a class=”item-link smart-select”>Event will be triggered right before Smart Select instance will be destroyed

App and Smart Select Instance Events

Smart Select instance emits events on both self instance and app instance. App instance events has same names prefixed with smartSelect.

EventTargetArgumentsDescription
opensmartSelect(smartSelect)Event will be triggered when Smart Select starts its opening animation. As an argument event handler receives smart select instance
smartSelectOpenapp
openedsmartSelect(smartSelect)Event will be triggered after Smart Select completes its opening animation. As an argument event handler receives smart select instance
smartSelectOpenedapp
closesmartSelect(smartSelect)Event will be triggered when Smart Select starts its closing animation. As an argument event handler receives smart select instance
smartSelectCloseapp
closedsmartSelect(smartSelect)Event will be triggered after Smart Select completes its closing animation. As an argument event handler receives smart select instance
smartSelectClosedapp
beforeDestroysmartSelect(smartSelect)Event will be triggered right before Smart Select instance will be destroyed. As an argument event handler receives smart select instance
smartSelectBeforeDestroyapp

Smart Select Auto Initialization

If you don’t need to use Smart Select API and your Smart Select is inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional smart-select-init class:

  1. <li>
  2. <!-- Add smart-select-init class -->
  3. <a href="#" class="item-link smart-select smart-select-init">
  4. <!-- select -->
  5. <select name="fruits">
  6. <option value="apple" selected>Apple</option>
  7. <option value="pineapple">Pineapple</option>
  8. ...
  9. </select>
  10. <div class="item-content">
  11. <div class="item-inner">
  12. <div class="item-title">Fruit</div>
  13. <div class="item-after">Apple</div>
  14. </div>
  15. </div>
  16. </a>
  17. </li>

In this case if you need to access created Smart Select instance you can use the app.smartSelect.get app method:

  1. var smartSelect = app.smartSelect.get('.smart-select');

When using auto init you may need to pass additional parameters. In this case you may pass all additional parameters via data- attributes on smart select element:

  1. <li>
  2. <!-- Pass parameters as kebab-case data attributes -->
  3. <a href="#" class="item-link smart-select smart-select-init" data-open-in="popup" data-virtual-list="true" data-page-back-link-text="Go back">
  4. <!-- select -->
  5. <select name="fruits">
  6. <option value="apple" selected>Apple</option>
  7. <option value="pineapple">Pineapple</option>
  8. ...
  9. </select>
  10. <div class="item-content">
  11. <div class="item-inner">
  12. <div class="item-title">Fruit</div>
  13. <div class="item-after">Apple</div>
  14. </div>
  15. </div>
  16. </a>
  17. </li>

Examples

  1. <!-- Default Setup -->
  2. <li>
  3. <a class="item-link smart-select smart-select-init">
  4. <select name="fruits">
  5. <option value="apple" selected>Apple</option>
  6. <option value="pineapple">Pineapple</option>
  7. <option value="pear">Pear</option>
  8. <option value="orange">Orange</option>
  9. <option value="melon">Melon</option>
  10. <option value="peach">Peach</option>
  11. <option value="banana">Banana</option>
  12. </select>
  13. <div class="item-content">
  14. <div class="item-inner">
  15. <div class="item-title">Fruit</div>
  16. </div>
  17. </div>
  18. </a>
  19. </li>
  20. <!-- Open In Popup + Searchbar + Multiple -->
  21. <li>
  22. <a class="item-link smart-select smart-select-init" data-open-in="popup" data-searchbar="true" data-searchbar-placeholder="Search car">
  23. <select name="car" multiple>
  24. <optgroup label="Japanese">
  25. <option value="honda" selected>Honda</option>
  26. <option value="lexus">Lexus</option>
  27. <option value="mazda">Mazda</option>
  28. <option value="nissan">Nissan</option>
  29. <option value="toyota">Toyota</option>
  30. </optgroup>
  31. <optgroup label="German">
  32. <option value="audi" selected>Audi</option>
  33. <option value="bmw">BMW</option>
  34. <option value="mercedes">Mercedes</option>
  35. <option value="vw">Volkswagen</option>
  36. <option value="volvo">Volvo</option>
  37. </optgroup>
  38. <optgroup label="American">
  39. <option value="cadillac">Cadillac</option>
  40. <option value="chrysler">Chrysler</option>
  41. <option value="dodge">Dodge</option>
  42. <option value="ford" selected>Ford</option>
  43. </optgroup>
  44. </select>
  45. <div class="item-content">
  46. <div class="item-inner">
  47. <div class="item-title">Car</div>
  48. </div>
  49. </div>
  50. </a>
  51. </li>
  52. <!-- Open In Sheet -->
  53. <li>
  54. <a class="item-link smart-select smart-select-init" data-open-in="sheet">
  55. <select name="mac-windows">
  56. <option value="mac" selected>Mac</option>
  57. <option value="windows">Windows</option>
  58. </select>
  59. <div class="item-content">
  60. <div class="item-inner">
  61. <div class="item-title">Mac or Windows</div>
  62. </div>
  63. </div>
  64. </a>
  65. </li>
  66. <!-- Open In Popover -->
  67. <li>
  68. <a class="item-link smart-select smart-select-init" data-open-in="popover">
  69. <select name="superhero" multiple>
  70. <option value="Batman" selected>Batman</option>
  71. <option value="Superman">Superman</option>
  72. <option value="Hulk">Hulk</option>
  73. <option value="Spiderman">Spiderman</option>
  74. <option value="Ironman">Ironman</option>
  75. <option value="Thor">Thor</option>
  76. <option value="Wonder Woman">Wonder Woman</option>
  77. </select>
  78. <div class="item-content">
  79. <div class="item-inner">
  80. <div class="item-title">Super Hero</div>
  81. </div>
  82. </div>
  83. </a>
  84. </li>