ts-basic-components-checkboxgroup.md 3.4 KB
Newer Older
E
esterzhou 已提交
1 2 3 4
# CheckboxGroup

The **\<CheckboxGroup>** component is used to select or deselect all check boxes in a group.

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
E
esterzhou 已提交
8 9 10

## Child Components

E
ester.zhou 已提交
11
None
E
esterzhou 已提交
12 13 14

## APIs

E
ester.zhou 已提交
15
CheckboxGroup(options?: { group?: string })
E
esterzhou 已提交
16 17 18

Creates a check box group so that you can select or deselect all check boxes in the group at the same time. Check boxes and the check box group that share the group name belong to the same group.

E
ester.zhou 已提交
19
**Parameters**
E
esterzhou 已提交
20 21 22



E
ester.zhou 已提交
23
| Name| Type| Mandatory| Description|
E
esterzhou 已提交
24
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
25 26 27 28 29 30 31 32 33
| group | string | No| Group name.|

## Attributes

In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.

| Name| Type| Description|
| -------- | -------- | -------- |
| selectAll | boolean | Whether to select all.<br>Default value: **false**|
E
ester.zhou 已提交
34
| selectedColor | [ResourceColor](ts-types.md#resourcecolor) | Color of the selected check box.|
E
esterzhou 已提交
35 36 37

## Events

E
ester.zhou 已提交
38
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
E
ester.zhou 已提交
39

E
ester.zhou 已提交
40
| Name| Description|
E
esterzhou 已提交
41
| -------- | -------- |
E
ester.zhou 已提交
42 43 44 45 46 47 48
| onChange (callback: (event: CheckboxGroupResult) => void ) |Triggered when the selection status of the check box group or any check box wherein changes.|

## CheckboxGroupResult
| Name    | Type  | Description     |
| ------ | ------ | ------- |
| name   | Array&lt;string&gt; | Names of all the selected check boxes in the group.|
| status | SelectStatus | Selected status.|
E
esterzhou 已提交
49

E
ester.zhou 已提交
50 51 52 53 54 55 56
## SelectStatus

| Name | Description|
| ----- | -------------------- |
| All   | All check boxes in the group are selected.|
| Part  | Some check boxes in the group are selected.|
| None  | None of the check boxes in the group are selected.|
E
esterzhou 已提交
57 58 59 60


## Example

E
ester.zhou 已提交
61 62
```ts
// xxx.ets
E
esterzhou 已提交
63 64 65 66 67 68
@Entry
@Component
struct CheckboxExample {
  build() {
    Scroll() {
      Column() {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
          CheckboxGroup({ group: 'checkboxGroup' })
            .selectedColor(0xed6f21)
            .onChange((itemName: CheckboxGroupResult) => {
              console.info("TextPicker::dialogResult is" + JSON.stringify(itemName))
            })
          Text('select all').fontSize(20)
        }

        Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
          Checkbox({ name: 'checkbox1', group: 'checkboxGroup' })
            .select(true)
            .selectedColor(0x39a2db)
            .onChange((value: boolean) => {
              console.info('Checkbox1 change is' + value)
            })
          Text('Checkbox1').fontSize(20)
        }

        Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
          Checkbox({ name: 'checkbox2', group: 'checkboxGroup' })
            .select(false)
            .selectedColor(0x39a2db)
            .onChange((value: boolean) => {
              console.info('Checkbox2 change is' + value)
            })
          Text('Checkbox2').fontSize(20)
        }

        Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
          Checkbox({ name: 'checkbox3', group: 'checkboxGroup' })
            .select(true)
            .selectedColor(0x39a2db)
            .onChange((value: boolean) => {
              console.info('Checkbox3 change is' + value)
            })
          Text('Checkbox3').fontSize(20)
        }
E
esterzhou 已提交
107 108 109 110 111 112
      }
    }
  }
}
```
![](figures/checkboxgroup.gif)