ts-basic-components-checkboxgroup.md 3.2 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 6 7 8
> **NOTE**
>
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.

E
esterzhou 已提交
9 10 11 12 13 14
## Required Permissions

None

## Child Components

E
ester.zhou 已提交
15
Not supported
E
esterzhou 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

## 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.

- Parameters
  | Name| Type| Mandatory| Default Value| Description|
  | -------- | -------- | -------- | -------- | -------- |
  | group | string | No| - | Group name.|


## Attributes

E
ester.zhou 已提交
31
| Name| Type| Default Value| Description|
E
esterzhou 已提交
32
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
33 34
| selectAll | boolean | false | Whether to select all.|
| selectedColor | Color | - | Color of the selected check box.|
E
esterzhou 已提交
35 36 37

## Events

E
ester.zhou 已提交
38
| Name| Description|
E
esterzhou 已提交
39
| -------- | -------- |
E
ester.zhou 已提交
40
| 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 已提交
41 42

- SelectStatus enums
E
ester.zhou 已提交
43
  | Name | Description|
E
esterzhou 已提交
44 45 46 47 48 49 50 51
  | ----- | -------------------- |
  | 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.|


## Example

E
ester.zhou 已提交
52 53
```ts
// xxx.ets
E
esterzhou 已提交
54 55 56 57 58 59 60
@Entry
@Component
struct CheckboxExample {

  build() {
    Scroll() {
      Column() {
61 62 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
        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 已提交
99 100 101 102 103 104
      }
    }
  }
}
```
![](figures/checkboxgroup.gif)