ts-basic-components-checkboxgroup.md 3.3 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 15 16 17 18

## APIs

CheckboxGroup( group?: string )

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 34
| 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**|
| selectedColor | [ResourceColor](../../ui/ts-types.md) | Color of the selected check box.|
E
esterzhou 已提交
35 36 37

## Events

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

E
ester.zhou 已提交
40
| Name| Description|
E
esterzhou 已提交
41
| -------- | -------- |
E
ester.zhou 已提交
42
| onChange (callback: (names: Array&lt;string&gt;, status: SelectStatus) => void ) |Triggered when the selection status of the check box group or any check box wherein changes.<br>- **names**: names of all selected check boxes in the group.<br>- **status**: selection status.|
E
esterzhou 已提交
43

E
ester.zhou 已提交
44 45 46 47 48 49 50
## 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 已提交
51 52 53 54


## Example

E
ester.zhou 已提交
55 56
```ts
// xxx.ets
E
esterzhou 已提交
57 58 59 60 61 62
@Entry
@Component
struct CheckboxExample {
  build() {
    Scroll() {
      Column() {
63 64 65 66 67 68 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
        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 已提交
101 102 103 104 105 106
      }
    }
  }
}
```
![](figures/checkboxgroup.gif)