`

移动端安卓和IOS开发框架Framework7教程-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).

Create Picker Instance

Picker can be created and initialized only using JavaScript. We need to use related App's method:

myApp.picker(parameters) - initialize Picker with parameters

  • parameters - object - object with Picker parameters. Required.
  • Method returns initialized Picker instance

For example:

  1. var myPicker = app.picker({
  2.     input: '#picker-input'
  3.     cols: [
  4.        {
  5.          values: ['Apple', 'Orange', 'Bananna']
  6.        }
  7.      ]
  8. });   
  9. myPicker.open(); // open Picker
复制

Picker Parameters

Let's look on list of all available parameters:

Parameter Type Default Description Common Picker Modal Component ParametersSpecific Picker ParametersCallbacks
container string or HTMLElement   String with CSS selector or HTMLElement where to place generated Picker HTML. Use only for inline pickers
input string or HTMLElement   String with CSS selector or HTMLElement with related input element
scrollToInput boolean true Scroll viewport (page-content) to input when picker opened
inputReadOnly boolean true Sets "readonly" attribute on specified input
convertToPopover boolean true Converts picker modal to Popover on large screens (on iPad)
onlyOnPopover boolean false Enable it and Picker will be always opened in Popover
cssClass string   Additional CSS class name to be set on picker modal
toolbar boolean true Enables picker modal toolbar
toolbarCloseText string 'Done' Text for Done/Close toolbar button
toolbarTemplate string   Toolbar HTML Template. By default it is HTML string with following template:
  1. <div class="toolbar">
  2.   <div class="toolbar-inner">
  3.     <div class="left"></div>
  4.     <div class="right">
  5.       <a href="#" class="link close-picker">{{closeText}}</a>
  6.     </div>
  7.   </div>
  8. </div>
复制
 
rotateEffect boolean false Enables 3D rotate effect
momentumRatio number 7 Larger values produces more momentum when you release picker after fast touch and move
updateValuesOnMomentum boolean false Updates picker and input values during momentum
updateValuesOnTouchmove boolean true Updates picker and input values during touch move
value array   Array with initial values. Each array item represents value of related column
formatValue function (p, values, displayValues)   Function to format input value, should return new/formatted string value. valuesand displayValues are arrays where each item represents value/display value of related column
cols array   Array with columns. Each array item represents object with column parameters
onChange function (p, values, displayValues)   Callback function that will be executed when picker value changed. values anddisplayValues are arrays where each item represents value/display value of related column
onOpen function (p)   Callback function that will be executed when picker is opened
onClose function (p)   Callback function that will be executed when picker is closed

Column parameters

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

Parameter Type Default Description Callbacks
values array   Array with string columns values
displayValues array   Array with string columns values that will be displayed in Picker. If not specified, it will display values from values parameter
cssClass string   Additional CSS class name to be set on column HTML container
textAlign string   Text alignment of column values, could be "left", "center" or "right"
width number   Column width in px. Useful if you need to fix column widths in picker with dependent columns. By default, calculated automatically
divider boolean false Defines column that should be used as a visual divider, that doesn't have any values
content string   Should be specified for divider-column (divider:true) with content of the column
onChange function (p, value, displayValue)   Callback function that will be executed when column value changed. value anddisplayValue represent current column value and displayValue

Picker Methods & Properties

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

PropertiesMethods
myPicker.params Object with passed initialization parameters
myPicker.value Array where each item represents current selected value for each column
myPicker.displayValue Array where each item represents current selected display value for each column
myPicker.opened true if Picker is currently opened
myPicker.inline true if Picker is inline Picker
myPicker.cols Array with specified Picker columns. Each column also has its own methods and properties (look below)
myPicker.container Dom7 instance with Picker HTML container
myPicker.setValue(values, duration) Set new picker value. values is array where each item represents value for each column. duration - transition duration in ms
myPicker.open() Open Picker
myPicker.close() Close Picker
myPicker.destroy() Destroy Picker instance and remove all events

Column Methods & Properties

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

  1. //Get first column
  2. var col = myPicker.cols[0];
复制
PropertiesMethods
col.container Dom7 instance with column HTML container
col.items Dom7 instance with column items HTML elements
col.value Currently selected column value
col.displayValue Currently selected column display value
col.activeIndex Index number of currently selected/active item
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

Access to Picker's Instance

If you initialize Picker as inline Picker it is possible to access to Picker's instance from its HTML container

  1. var myPicker = $$('.picker-inline')[0].f7Picker;
复制

Examples

Picker With Single Value

  1. <div class="content-block-title">Picker with single value</div>
  2. <div class="list-block">
  3.   <ul>
  4.     <li>
  5.       <div class="item-content">
  6.         <div class="item-inner">
  7.           <div class="item-input">
  8.             <input type="text" placeholder="Your iOS device" readonly id="picker-device">
  9.           </div>
  10.         </div>
  11.       </div>
  12.     </li>
  13.   </ul>
  14. </div>
复制
  1. var pickerDevice = myApp.picker({
  2.     input: '#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="content-block-title">2 values and 3d-rotate effect</div>
  2. <div class="list-block">
  3.   <ul>
  4.     <li>
  5.       <div class="item-content">
  6.         <div class="item-inner">
  7.           <div class="item-input">
  8.             <input type="text" placeholder="Describe yourself" readonly id="picker-describe">
  9.           </div>
  10.         </div>
  11.       </div>
  12.     </li>
  13.   </ul>
  14. </div>
复制
  1. var pickerDescribe = myApp.picker({
  2.     input: '#picker-describe',
  3.     rotateEffect: true,
  4.     cols: [
  5.         {
  6.             textAlign: 'left',
  7.             values: ('Super Lex Amazing Bat Iron Rocket Lex Cool Beautiful Wonderful Raining Happy Amazing 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="content-block-title">Dependent values</div>
  2. <div class="list-block">
  3.  <ul>
  4.    <li>
  5.      <div class="item-content">
  6.        <div class="item-inner">
  7.          <div class="item-input">
  8.            <input type="text" placeholder="Your car" readonly id="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 = myApp.picker({
  7.     input: '#picker-dependent',
  8.     rotateEffect: true,
  9.     formatValue: function (picker, 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="content-block-title">Custom toolbar</div>
  2. <div class="list-block">
  3.  <ul>
  4.    <li>
  5.      <div class="item-content">
  6.        <div class="item-inner">
  7.          <div class="item-input">
  8.            <input type="text" placeholder="Describe yourself" readonly id="picker-custom-toolbar">
  9.          </div>
  10.        </div>
  11.      </div>
  12.    </li>
  13.  </ul>
  14. </div>
复制
  1. var pickerCustomToolbar = myApp.picker({
  2.     input: '#picker-custom-toolbar',
  3.     rotateEffect: true,
  4.     toolbarTemplate: 
  5.         '<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 close-picker">That\'s me</a>' +
  12.                 '</div>' +
  13.             '</div>' +
  14.         '</div>',
  15.     cols: [
  16.         {
  17.             values: ['Mr', 'Ms'],
  18.         },
  19.         {
  20.             textAlign: 'left',
  21.             values: ('Super Lex Amazing Bat Iron Rocket Lex Cool Beautiful Wonderful Raining Happy Amazing Funny Cool Hot').split(' ')
  22.         },
  23.         {
  24.             values: ('Man Luthor Woman Boy Girl Person Cutie Babe Raccoon').split(' ')
  25.         },
  26.     ],
  27.     onOpen: function (picker) {
  28.         picker.container.find('.toolbar-randomize-link').on('click', function () {
  29.             var col0Values = picker.cols[0].values;
  30.             var col0Random = col0Values[Math.floor(Math.random() * col0Values.length)];
  31.  
  32.             var col1Values = picker.cols[1].values;
  33.             var col1Random = col1Values[Math.floor(Math.random() * col1Values.length)];
  34.  
  35.             var col2Values = picker.cols[2].values;
  36.             var col2Random = col2Values[Math.floor(Math.random() * col2Values.length)];
  37.  
  38.             picker.setValue([col0Random, col1Random, col2Random]);
  39.         });
  40.     }
  41. });
复制

  Inline Picker / Date-time

  1. <div class="content-block-title">Inline Picker / Date-time</div>
  2. <div class="content-block">
  3.  <div style="padding:0; margin-right:-15px; width:auto" class="content-block-inner">
  4.    <div style="margin:0" class="list-block">
  5.      <ul style="border-top:none">
  6.        <li>
  7.          <div class="item-content">
  8.            <div class="item-inner">
  9.              <div class="item-input">
  10.                <input type="text" placeholder="Date Time" readonly id="picker-date">
  11.              </div>
  12.            </div>
  13.          </div>
  14.        </li>
  15.      </ul>
  16.    </div>
  17.    <div id="picker-date-container"></div>
  18.  </div>
  19. </div>
复制
  1. var today = new Date();
  2.  
  3. var pickerInline = myApp.picker({
  4.     input: '#picker-date',
  5.     container: '#picker-date-container',
  6.     toolbar: false,
  7.     rotateEffect: true,
  8.  
  9.     value: [today.getMonth(), today.getDate(), today.getFullYear(), today.getHours(), (today.getMinutes()  daysInMonth) {
  10.             picker.cols[1].setValue(daysInMonth);
  11.         }
  12.     },
  13.  
  14.     formatValue: function (p, values, displayValues) {
  15.         return displayValues[0] + ' ' + values[1] + ', ' + values[2] + ' ' + values[3] + ':' + values[4];
  16.     },
  17.  
  18.     cols: [
  19.         // Months
  20.         {
  21.             values: ('0 1 2 3 4 5 6 7 8 9 10 11').split(' '),
  22.             displayValues: ('January February March April May June July August September October November December').split(' '),
  23.             textAlign: 'left'
  24.         },
  25.         // Days
  26.         {
  27.             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],
  28.         },
  29.         // Years
  30.         {
  31.             values: (function () {
  32.                 var arr = [];
  33.                 for (var i = 1950; i
复制

 

1
1
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    一个很漂亮的基于纯js实现的color-picker控件源码例子程序

    "一个很漂亮的基于纯js实现的color-picker控件源码例子程序" 这个标题表明我们正在讨论一个使用纯JavaScript编写的颜色选择器(color-picker)控件。这个控件的设计和功能都非常吸引人,是用于网页应用的色彩选择...

    lb-picker选择器示例.rar

    在uni-app框架中,lb-picker选择器是一种常用的交互组件,用于实现用户在多个选项中进行选择的功能。这个组件是基于JavaScript和Vue.js技术构建的,适用于移动应用开发,特别是那些需要用户输入特定信息如日期、时间...

    city-picker城市选择插件

    城市选择插件“city-picker”是一款广泛应用于网页开发中的组件,尤其在处理涉及地理位置和区域选择的场景下显得尤为重要。这款插件基于JavaScript和jQuery库,使得在网页上实现便捷的城市、区县、街道级别的选择变...

    uni-app uni-data-picker 省市区数据表

    &lt;uni-data-picker&gt; 是一个选择类datacom组件。支持多列级联选择。列数没有限制,如果屏幕显示不全,顶部tab区域会左右滚动。 适用于uni-app使用uni-data-picker实现省市区选择器的json数据

    react-native-image-picker 的使用方法demo

    `react-native-image-picker` 是一个流行的React Native库,用于在iOS和Android应用中集成图像选择功能,包括从相册选择图片、拍摄照片或录制视频。在这个项目中,我们将深入探讨如何使用`react-native-image-picker...

    elementui elementUI - date-picker.zip

    ElementUI 是一个基于 Vue.js 的开源 UI 组件库,它为开发者提供了丰富的界面元素和设计模式,便于构建美观且响应式的 Web ...在实际开发中,务必参考官方文档以获取最新的 API 和最佳实践,确保代码的稳定性和兼容性。

    city-picker 城市选择插件

    城市选择插件“city-picker”是一款广泛应用于网页开发中的组件,它主要功能是为用户提供一个交互式的、方便快捷的城市选择界面。在当前信息化社会中,无论是电商网站、地图服务还是各种本地生活应用,都需要这样的...

    react-native-modal-datetime-picker, 针对Android和iOS的本地日期时间选取器.zip

    react-native-modal-datetime-picker, 针对Android和iOS的本地日期时间选取器 react-native-modal-datetime-picker 声明性交叉平台响应本地日期时间选取器。描述这个库公开了一个跨平台接口,用于显示本机日期选取器...

    react-native-picker,一、安卓、iOS.zip

    React-Native-Picker 是一个流行的开源项目,专为React Native平台设计,用于在Android和iOS应用中实现选择器功能。这个项目提供了与原生平台高度兼容的下拉选择器组件,使得开发者能够轻松地在跨平台上创建一致的...

    一个简约美观的月份选择器插件month-picker

    在网页开发中,日期和时间的选择是常见的交互元素,尤其在表单填写、日历应用等场景下尤为重要。`month-picker`是一款基于jQuery的月份选择器插件,它提供了简洁且美观的界面,让用户能够轻松选择年份和月份,简化了...

    vue element-ui el-date-picker限制选择时间为当天之前的代码

    在使用Vue.js框架开发前端界面时,经常会用到Element UI库中的组件来实现复杂的交互功能。Element UI是由饿了么前端团队开源的一套基于Vue 2.0的桌面端组件库,广泛应用于Web开发中。其中,`el-date-picker`组件是一...

    Android-react-native-image-crop-picker.zip

    Android-react-native-image-crop-picker.zip,iOS/Android图像选择器,支持相机、视频、可配置压缩、多个图像和裁剪,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核...

    el-data-picker季度区间选择器

    "el-data-picker季度区间选择器"就是这样一个组件,它专为基于ElementUI 2.15.8和Vue.js 2框架的应用设计。这个组件允许用户方便地选取特定时间范围内的季度,提高用户在数据过滤、检索或分析时的效率。 ElementUI...

    android-image-picker,Image Picker for Android

    总的来说,"android-image-picker"是一个强大且易于使用的开源组件,它极大地简化了Android应用中图片选择的实现过程,帮助开发者节省时间和精力,专注于核心业务逻辑的开发。对于任何需要在Android应用中实现图片...

    react-native-modal-datetime-picker:适用于Android和iOS的React-Native datetime-picker

    该库公开了一个跨平台的界面,用于显示模式内部的本机日期选择器和时间选择器,从而提供统一的用户和开发人员体验。 在后台,该库使用 。 设置(适用于非EXPO项目) 如果您的项目未使用 ,请使用npm或yarn安装库...

    jQuery省市区三级联动插件city-picker

    **jQuery省市区三级联动插件city-picker详解** 在网页开发中,经常需要实现省市区三级联动的效果,即用户选择一个省份后,相应的城市列表自动更新,再选择城市时,对应的区县列表也会随之变化。这种功能可以提升...

    最全 element UI el-date-picker组件换色(less)

    element UI el-date-picker组件样式更改 popper-class="down-time" 原码是body下的弹出器

    uview u-picker的省市区json文件

    uview u-picker的省市区json文件

    react-native-document-picker, 使用文档提供程序来响应本机的文档选取器.zip

    react-native-document-picker, 使用文档提供程序来响应本机的文档选取器 react-native-document-picker苹果 UIDocumentMenuViewController 和 Intent.ACTION_OPEN_DOCUMENT/Intent.ACTION_PICK的本地包装器。...

    el-date-picker 英文改中文.doc

    https://blog.csdn.net/xuelang532777032/article/details/121287203,内有博客截图,方便没有vip的小伙伴下载

Global site tag (gtag.js) - Google Analytics