Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:table): radio selection type #8823

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5c11a42
fix(module:tabs): wrong cursor
ParsaArvanehPA Feb 1, 2024
aac33c4
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 13, 2024
f7525f5
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 19, 2024
8a974b9
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 19, 2024
e5276bb
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 20, 2024
0f989fe
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 27, 2024
ec35a90
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Feb 28, 2024
6ec6fe1
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 2, 2024
988985b
feat(module:table): radio selection type
ParsaArvanehPA Mar 20, 2024
5e4dd2a
feat(module:table): radio selection type
ParsaArvanehPA Mar 20, 2024
5d1e384
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 24, 2024
d81339e
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Mar 24, 2024
2284767
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Apr 6, 2024
0ac094d
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Apr 23, 2024
59d40a5
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA May 12, 2024
82afabb
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Jul 14, 2024
5cad626
feat(module:table): radio selection type
ParsaArvanehPA Jul 17, 2024
8166519
feat(module:table): radio selection type
ParsaArvanehPA Jul 17, 2024
3a46b38
feat(module:table): radio selection type
ParsaArvanehPA Jul 17, 2024
11b7fc8
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Jul 17, 2024
ec24a0b
Merge branch 'refs/heads/master' into feature/radio-selection-in-tables
ParsaArvanehPA Jul 17, 2024
8705669
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Aug 4, 2024
19dd9b5
Merge branch 'refs/heads/master' into feature/radio-selection-in-tables
ParsaArvanehPA Aug 4, 2024
105ad16
feat(module:table): radio selection type
ParsaArvanehPA Aug 4, 2024
0e963ac
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Aug 12, 2024
2bb0cd9
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Sep 3, 2024
674a373
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Sep 8, 2024
124d3ac
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Sep 16, 2024
413ac69
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Sep 18, 2024
5bee081
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Sep 22, 2024
2569a26
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Sep 23, 2024
1e6cc5e
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Sep 28, 2024
e0eb45a
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Sep 30, 2024
aa3571f
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Oct 1, 2024
eb50945
Merge branch 'refs/heads/master' into feature/radio-selection-in-tables
ParsaArvanehPA Oct 5, 2024
44d9311
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Oct 5, 2024
ca70f46
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Oct 8, 2024
5227c13
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Oct 15, 2024
590f22c
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Oct 22, 2024
1577205
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Nov 2, 2024
912db8e
Merge branch 'NG-ZORRO:master' into master
ParsaArvanehPA Nov 23, 2024
ccbadd6
Merge branch 'master' into feature/radio-selection-in-table
ParsaArvanehPA Nov 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions components/table/demo/selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 2
title:
en-US: Selection
zh-CN: 可选择
---

Comment on lines +1 to +7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order number 2 was unused.

## zh-CN

第一列是联动的选择框。可以通过 `nzRowSelectionType` 属性指定选择类型,默认为 `checkbox`。

## en-US

Rows can be selectable by making first column as a selectable column. You can use `nzRowSelectionType` to set selection type. Default is `checkbox`.
116 changes: 116 additions & 0 deletions components/table/demo/selection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Component, OnInit } from '@angular/core';

import { NzRowSelectionType } from 'ng-zorro-antd/table';

interface ItemData {
id: number;
name: string;
age: number;
address: string;
}

@Component({
selector: 'nz-demo-table-selection',
template: `
<nz-radio-group [(ngModel)]="rowSelectionType" (ngModelChange)="resetSelectedValues()">
<label nz-radio nzValue="checkbox">checkbox</label>
<label nz-radio nzValue="radio">radio</label>
</nz-radio-group>

<nz-table #rowSelectionTable [nzData]="listOfData" (nzCurrentPageDataChange)="onCurrentPageDataChange($event)">
<thead>
<tr>
<ng-container *ngIf="rowSelectionType === 'radio'">
<th nzShowRowSelection [nzRowSelectionType]="'radio'"> </th>
</ng-container>
<ng-container *ngIf="rowSelectionType === 'checkbox'">
<th [(nzChecked)]="checked" [nzIndeterminate]="indeterminate" (nzCheckedChange)="onAllChecked($event)"></th>
</ng-container>
<th>Name</th>
<th>Age</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of rowSelectionTable.data">
<ng-container *ngIf="rowSelectionType === 'radio'">
<td
[nzChecked]="setOfCheckedId.has(data.id)"
[nzRowSelectionType]="'radio'"
(nzCheckedChange)="onItemChecked(data.id, $event)"
></td>
</ng-container>

<ng-container *ngIf="rowSelectionType === 'checkbox'">
<td [nzChecked]="setOfCheckedId.has(data.id)" (nzCheckedChange)="onItemChecked(data.id, $event)"></td>
</ng-container>
<td>{{ data.name }}</td>
<td>{{ data.age }}</td>
<td>{{ data.address }}</td>
</tr>
</tbody>
</nz-table>
`,
styles: `
nz-radio-group {
margin-block-end: 1rem;
}
`
})
export class NzDemoTableSelectionComponent implements OnInit {
rowSelectionType: NzRowSelectionType = 'checkbox';

checked = false;
indeterminate = false;
listOfCurrentPageData: readonly ItemData[] = [];
listOfData: readonly ItemData[] = [];
setOfCheckedId = new Set<number>();

updateCheckedSet(id: number, checked: boolean): void {
if (checked) {
if (this.rowSelectionType === 'radio') {
this.setOfCheckedId.forEach(x => {
this.setOfCheckedId.delete(x);
});
}

this.setOfCheckedId.add(id);
} else {
this.setOfCheckedId.delete(id);
}
}

onItemChecked(id: number, checked: boolean): void {
this.updateCheckedSet(id, checked);
this.refreshCheckedStatus();
}

onAllChecked(value: boolean): void {
this.listOfCurrentPageData.forEach(item => this.updateCheckedSet(item.id, value));
this.refreshCheckedStatus();
}

onCurrentPageDataChange($event: readonly ItemData[]): void {
this.listOfCurrentPageData = $event;
this.refreshCheckedStatus();
}

refreshCheckedStatus(): void {
this.checked = this.listOfCurrentPageData.every(item => this.setOfCheckedId.has(item.id));
this.indeterminate = this.listOfCurrentPageData.some(item => this.setOfCheckedId.has(item.id)) && !this.checked;
}

resetSelectedValues(): void {
this.setOfCheckedId = new Set<number>();
this.indeterminate = false;
}

ngOnInit(): void {
this.listOfData = new Array(5).fill(0).map((_, index) => ({
id: index,
name: `Edward King ${index}`,
age: 32,
address: `London, Park Lane no. ${index}`
}));
}
}
107 changes: 64 additions & 43 deletions components/table/doc/index.en-US.md

Large diffs are not rendered by default.

251 changes: 135 additions & 116 deletions components/table/doc/index.zh-CN.md

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions components/table/src/addon/selection.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzRadioComponent, NzRadioGroupComponent } from 'ng-zorro-antd/radio';

import { NzRowSelectionType } from '../table.types';

@Component({
selector: 'nz-table-selection',
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
template: `
@if (showCheckbox) {
@if (showCheckbox && nzRowSelectionType === 'checkbox') {
<label
nz-checkbox
[class.ant-table-selection-select-all-custom]="showRowSelection"
Expand All @@ -28,7 +31,7 @@
(ngModelChange)="onCheckedChange($event)"
></label>
}
@if (showRowSelection) {
@if (showRowSelection && nzRowSelectionType === 'checkbox') {
<div class="ant-table-selection-extra">
<span nz-dropdown class="ant-table-selection-down" nzPlacement="bottomLeft" [nzDropdownMenu]="selectionMenu">
<span nz-icon nzType="down"></span>
Expand All @@ -46,10 +49,11 @@
}
`,
host: { class: 'ant-table-selection' },
imports: [FormsModule, NzCheckboxModule, NzDropDownModule, NzIconModule],
imports: [FormsModule, NzCheckboxModule, NzDropDownModule, NzIconModule, NzRadioComponent, NzRadioGroupComponent],
standalone: true
})
export class NzTableSelectionComponent {
@Input() nzRowSelectionType: NzRowSelectionType = 'checkbox';

Check warning on line 56 in components/table/src/addon/selection.component.ts

View check run for this annotation

Codecov / codecov/patch

components/table/src/addon/selection.component.ts#L56

Added line #L56 was not covered by tests
@Input() listOfSelections: Array<{ text: string; onSelect(...args: NzSafeAny[]): NzSafeAny }> = [];
@Input() checked = false;
@Input() disabled = false;
Expand Down
30 changes: 27 additions & 3 deletions components/table/src/cell/td-addon.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import { FormsModule } from '@angular/forms';

import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
import { NzRadioComponent, NzRadioGroupComponent } from 'ng-zorro-antd/radio';

import { NzRowExpandButtonDirective } from '../addon/row-expand-button.directive';
import { NzRowIndentDirective } from '../addon/row-indent.directive';
import { NzRowSelectionType } from '../table.types';

@Component({
selector:
Expand Down Expand Up @@ -54,24 +56,42 @@
[attr.aria-label]="nzLabel"
(ngModelChange)="onCheckedChange($event)"
></label>
} @else if (nzShowRadio) {
<label
nz-radio
[nzDisabled]="nzDisabled"
[ngModel]="nzChecked"
[attr.aria-label]="nzLabel"
(ngModelChange)="onCheckedChange($event)"
></label>
}
<ng-content></ng-content>
`,
host: {
'[class.ant-table-cell-with-append]': `nzShowExpand || nzIndentSize > 0`,
'[class.ant-table-selection-column]': `nzShowCheckbox`
'[class.ant-table-selection-column]': `nzShowCheckbox || nzShowRadio`
},
imports: [NzRowIndentDirective, NzRowExpandButtonDirective, NgTemplateOutlet, NzCheckboxModule, FormsModule],
imports: [
NzRowIndentDirective,
NzRowExpandButtonDirective,
NgTemplateOutlet,
NzCheckboxModule,
FormsModule,
NzRadioComponent,
NzRadioGroupComponent
],
standalone: true
})
export class NzTdAddOnComponent implements OnChanges {
@Input() nzChecked = false;
@Input() nzRowSelectionType: NzRowSelectionType = 'checkbox';
@Input() nzDisabled = false;
@Input() nzIndeterminate = false;
@Input() nzLabel: string | null = null;
@Input() nzIndentSize = 0;
@Input({ transform: booleanAttribute }) nzShowExpand = false;
@Input({ transform: booleanAttribute }) nzShowCheckbox = false;
@Input({ transform: booleanAttribute }) nzShowRadio = false;
@Input({ transform: booleanAttribute }) nzExpand = false;
@Input() nzExpandIcon: TemplateRef<void> | null = null;
@Output() readonly nzCheckedChange = new EventEmitter<boolean>();
Expand Down Expand Up @@ -102,7 +122,11 @@
this.nzShowExpand = true;
}
if (isFirstChange(nzChecked) && !this.isNzShowCheckboxChanged) {
this.nzShowCheckbox = true;
if (this.nzRowSelectionType === 'checkbox') {
this.nzShowCheckbox = true;
} else {
this.nzShowRadio = true;

Check warning on line 128 in components/table/src/cell/td-addon.component.ts

View check run for this annotation

Codecov / codecov/patch

components/table/src/cell/td-addon.component.ts#L128

Added line #L128 was not covered by tests
}
}
}
}
3 changes: 3 additions & 0 deletions components/table/src/cell/th-selection.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { NzSafeAny } from 'ng-zorro-antd/core/types';

import { NzTableSelectionComponent } from '../addon/selection.component';
import { NzRowSelectionType } from '../table.types';

@Component({
selector: 'th[nzSelections],th[nzChecked],th[nzShowCheckbox],th[nzShowRowSelection]',
Expand All @@ -31,6 +32,7 @@
[checked]="nzChecked"
[disabled]="nzDisabled"
[indeterminate]="nzIndeterminate"
[nzRowSelectionType]="nzRowSelectionType"
[label]="nzLabel"
[listOfSelections]="nzSelections"
[showCheckbox]="nzShowCheckbox"
Expand All @@ -51,6 +53,7 @@
@Input() nzLabel: string | null = null;
@Input({ transform: booleanAttribute }) nzShowCheckbox = false;
@Input({ transform: booleanAttribute }) nzShowRowSelection = false;
@Input() nzRowSelectionType: NzRowSelectionType = 'checkbox';

Check warning on line 56 in components/table/src/cell/th-selection.component.ts

View check run for this annotation

Codecov / codecov/patch

components/table/src/cell/th-selection.component.ts#L56

Added line #L56 was not covered by tests
@Output() readonly nzCheckedChange = new EventEmitter<boolean>();

private isNzShowExpandChanged = false;
Expand Down
1 change: 1 addition & 0 deletions components/table/src/table.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type NzTableLayout = 'fixed' | 'auto';
export type NzTablePaginationPosition = 'top' | 'bottom' | 'both';
export type NzTablePaginationType = 'default' | 'small';
export type NzTableSize = 'middle' | 'default' | 'small';
export type NzRowSelectionType = 'checkbox' | 'radio';
export type NzTableFilterList = Array<{ text: string; value: NzSafeAny; byDefault?: boolean }>;
export type NzTableSortOrder = string | 'ascend' | 'descend' | null;
export type NzTableSortFn<T = unknown> = (a: T, b: T, sortOrder?: NzTableSortOrder) => number;
Expand Down
13 changes: 13 additions & 0 deletions components/table/src/testing/td.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { By } from '@angular/platform-browser';

import { NzTdAddOnComponent } from '../cell/td-addon.component';
import { NzTableModule } from '../table.module';
import { NzRowSelectionType } from '../table.types';

describe('nz-td', () => {
beforeEach(fakeAsync(() => {
Expand All @@ -24,11 +25,21 @@ describe('nz-td', () => {
testComponent = fixture.debugElement.componentInstance;
td = fixture.debugElement.query(By.directive(NzTdAddOnComponent));
});
it('should default value for selection type be checkbox', () => {
fixture.detectChanges();
expect(td.nativeElement.querySelector('.ant-checkbox-wrapper')).toBeDefined();
expect(td.nativeElement.classList).toContain('ant-table-selection-column');
});
it('should checkbox work', () => {
fixture.detectChanges();
expect(td.nativeElement.querySelector('.ant-checkbox-wrapper')).toBeDefined();
expect(td.nativeElement.classList).toContain('ant-table-selection-column');
});
it('should radio work', fakeAsync(() => {
testComponent.selectionType = 'radio';
flush();
expect(td.nativeElement.querySelector('.ant-radio-wrapper')).toBeDefined();
}));
it('should checked work', fakeAsync(() => {
fixture.detectChanges();
expect(td.nativeElement.querySelector('.ant-checkbox-wrapper').firstElementChild!.classList).not.toContain(
Expand Down Expand Up @@ -155,6 +166,7 @@ describe('nz-td', () => {
(nzCheckedChange)="checkedChange($event)"
[nzDisabled]="disabled"
[(nzExpand)]="expand"
[nzRowSelectionType]="selectionType"
(nzExpandChange)="expandChange($event)"
[nzIndentSize]="indentSize"
[nzLeft]="left"
Expand All @@ -163,6 +175,7 @@ describe('nz-td', () => {
`
})
export class NzTestTdComponent {
selectionType: NzRowSelectionType = 'checkbox';
checked = false;
checkedChange = jasmine.createSpy('show change');
indeterminate = false;
Expand Down