Checkbox多选框
- 在一组可选项中进行多项选择时;
- 单独使用可以表示两种状态之间的切换,和 类似。区别在于切换
switch
会直接触发状态改变,而checkbox
一般用于状态标记,需要和提交操作配合。
想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。
简单的 checkbox。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-checkbox-basic',
template: `
<label nz-checkbox [(ngModel)]="checked">Checkbox</label>
`
})
export class NzDemoCheckboxBasicComponent {
checked = true;
}
联动 checkbox。
在实现全选效果时,你可能会用到 nzIndeterminate
属性。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-checkbox-check-all',
template: `
<div style="border-bottom: 1px solid rgb(233, 233, 233);">
<label
nz-checkbox
[(ngModel)]="allChecked"
(ngModelChange)="updateAllChecked()"
[nzIndeterminate]="indeterminate"
>
Check all
</label>
</div>
<br />
`
})
export class NzDemoCheckboxCheckAllComponent {
allChecked = false;
indeterminate = true;
checkOptionsOne = [
{ label: 'Apple', value: 'Apple', checked: true },
{ label: 'Pear', value: 'Pear', checked: false },
{ label: 'Orange', value: 'Orange', checked: false }
];
updateAllChecked(): void {
this.indeterminate = false;
if (this.allChecked) {
return {
...item,
checked: true
};
});
} else {
this.checkOptionsOne = this.checkOptionsOne.map(item => {
return {
...item,
checked: false
};
});
}
}
updateSingleChecked(): void {
if (this.checkOptionsOne.every(item => item.checked === false)) {
this.allChecked = false;
this.indeterminate = false;
} else if (this.checkOptionsOne.every(item => item.checked === true)) {
this.allChecked = true;
this.indeterminate = false;
} else {
}
}
}
checkbox 不可用。
方便的从数组生成 Checkbox 组。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-checkbox-group',
template: `
<nz-checkbox-group [(ngModel)]="checkOptionsOne" (ngModelChange)="log(checkOptionsOne)"></nz-checkbox-group>
<br />
<br />
<nz-checkbox-group [(ngModel)]="checkOptionsTwo" (ngModelChange)="log(checkOptionsTwo)"></nz-checkbox-group>
<br />
<br />
<nz-checkbox-group [(ngModel)]="checkOptionsThree" (ngModelChange)="log(checkOptionsThree)"></nz-checkbox-group>
`
})
export class NzDemoCheckboxGroupComponent {
checkOptionsOne = [
{ label: 'Apple', value: 'Apple', checked: true },
{ label: 'Pear', value: 'Pear' },
];
checkOptionsTwo = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Pear', value: 'Pear', checked: true },
{ label: 'Orange', value: 'Orange' }
];
checkOptionsThree = [
{ label: 'Apple', value: 'Apple', disabled: true, checked: true },
{ label: 'Pear', value: 'Pear', disabled: true },
{ label: 'Orange', value: 'Orange' }
];
log(value: object[]): void {
console.log(value);
}
}
nz-checkbox-wrapper
内嵌 nz-checkbox
并与 Grid 组件一起使用,可以实现灵活的布局。
通过ViewChild或其他方式获得 实例