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 is pretty simple. It is a well known link with inside and additional **smart-select** class on item-link:

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

    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>

    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. <option value="audi">Audi</option>
    12. <option value="bmw">BMW</option>
    13. ...
    14. </optgroup>
    15. <optgroup label="American">
    16. <option value="cadillac">Cadillac</option>
    17. <option value="chrysler">Chrysler</option>
    18. ...
    19. </select>
    20. <div class="item-content">
    21. <div class="item-inner">
    22. <div class="item-title">Car</div>
    23. </div>
    24. </div>
    25. </a>
    26. </li>

    Different Display Value

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

    Smart Select App Methods

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

    • 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

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

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

    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:

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

    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.

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