jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明

/******************HTML***********************/

<input />

<button >Clear</button>

<button >Show</button>

/*****************Javascript**********************/

$(function(){

$('#demo').mobiscroll().date({

theme: 'default',

lang: 'zh',

display: 'bottom',

mode: 'scroller',

invalid: [ 'w0', 'w6', '5/1', '12/24', '12/25' ]

});

$('#show').click(function(){

$('#demo').mobiscroll('show');

return false;

});

$('#clear').click(function () {

$('#demo').val('');

return false;

});

});

/***************初始化控件选项*******************/

//初始化日期控件

var opt = {

preset: 'date', //日期

theme: 'jqm', //皮肤样式

display: 'modal', //显示方式

mode: 'clickpick', //日期选择模式

dateFormat: 'yy-mm-dd', // 日期格式

setText: '确定', //确认按钮名称

cancelText: '取消',//取消按钮名籍我

dateOrder: 'yymmdd', //面板中日期排列格式

dayText: '日', monthText: '月', yearText: '年', //面板中年月日文字

endYear:2020 //结束年份

};

$('input:jqmData(role="datebox")').mobiscroll(opt);

/***************************************************************/

Mobiscroll Core Documentation

These are the core functions, options, events and methods that are available, if they are not overriden by a preset implementation.

Utility functions

NameParametersDescription
setDefaultsfunction(settings)Set settings for all scroller instances

Localization

NameTypeDefault valueDescription
cancelTextString'Cancel'Text for Cancel button.
clearTextString'Clear'Text for Clear button.
setTextString'Set'Text for Set button.
headerTextMixed'{value}'Specifies a custom string which appears in the popup header. If the string contains '{value}' substring, it is replaced with the formatted value of the scroller. If it's set to false, the header is hidden. If a function is passed, it receives the formatted value as parameter and the returned value appears in the header
langString'en-US'Language of the scroller. Based on the language string the scroller loads the language based default settings from the language modules.

Supported languages:

  • 'en-US' or undefined - English
  • 'zh' - Chinese
  • 'de' - German
  • 'es' - Spanish
  • 'fr' - French
  • 'it' - Italian
  • 'ja' - Japanese
  • 'hu' - Hungarian
  • 'nl' - Dutch
  • 'no' - Norwegian
  • 'pt-BR' - Brazilian Portuguese
  • 'pt-PT' - European Portuguese
  • 'sv' - Swedish
  • 'tr' - Turkish

Options

NameTypeDefault valueDescription
anchorjQuery ObjectundefinedSpecifies the anchor element for positioning, if display is set to 'bubble'. If undefined, it defaults to the element on which the scroller was called.
animateStringundefinedAnimation to use for show/hide (if display is not inline). Possible values:
  • 'fade'
  • 'flip'
  • 'pop'
  • 'swing'
  • 'slidevertical'
  • 'slidehorizontal'
  • 'slidedown'
  • 'slideup'
Requires the animation plugin.
buttonsArray['set', 'cancel']Buttons to display. Predefined buttons are: 'set', 'clear', 'cancel'. Custom buttons can also be specified:
buttons: [     'set',    { text: 'Custom', handler: function (event, inst) { alert('Custom button clicked!'); } }    'cancel']                
closeOnOverlayBooleantrueIf true, scroller is closed on overlay tap/click.
contextString'body'Context in which mobiscroll is appended and positioned (if not inline). Can be a jQuery selector string or a jQuery object.
delayInteger300Specifies the speed in milliseconds to change values in clickpick mode with tap & hold
disabledBooleanfalseDisables (true) or enables (false) the scroller. Can be set when initialising the scroller
displayString'modal'Controls the positioning of the scroller. Possible options:
  • 'modal' - Scroller appears as a popup at the center of the viewport.
  • 'inline' - If called on div element, scroller is placed inside the div (overwriting existing content), otherwise is placed after the original element.
  • 'bubble' - Scroller appears as a bubble positioned to the element defined by the 'anchor' setting. By default the anchor is the original element.
  • 'top' - Scroller appears docked to the top of the viewport.
  • 'bottom' - Scroller appears docked to the bottom of the viewport.
fixedWidthNumber or ArrayundefinedWidth of the wheels. If number, it is applied to all wheels, if an array, it is applied to each wheel sepparately.
heightNumber40Height in pixels of one row on the wheel
maxWidthNumber or ArrayundefinedMaximum width of the wheels. If number, it is applied to all wheels, if an array, it is applied to each wheel sepparately.
minWidthNumber or ArrayundefinedMinimum width of the wheels. If number, it is applied to all wheels, if an array, it is applied to each wheel sepparately.
modeString'scroller'Option to choose between modes. Possible modes: 'scroller' - standard behaviour, 'clickpick' - '+' and '-' buttons
presetString''Load preset configurations. It is possible to write custom presets.
scrollLockBooleantrueDisables page scrolling on touchmove (if not in inline mode)

At the time of 2.1-beta on Android 2.3 fast touchmoves sometimes causes pagescroll which may cause that the scroller gets out of the viewport.

readonlyBoolean or Array of BooleansfalseIf true, the scroller is displayed, but wheels are not moveable. If an array, it can be specified as a per wheel configuration, e.g. for 3 wheels: [true, false, false] - disables the first wheel.
rowsNumber3Number of visible rows on the wheel
showLabelBooleantrueShow/hide labels above wheels
showOnFocusBooleantruePops up the scroller on input focus
showOnTapBooleantruePops up the scroller on element tap
tapBooleantrueFire button actions on tap instead of click (on touch devices)
themeString''Sets the scroller's visual appearance. Supplied themes:
  • 'android' - Android 2.x theme
  • 'android-ics' - Android 4.x theme
  • 'android-ics light' - Android 4.x theme (light version)
  • 'bootstrap' - Bootstrap themes
  • 'ios' - iOS theme
  • 'ios7' - iOS 7 theme
  • 'jqm' - Integrates with jQuery Mobile look & feel
  • 'sense-ui' - HTC Android Sense UI theme
  • 'wp' - Windows Phone Metro UI theme
  • 'wp light' - Windows Phone Metro UI theme (light version)
It's possible to create custom themes in css by prefixing any css class used in the scroller markup with the theme name, e.g.: .my-theme .dww { / My CSS / }, and set the theme option to 'my-theme'
wheelsObject[]Wheels configuration. If keys array is omitted, the values array is used for keys as well.

Starting from 2.6.0 a new format was introduced for passing the wheels. The old format is still supported but it is considered deprecated and will be removed in the future.

Below is an example with 2 wheel groups, each containing 2 wheels.

wheels: [ // Wheel groups array    [ // First wheel group        { // Wheel object            label: 'Label 1',             keys: [1, 2, 3],              values: ['x', 'y', 'z'],         },         { // Wheel object            label: 'Label 2',             keys: [1, 2],              values: ['a', 'b'],         }     ],     [ // Second wheel group        { // Wheel object            label: 'Label 3',             values: [1, 2],         },         { // Wheel object             label: 'Label 4',             values: [4, 5],         }     ]]
widthNumber80Minimum width in pixels of the wheels, expand to fit values and labels

Theme options

NameTypeDefault valueDescription
jQuery Mobile Theme
jqmBorderString'a'Swatch for picker border.
jqmBodyString'c'Swatch for picker body.
jqmHeaderString'b'Swatch for picker header.
jqmWheelString'd'Swatch for wheel background (clickpick mode only).
jqmClickPickString'c'Swatch for clickpick buttons.
jqmSetString'b'Swatch for set button.
jqmCancelString'c'Swatch for cancel button.
Windows Phone Theme
accentString'none'Specifies the accent color of the theme. Possible values: 'lime', 'green', 'emerald', 'teal', 'cyan', 'cobalt', 'indigo', 'violet', 'pink', 'magenta', 'crimson', 'red', 'orange', 'amber', 'yellow', 'brown', 'olive', 'steel', 'mauve', 'sienna'.

Setting options runtime

There are two ways to modify options after initalization

  1. Using the option method.

    The option method always triggers reinitialization. Most of the settings can be updated only this way, updating without initialization has no effect, because the markup is already generated. If the scroller was visible, the reinitialization hides it and it's not shown again automatically (except in inline display mode).

    // Modify one option$('#scroller').mobiscroll('option', 'mode', 'clickpick');// Modify multiple options$('#scroller').mobiscroll('option', { mode: 'clickpick', rows: 5 });
  2. Modify directly the inst.settings object.

    Useful when changing dynamic settings, which do not need redraw (e.g. readonly, calendar marked days).

    // Get instrance and modify a settingvar inst = $('#scroller').mobiscroll('getInst');inst.settings.readonly = true;// Modify settings in an event$('#scroller').mobiscroll({    onBeforeShow: function (inst) {        inst.settings.readonly = true;    }});

Conversion functions

NameParametersDescription
formatResultfunction(data)Receives selected scroller values as an array and must return a string to set as the value of the input element. By default it concatenates the values to a string, or, if a preset is selected, formats the date/time according to dateFormat/timeFormat option
parseValuefunction(valueText)Receives a string as parameter and must return an array which represents the selected scroller values.

Events

NameParametersDescription
onBeforeShowfunction(inst)Gets called before the scroller appears. It is usefull if you want to modify the settings object before generating the markup (e.g. change the wheel values). The function receives the scroller instance as parameters
onCancelfunction(valueText, inst)Allows you to define your own event when cancel is pressed. The function receives the selected value as text and the scroller instance as parameters
onChangefunction(valueText, inst)Allows you to define your own event when a wheel value is changed. The function receives the selected value as text and the scroller instance as parameters
onClosefunction(valueText, btn, inst)Allows you to define your own event when the scroller is closed. The function receives the selected value as text, the button, which triggered the scroller to close ('set', or 'cancel') and the scroller instance as parameters. If returns false, close is prevented.
onDestroyfunction(inst)Gets called when the mobiscroll instance is destroyed. The function receives the scroller instance as parameter.
onMarkupReadyfunction(html, inst)Allows you to define your own event when the html markup of the scroller is generated, but it is not yet shown. It is usefull, if you want to make modifications to the markup (e.g. add custom elements), before the positioning runs. The function receives the jQuery object containing the generated html and the scroller instance as parameters. If returns false, close is prevented.
onPositionfunction(html, inst)Gets called when the scroller is positioned (on initial show and resize / orientation change). The function receives the jQuery object containing the generated html and the scroller instance as parameters
onSelectfunction(valueText, inst)Allows you to define your own event when a scroller value is set. The function receives the selected value as text and the scroller instance as parameters
onShowfunction(html, valueText, inst)Gets called when the scroller appears. The function receives the jQuery object containing the generated html, the formatted value and the scroller instance as parameters
onValueTapfunction(item, inst)Gets called when the user taps on a value on the wheel. The function receives the jQuery object containing the html of the tapped item and the scroller instance as parameters
validatefunction(html, index, inst)Gets called on initialization and on every wheel change, can be used to validate the selected values. Has 3 parameters: html - jQuery object containing the generated html, index - index of the changed wheel (undefined on initial call), inst - mobiscroll instance

Methods

NameParametersDescription
cancel.mobiscroll('cancel')Hides the scroller and also invokes the onCancel event.
changeWheel.mobiscroll('changeWheel', indexes, time)Regenerates the wheel with the given indexes based on the option. Usefull if you want to change a wheel dinamically based on another wheel's currently selected value. The time parameter specifies the animation time to scroll the new wheel to the selected value.
destroy.mobiscroll('destroy')Remove the scroller functionality completely. This will return the element back to its pre-init state.
disable.mobiscroll('disable')Disables the scroller.
enable.mobiscroll('enable')Enables the scroller.
getInst.mobiscroll('getInst')Returns the object instance.
getValue.mobiscroll('getValue')Returns the selected scroller values as an array.
getValues.mobiscroll('getValues')Returns the list of selected values if multiple selection is implemented in the preset.
hide.mobiscroll('hide', prevAnim, btn)Hides the scroller. If prevAnim is true, hide will not be animated. The optional btn parameter specifies which button caused the scroller to hide, and it's passed to the onClose event.
init.mobiscroll('init', settings)Initializes the scroller. Gets called when creating a new scroller instance, or updating the options of an existing scroller instance with the settings parameter.
isVisible.mobiscroll('isVisible')Returns a boolean indicating whether the scroller is visible or not.
option.mobiscroll('option', optionName, value) or .mobiscroll('option', options)Set scroller options
position.mobiscroll('position', check)Recalculates scroller position (if not inline). If check is true, the function checks if window size changed after last position call, if false or ommitted, position is recalculated anyway.
select.mobiscroll('select')Hides the scroller and also invokes the onSelect event.
setValue.mobiscroll('setValue', values, fill, time, temp)Sets the scroller values from the values parameter passed as array. If the fill parameter is true, the associated input field is also updated with the new value. The time parameter specifies the duration of the animation in seconds to scroll the wheels to the new values. There is no animation, if time is omitted or 0. If temp is true, only temporary values are set.
show.mobiscroll('show', prevAnim)Shows the scroller. If prevAnim is true, show will not be animated.
tap.mobiscroll('tap', el, handler)Attaches the handler function to the tap event of element el. If the action was initiated with touch event, handler is called on touchend, otherwise on click.
trigger.mobiscroll('trigger', name, params)Calls the name event with params (Array) as parameters, if it is defined in preset, theme or user settings. The instance is always added to the end of the parameter list.

Need help? Ask us and get professional help!

Email

Message

Your Request has been submitted

We'd like to let you know that we cannot give a guarantee on the response time.

If you'd like expedited support, you can purchase a support ticket or get a support subscription. We'll make sure you get a response up to the next business day.

close