官术网_书友最值得收藏!

Working with dates using DatePicker

This recipe describes how to make use of the date picker form field provided by Sencha Touch, which allows the user to select a date.

Getting ready

Make sure that you have set up your development environment by following the recipes outlined in Chapter 1.

Make sure that you have followed the Getting your form ready with FormPanel recipe in this chapter.

How to do it...

Carry out the following steps:

  1. Copy ch02_01.js to ch02_04.js.
  2. Open a new file named ch02_04.js and replace the definition of formBase with the following code:
    var formBase = {
      items: [{
        xtype: 'datepickerfield',
        name: 'date',
        label: 'Date'
      }]
    };
  3. Include ch02_04.js in place of ch02_03.js in index.html.
  4. Deploy and access the application in the browser.

How it works...

The date picker field can be constructed by using the Ext.form.DatePicker class instance or by using xtype: datepickerfield. The date picker form field implements HTML <select> . When the user tries to select an entry, it shows the date picker with the month, day, and year slots for selection. After selection, when the user clicks on the Done button, the field is set with the selected value.

There's more...

Additionally, there are other things that can be done such as setting the date to the current date, or any particular date, or changing the order of appearance of a month, day, and year. Let's see what it takes to accomplish this.

Setting the default date to the current date

In order to set the default value to the current date, the value property must be set to the current date. The following code shows how to do it:

var formBase = {
  items: [{
    xtype: 'datepickerfield',
    name: 'date',
    label: 'Date',
    value: new Date(),

Setting the default date to a particular date

The default date is 01/01/1970. Let's assume that you need to set the date to a different date, but not the current date. To do so, you will have to set the value using the year, month, and day properties, as follows:

var formBase = {
  items: [{
    xtype: 'datepickerfield',
    name: 'date',
    label: 'Date',
    value: {year: 2011, month: 6, day: 11},
…

Changing the slot order

By default, the slot order is month, day, and year. You can change it by setting the slotOrder property of the picker property of date picker, as shown in the following code:

var formBase = {
  items: [{
    xtype: 'datepickerfield',
    name: 'date',
    label: 'Date',
    picker: {slotOrder: ['day', 'month', 'year']}
  }]
};

Setting the picker date range

By default, the date range shown by the picker is 1970 until the current year. For our application need, if we have to alter the year range, we can do so by setting the yearFrom and yearTo properties of the picker property of the date picker, as follows:

var formBase = {
  items: [{
    xtype: 'datepickerfield',
    name: 'date',
    label: 'Date',
    picker: {yearFrom: 2000, yearTo: 2010}
  }]
};

See also

  • The recipe named Setting up the Android-based development environment in Chapter 1
  • The recipe named Setting up the iOS-based development environment in Chapter 1
  • The recipe named Setting up the Blackberry-based development environment in Chapter 1
  • The recipe named Setting up the browser-based development environment in Chapter 1
  • The recipe named Setting up the production environment in Chapter 1
  • The recipe named Getting your form ready with FormPanel in this chapter
主站蜘蛛池模板: 丹江口市| 尤溪县| 德昌县| 南丰县| 股票| 新巴尔虎左旗| 马山县| 东平县| 城口县| 阿勒泰市| 甘洛县| 密山市| 佛冈县| 南开区| 泉州市| 百色市| 平安县| 镇江市| 水城县| 弥渡县| 辽阳市| 定西市| 财经| 兴海县| 新泰市| 道孚县| 日喀则市| 巴彦县| 迁西县| 宁强县| 合作市| 涪陵区| 宜阳县| 北宁市| 耿马| 增城市| 临沭县| 响水县| 西宁市| 顺义区| 朝阳区|