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

Checkbox and Checkbox group

Checkboxes permit the user to make multiple selections from a number of available options. It is a convenient way to learn about user choices. For example, in an application, we may have a checkbox asking the user if he/she stayed in Hyderabad. Moreover, if we are capturing the detail about multiple cities where the user had stayed, then we would group multiple checkboxes under one name and use them as a checkbox group. In this recipe, we will see how we can create a checkbox and a checkbox group using Sencha Touch and how to handle the values when you want to set them, or when the form data is posted.

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_09.js.
  2. Open a new file named ch02_09.js and replace the definition of formBase with the following code:
    var formBase = {
      items: [{
        xtype: 'checkboxfield',
        name: 'city',
        value: 'Hyderabad',
        label: 'Hyderabad',
        checked: true
        }, {
          xtype: 'checkboxfield',
          name: 'city',
          value: 'Mumbai',
          label: 'Mumbai'
      }]
    };
  3. Include ch02_09.js in place of ch02_08.js in index.html.
  4. Deploy and access the application in the browser. You will see the checkboxes as shown in the following screenshot:
    How to do it...

How it works...

The preceding code creates two checkboxes inside the form panel. checked: true checks the checkbox when it is rendered. When a form is submitted, the checkbox values are returned as an array. For example, given the preceding code, when the user clicks on Submit, city would have two values, as follows:

city: ['Hyderabad', 'Mumbai']

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
主站蜘蛛池模板: 博乐市| 琼结县| 扬州市| 新疆| 常山县| 界首市| 杭州市| 天等县| 新化县| 漠河县| 松桃| 大埔县| 梅河口市| 扬州市| 交城县| 布尔津县| 南岸区| 霸州市| 林州市| 同心县| 孙吴县| 汾西县| 连山| 凌云县| 嘉峪关市| 界首市| 泉州市| 昆明市| 松桃| 金寨县| 宜城市| 留坝县| 通渭县| 西盟| 彰化县| 平安县| 冕宁县| 隆尧县| 岐山县| 神农架林区| 灵丘县|