Picker - 图1

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).

Picker App Methods

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

app.picker.destroy(el)- destroy Picker 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:

  1. var picker = app.picker.create({
  2. inputEl: '#picker-input',
  3. cols: [
  4. {
  5. values: ['apple', 'orange', 'banana'],
  6. displayValues: ['Apple', 'Orange', 'Banana'],
  7. }
  8. ]
  9. });

Picker Parameters

Let’s look on list of all available Picker parameters:

ParameterTypeDefaultDescription
rotateEffectbooleanfalseEnables 3D rotate effect
momentumRationumber7Larger values produces more momentum when you release picker after fast touch and move
mousewheelbooleantrueEnables ability to scroll through picker values with mousewheel
updateValuesOnMomentumbooleanfalseUpdates picker and input values during momentum
updateValuesOnTouchmovebooleantrueUpdates picker and input values during touch move
updateValuesOnMousewheelbooleantrueUpdates picker and input values during mousewheel scrolling
freeModebooleanfalseDisables snapping on values
valuearrayArray with initial values. Each array item represents value of related column
formatValuefunction (values, displayValues)Function to format input value, should return new/formatted string value. values and displayValues are arrays where each item represents value/display value of related column
colsarrayArray with columns. Each array item represents object with column parameters
Container/opener-specific parameters
containerElstring
HTMLElement
String with CSS selector or HTMLElement where to place generated Picker HTML. Use only for inline picker
openInstringautoCan be auto, popover (to open picker in popover), sheet (to open in sheet modal). In case of auto will open in sheet modal on small screens and in popover on large screens.
inputElstring or HTMLElementString with CSS selector or HTMLElement with related input element
scrollToInputbooleantrueScroll viewport (page-content) to input when picker opened
inputReadOnlybooleantrueSets “readonly” attribute on specified input
cssClassstringAdditional CSS class name to be set on picker element
closeByOutsideClickbooleantrueIf enabled, picker will be closed by clicking outside of picker or related input element
toolbarbooleantrueEnables picker toolbar
toolbarCloseTextstringDoneText for Done/Close toolbar button
routableModalsbooleantrueWill add opened picker to router history which gives ability to close picker by going back in router history and set current route to the picker modal
urlstringselect/Picker modal URL that will be set as a current route
viewobjectView where to set routing when routableModals enabled. Defaults to parent view of inputEl or main view if not found parent view
Render Functions
renderToolbarfunctionFunction to render toolbar. Must return toolbar HTML string
renderfunctionFunction to render whole picker. Must return picker full HTML string
Events
onobjectObject with events handlers. For example:
  1. var picker = app.picker.create({
  2. on: {
  3. opened: function () {
  4. console.log(‘Picker opened’)
  5. }
  6. }
  7. })

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. });

Column Parameters

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.

Picker Methods & Properties

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

Properties
picker.appLink to global app instance
picker.containerElPicker wrapping container HTML element (when inline picker is in use)
picker.$containerElDom7 instance with picker wrapping container HTML element (when inline picker is in use)
picker.elPicker HTML element
picker.$elDom7 instance with picker HTML element
picker.inputElPicker input HTML element (passed in inputEl parameter)
picker.$inputElDom7 instance with picker input HTML element (passed in inputEl parameter)
picker.valueArray where each item represents current selected value for each column
picker.colsArray with specified Picker columns. Each column also has its own methods and properties (look below)
picker.openedtrue if Picker is currently opened
picker.inlinetrue when inline picker is in use
picker.urlPicker URL (that was passed in url parameter)
picker.viewPicker View (that was passed in view parameter) or found parent view
picker.paramsObject with initialization parameters
Methods
picker.setValue(values, duration)Set new picker value. values is array where each item represents value for each column. duration - transition duration in ms
picker.getValue()Returns current picker value
picker.addValue()Adds value to the values array. Useful in case if multiple selection is enabled (with multiple: true parameter)
picker.open()Open Picker
picker.close()Close Picker
picker.destroy()Destroy Picker instance and remove all events
picker.on(event, handler)Add event handler
picker.once(event, handler)Add event handler that will be removed after it was fired
picker.off(event, handler)Remove event handler
picker.off(event)Remove all handlers for specified event
picker.emit(event, …args)Fire event on instance

Column Methods & 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 Events

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

DOM Events

EventTargetDescription
picker:openPicker Element<div class=”picker”>Event will be triggered when Picker starts its opening animation
picker:openedPicker Element<div class=”picker”>Event will be triggered after Picker completes its opening animation
picker:closePicker Element<div class=”picker”>Event will be triggered when Picker starts its closing animation
picker:closedPicker Element<div class=”picker”>Event will be triggered after Picker completes its closing animation

App and Picker Instance Events

Picker instance emits events on both self instance and app instance. App instance events has same names prefixed with picker.

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

CSS Variables

Below is the list of related CSS variables (CSS custom properties).

  1. :root {
  2. --f7-picker-height: 260px;
  3. --f7-picker-inline-height: 200px;
  4. --f7-picker-popover-height: 200px;
  5. --f7-picker-popover-width: 280px;
  6. --f7-picker-landscape-height: 200px;
  7. --f7-picker-item-height: 36px;
  8. }
  9. .ios {
  10. --f7-picker-column-font-size: 24px;
  11. --f7-picker-divider-text-color: #000;
  12. --f7-picker-item-text-color: #707274;
  13. --f7-picker-item-selected-text-color: #000;
  14. --f7-picker-item-selected-border-color: #a8abb0;
  15. }
  16. .ios .theme-dark,
  17. .ios.theme-dark {
  18. --f7-picker-divider-text-color: #fff;
  19. --f7-picker-item-selected-text-color: #fff;
  20. --f7-picker-item-selected-border-color: rgba(255, 255, 255, 0.06);
  21. }
  22. .md {
  23. --f7-picker-column-font-size: 20px;
  24. --f7-picker-divider-text-color: rgba(0, 0, 0, 0.87);
  25. --f7-picker-item-text-color: inherit;
  26. --f7-picker-item-selected-text-color: inherit;
  27. --f7-picker-item-selected-border-color: rgba(0, 0, 0, 0.15);
  28. }
  29. .md .theme-dark,
  30. .md.theme-dark {
  31. --f7-picker-divider-text-color: rgba(255, 255, 255, 0.87);
  32. --f7-picker-item-selected-border-color: rgba(255, 255, 255, 0.15);
  33. }
  34. .aurora {
  35. --f7-picker-column-font-size: 16px;
  36. --f7-picker-divider-text-color: #000;
  37. --f7-picker-item-height: 28px;
  38. --f7-picker-item-text-color: #888;
  39. --f7-picker-item-selected-text-color: #000;
  40. --f7-picker-item-selected-border-color: rgba(0, 0, 0, 0.12);
  41. }
  42. .aurora .theme-dark,
  43. .aurora.theme-dark {
  44. --f7-picker-divider-text-color: #fff;
  45. --f7-picker-item-selected-text-color: #fff;
  46. --f7-picker-item-selected-border-color: rgba(255, 255, 255, 0.06);
  47. }

Examples

Picker With Single Value

  1. <div class="block-title">Picker with single value</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="Your iOS device" readonly="readonly" id="demo-picker-device"/>
  9. </div>
  10. </div>
  11. </div>
  12. </li>
  13. </ul>
  14. </div>
  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. <div class="block-title">Dependent values</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="Your car" readonly="readonly" id="demo-picker-dependent"/>
  9. </div>
  10. </div>
  11. </div>
  12. </li>
  13. </ul>
  14. </div>
  1. var carVendors = {
  2. Japanese : ['Honda', 'Lexus', 'Mazda', 'Nissan', 'Toyota'],
  3. German : ['Audi', 'BMW', 'Mercedes', 'Volkswagen', 'Volvo'],
  4. American : ['Cadillac', 'Chrysler', 'Dodge', 'Ford']
  5. };
  6. var pickerDependent = app.picker.create({
  7. inputEl: '#demo-picker-dependent',
  8. rotateEffect: true,
  9. formatValue: function (values) {
  10. return values[1];
  11. },
  12. cols: [
  13. {
  14. textAlign: 'left',
  15. values: ['Japanese', 'German', 'American'],
  16. onChange: function (picker, country) {
  17. if(picker.cols[1].replaceValues){
  18. picker.cols[1].replaceValues(carVendors[country]);
  19. }
  20. }
  21. },
  22. {
  23. values: carVendors.Japanese,
  24. width: 160,
  25. },
  26. ]
  27. });

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. <div class="block-title">Inline Picker / Date-time</div>
  2. <div class="list no-margin">
  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="Date Time" readonly="readonly" id="demo-picker-date"/>
  9. </div>
  10. </div>
  11. </div>
  12. </li>
  13. </ul>
  14. </div>
  15. <div class="block block-strong no-padding no-margin margin-bottom">
  16. <div id="demo-picker-date-container"></div>
  17. </div>
  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. }
  71. });