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

  • Sencha Touch Cookbook
  • Ajit Kumar
  • 561字
  • 2021-08-20 15:56:08

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
主站蜘蛛池模板: 新营市| 阳信县| 贵南县| 察哈| 永丰县| 山丹县| 勐海县| 青龙| 旅游| 政和县| 即墨市| 连江县| 清水河县| 乐陵市| 津市市| 溆浦县| 弋阳县| 措勤县| 晴隆县| 屏山县| 广州市| 黑河市| 昌都县| 古丈县| 塘沽区| 英德市| 秦皇岛市| 华蓥市| 斗六市| 章丘市| 宁河县| 南召县| 九江县| 湟中县| 河曲县| 平阴县| 南充市| 容城县| 东港市| 博兴县| 县级市|