Checkbox多选框

    • 在一组可选项中进行多项选择时;
    • 单独使用可以表示两种状态之间的切换,和 类似。区别在于切换 switch 会直接触发状态改变,而 checkbox 一般用于状态标记,需要和提交操作配合。

    想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

    简单的 checkbox。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-checkbox-basic',
    4. template: `
    5. <label nz-checkbox [(ngModel)]="checked">Checkbox</label>
    6. `
    7. })
    8. export class NzDemoCheckboxBasicComponent {
    9. checked = true;
    10. }

    Checkbox多选框 - 图2

    受控的 Checkbox

    联动 checkbox。

    在实现全选效果时,你可能会用到 nzIndeterminate 属性。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-checkbox-check-all',
    4. template: `
    5. <div style="border-bottom: 1px solid rgb(233, 233, 233);">
    6. <label
    7. nz-checkbox
    8. [(ngModel)]="allChecked"
    9. (ngModelChange)="updateAllChecked()"
    10. [nzIndeterminate]="indeterminate"
    11. >
    12. Check all
    13. </label>
    14. </div>
    15. <br />
    16. `
    17. })
    18. export class NzDemoCheckboxCheckAllComponent {
    19. allChecked = false;
    20. indeterminate = true;
    21. checkOptionsOne = [
    22. { label: 'Apple', value: 'Apple', checked: true },
    23. { label: 'Pear', value: 'Pear', checked: false },
    24. { label: 'Orange', value: 'Orange', checked: false }
    25. ];
    26. updateAllChecked(): void {
    27. this.indeterminate = false;
    28. if (this.allChecked) {
    29. return {
    30. ...item,
    31. checked: true
    32. };
    33. });
    34. } else {
    35. this.checkOptionsOne = this.checkOptionsOne.map(item => {
    36. return {
    37. ...item,
    38. checked: false
    39. };
    40. });
    41. }
    42. }
    43. updateSingleChecked(): void {
    44. if (this.checkOptionsOne.every(item => item.checked === false)) {
    45. this.allChecked = false;
    46. this.indeterminate = false;
    47. } else if (this.checkOptionsOne.every(item => item.checked === true)) {
    48. this.allChecked = true;
    49. this.indeterminate = false;
    50. } else {
    51. }
    52. }
    53. }

    Checkbox多选框 - 图4

    不可用

    checkbox 不可用。

    方便的从数组生成 Checkbox 组。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-checkbox-group',
    4. template: `
    5. <nz-checkbox-group [(ngModel)]="checkOptionsOne" (ngModelChange)="log(checkOptionsOne)"></nz-checkbox-group>
    6. <br />
    7. <br />
    8. <nz-checkbox-group [(ngModel)]="checkOptionsTwo" (ngModelChange)="log(checkOptionsTwo)"></nz-checkbox-group>
    9. <br />
    10. <br />
    11. <nz-checkbox-group [(ngModel)]="checkOptionsThree" (ngModelChange)="log(checkOptionsThree)"></nz-checkbox-group>
    12. `
    13. })
    14. export class NzDemoCheckboxGroupComponent {
    15. checkOptionsOne = [
    16. { label: 'Apple', value: 'Apple', checked: true },
    17. { label: 'Pear', value: 'Pear' },
    18. ];
    19. checkOptionsTwo = [
    20. { label: 'Apple', value: 'Apple' },
    21. { label: 'Pear', value: 'Pear', checked: true },
    22. { label: 'Orange', value: 'Orange' }
    23. ];
    24. checkOptionsThree = [
    25. { label: 'Apple', value: 'Apple', disabled: true, checked: true },
    26. { label: 'Pear', value: 'Pear', disabled: true },
    27. { label: 'Orange', value: 'Orange' }
    28. ];
    29. log(value: object[]): void {
    30. console.log(value);
    31. }
    32. }

    布局

    nz-checkbox-wrapper 内嵌 nz-checkbox 并与 Grid 组件一起使用,可以实现灵活的布局。

    通过ViewChild或其他方式获得 实例