Picker

    Picker is a powerful component that allows you to create custom overlay pickers which looks like iOS native picker.

    Picker could be used as inline component or as overlay. Overlay Picker will be automatically converted to Popover on tablets (iPad).

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

    app.picker.create(parameters)- create Picker instance

    • parameters - object. Object with picker parameters

    Method returns created Picker’s instance

    • el - HTMLElement or string (with CSS Selector) or object. Picker element or Picker instance to destroy.

    app.picker.get(el)- get Picker instance by HTML element

    • el - HTMLElement or string (with CSS Selector). Picker element.

    Method returns Picker’s instance

    app.picker.close(el)- close Picker

    • el - HTMLElement or string (with CSS Selector). Picker element to close.

    Method returns Picker’s instance

    For example:

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

    1. var app = new Framework7({
    2. picker: {
    3. rotateEffect: true,
    4. openIn: 'popover',
    5. }
    6. });

    When we configure Picker we need to pass cols parameter. It is an array where each item is an object with column parameters:

    ParameterTypeDefaultDescription
    valuesarrayArray with string columns values
    displayValuesarrayArray with string columns values that will be displayed in Picker. If not specified, it will display values from values parameter
    cssClassstringAdditional CSS class name to be set on column HTML container
    textAlignstringText alignment of column values, could be “left”, “center” or “right”
    widthnumberColumn width in px. Useful if you need to fix column widths in picker with dependent columns. By default, calculated automatically
    dividerbooleanfalseDefines column that should be used as a visual divider, that doesn’t have any values
    contentstringShould be specified for divider-column (divider:true) with content of the column
    onChangefunction(picker, value, displayValue)Callback function that will be executed when picker value changed.

    After we initialize Picker we have its initialized instance in variable (like picker variable in examples above) with helpful methods and properties:

    Each column in picker.cols array also has its own useful methods and properties.

    1. //Get first column
    2. var col = picker.cols[0];
    Properties
    col.elColumn HTML element
    col.$elDom7 instance with column HTML container
    col.itemsDom7 instance with column items HTML elements
    col.valueCurrently selected column value
    col.displayValueCurrently selected column display value
    col.activeIndexIndex number of currently selected/active item
    Methods
    col.setValue(value, duration)Set new value for current column. value is a new value, duration - transition duration in ms
    col.replaceValues(values, displayValues)Replace column values and displayValues with new ones

    Picker will fire the following DOM events on picker element and events on app and picker instance:

    EventTargetArgumentsDescription
    changepicker(picker, value, displayValue)Event will be triggered when picker value changes
    pickerChangeapp
    initpicker(picker)Event will be triggered when picker initialized
    pickerInitapp
    openpicker(picker)Event will be triggered when Picker starts its opening animation. As an argument event handler receives picker instance
    pickerOpenapp
    openedpicker(picker)Event will be triggered after Picker completes its opening animation. As an argument event handler receives picker instance
    pickerOpenedapp
    closepicker(picker)Event will be triggered when Picker starts its closing animation. As an argument event handler receives picker instance
    pickerCloseapp
    closedpicker(picker)Event will be triggered after Picker completes its closing animation. As an argument event handler receives picker instance
    pickerClosedapp
    beforeDestroypicker(picker)Event will be triggered right before Picker instance will be destroyed. As an argument event handler receives picker instance
    pickerBeforeDestroyapp

    Picker With Single Value

    1. var pickerDevice = app.picker.create({
    2. inputEl: '#demo-picker-device',
    3. cols: [
    4. {
    5. textAlign: 'center',
    6. values: ['iPhone 4', 'iPhone 4S', 'iPhone 5', 'iPhone 5S', 'iPhone 6', 'iPhone 6 Plus', 'iPad 2', 'iPad Retina', 'iPad Air', 'iPad mini', 'iPad mini 2', 'iPad mini 3']
    7. }
    8. ]
    9. });

    Two Values and 3D-Rotate Effect

    1. <div class="block-title">2 values and 3d-rotate effect</div>
    2. <div class="list no-hairlines-md">
    3. <ul>
    4. <li>
    5. <div class="item-content item-input">
    6. <div class="item-inner">
    7. <div class="item-input-wrap">
    8. <input type="text" placeholder="Describe yourself" readonly="readonly" id="demo-picker-describe"/>
    9. </div>
    10. </div>
    11. </div>
    12. </li>
    13. </ul>
    14. </div>
    1. var pickerDescribe = app.picker.create({
    2. inputEl: '#demo-picker-describe',
    3. rotateEffect: true,
    4. cols: [
    5. {
    6. textAlign: 'left',
    7. values: ('Super Amazing Bat Iron Rocket Lex Beautiful Wonderful Raining Happy Funny Cool Hot').split(' ')
    8. },
    9. {
    10. values: ('Man Luthor Woman Boy Girl Person Cutie Babe Raccoon').split(' ')
    11. },
    12. ]
    13. });

    Dependent Values

    1. var carVendors = {
    2. Japanese : ['Honda', 'Lexus', 'Mazda', 'Nissan', 'Toyota'],
    3. American : ['Cadillac', 'Chrysler', 'Dodge', 'Ford']
    4. var pickerDependent = app.picker.create({
    5. inputEl: '#demo-picker-dependent',
    6. rotateEffect: true,
    7. formatValue: function (values) {
    8. return values[1];
    9. },
    10. cols: [
    11. {
    12. textAlign: 'left',
    13. values: ['Japanese', 'German', 'American'],
    14. onChange: function (picker, country) {
    15. if(picker.cols[1].replaceValues){
    16. picker.cols[1].replaceValues(carVendors[country]);
    17. }
    18. }
    19. },
    20. {
    21. values: carVendors.Japanese,
    22. width: 160,
    23. },
    24. ]
    25. });

    Custom toolbar

    1. <div class="block-title">Custom toolbar</div>
    2. <div class="list no-hairlines-md">
    3. <ul>
    4. <li>
    5. <div class="item-content item-input">
    6. <div class="item-inner">
    7. <div class="item-input-wrap">
    8. <input type="text" placeholder="Describe yourself" readonly="readonly" id="demo-picker-custom-toolbar"/>
    9. </div>
    10. </div>
    11. </div>
    12. </li>
    13. </ul>
    14. </div>
    1. var pickerCustomToolbar = app.picker.create({
    2. inputEl: '#demo-picker-custom-toolbar',
    3. rotateEffect: true,
    4. renderToolbar: function () {
    5. return '<div class="toolbar">' +
    6. '<div class="toolbar-inner">' +
    7. '<div class="left">' +
    8. '<a href="#" class="link toolbar-randomize-link">Randomize</a>' +
    9. '</div>' +
    10. '<div class="right">' +
    11. '<a href="#" class="link sheet-close popover-close">That\'s me</a>' +
    12. '</div>' +
    13. '</div>' +
    14. '</div>';
    15. },
    16. cols: [
    17. {
    18. values: ['Mr', 'Ms'],
    19. },
    20. {
    21. textAlign: 'left',
    22. values: ('Super Amazing Bat Iron Rocket Lex Beautiful Wonderful Raining Happy Funny Cool Hot').split(' ')
    23. },
    24. {
    25. values: ('Man Luthor Woman Boy Girl Person Cutie Babe Raccoon').split(' ')
    26. },
    27. ],
    28. on: {
    29. open: function (picker) {
    30. picker.$el.find('.toolbar-randomize-link').on('click', function () {
    31. var col0Values = picker.cols[0].values;
    32. var col0Random = col0Values[Math.floor(Math.random() * col0Values.length)];
    33. var col1Values = picker.cols[1].values;
    34. var col1Random = col1Values[Math.floor(Math.random() * col1Values.length)];
    35. var col2Values = picker.cols[2].values;
    36. var col2Random = col2Values[Math.floor(Math.random() * col2Values.length)];
    37. picker.setValue([col0Random, col1Random, col2Random]);
    38. });
    39. },
    40. }
    41. });

    Inline Picker / Date-time

    1. var today = new Date();
    2. var pickerInline = app.picker.create({
    3. containerEl: '#demo-picker-date-container',
    4. inputEl: '#demo-picker-date',
    5. toolbar: false,
    6. rotateEffect: true,
    7. value: [
    8. today.getMonth(),
    9. today.getDate(),
    10. today.getFullYear(),
    11. today.getHours(),
    12. today.getMinutes() < 10 ? '0' + today.getMinutes() : today.getMinutes()
    13. ],
    14. formatValue: function (values, displayValues) {
    15. return displayValues[0] + ' ' + values[1] + ', ' + values[2] + ' ' + values[3] + ':' + values[4];
    16. },
    17. cols: [
    18. // Months
    19. {
    20. values: ('0 1 2 3 4 5 6 7 8 9 10 11').split(' '),
    21. displayValues: ('January February March April May June July August September October November December').split(' '),
    22. textAlign: 'left'
    23. },
    24. // Days
    25. {
    26. values: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],
    27. },
    28. // Years
    29. {
    30. values: (function () {
    31. var arr = [];
    32. for (var i = 1950; i <= 2030; i++) { arr.push(i); }
    33. return arr;
    34. })(),
    35. },
    36. // Space divider
    37. {
    38. divider: true,
    39. content: '&nbsp;&nbsp;'
    40. },
    41. // Hours
    42. {
    43. values: (function () {
    44. var arr = [];
    45. for (var i = 0; i <= 23; i++) { arr.push(i); }
    46. return arr;
    47. })(),
    48. },
    49. // Divider
    50. {
    51. divider: true,
    52. content: ':'
    53. },
    54. // Minutes
    55. {
    56. values: (function () {
    57. var arr = [];
    58. for (var i = 0; i <= 59; i++) { arr.push(i < 10 ? '0' + i : i); }
    59. return arr;
    60. })(),
    61. }
    62. ],
    63. on: {
    64. change: function (picker, values, displayValues) {
    65. var daysInMonth = new Date(picker.value[2], picker.value[0]*1 + 1, 0).getDate();
    66. if (values[1] > daysInMonth) {
    67. picker.cols[1].setValue(daysInMonth);
    68. }
    69. },
    70. });