-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:table): support table expand feature (#259)
close #185
- Loading branch information
Showing
13 changed files
with
399 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
Component, | ||
Input, | ||
Output, | ||
HostListener, | ||
HostBinding, | ||
EventEmitter, | ||
} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'nz-row-expand-icon', | ||
template: `` | ||
}) | ||
export class NzRowExpandIconComponent { | ||
@Input() nzExpand = false; | ||
@Input() @HostBinding('class.ant-table-row-spaced') nzShowExpand = false; | ||
@Output() nzExpandChange = new EventEmitter(); | ||
|
||
@HostBinding(`class.ant-table-row-expanded`) | ||
get expanded() { | ||
return this.nzExpand && !this.nzShowExpand; | ||
} | ||
|
||
@HostBinding(`class.ant-table-row-collapsed`) | ||
get collapsed() { | ||
return !this.nzExpand && !this.nzShowExpand; | ||
} | ||
|
||
@HostBinding(`class.ant-table-row-expand-icon`) _expandIcon = true; | ||
|
||
|
||
@HostListener('click') | ||
onClick() { | ||
this.nzExpand = !this.nzExpand; | ||
this.nzExpandChange.emit(this.nzExpand); | ||
} | ||
|
||
constructor() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { | ||
Component, | ||
Input, | ||
HostBinding, | ||
} from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'nz-row-indent', | ||
template: `` | ||
}) | ||
export class NzRowIndentComponent { | ||
@Input() nzIndentSize; | ||
|
||
@HostBinding(`style.paddingLeft.px`) | ||
get paddingLeft() { | ||
return this.nzIndentSize * 20; | ||
} | ||
|
||
@HostBinding(`class.ant-table-row-indent`) _rowIndent = true; | ||
|
||
constructor() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/showcase/nz-demo-table/nz-demo-table-expand-tree.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'nz-demo-table-expand-tree', | ||
template: ` | ||
<nz-table #nzTable [nzDataSource]="data" [nzPageSize]="10"> | ||
<thead nz-thead> | ||
<tr> | ||
<th nz-th [nzWidth]="'40%'"><span>Name</span></th> | ||
<th nz-th [nzWidth]="'30%'"><span>Age</span></th> | ||
<th nz-th><span>Address</span></th> | ||
</tr> | ||
</thead> | ||
<tbody nz-tbody> | ||
<ng-template ngFor let-data [ngForOf]="nzTable.data"> | ||
<ng-template ngFor let-item [ngForOf]="expandDataCache[data.key]"> | ||
<tr nz-tbody-tr *ngIf="(item.parent&&item.parent.expand)||!(item.parent)"> | ||
<td nz-td> | ||
<nz-row-indent [nzIndentSize]="item.level"></nz-row-indent> | ||
<nz-row-expand-icon [(nzExpand)]="item.expand" (nzExpandChange)="collapse(expandDataCache[data.key],item,$event)" [nzShowExpand]="!item.children"></nz-row-expand-icon> | ||
{{item.name}} | ||
</td> | ||
<td nz-td>{{item.age}}</td> | ||
<td nz-td>{{item.address}}</td> | ||
</tr> | ||
</ng-template> | ||
</ng-template> | ||
</tbody> | ||
</nz-table>`, | ||
styles : [] | ||
}) | ||
export class NzDemoTableExpandTreeComponent implements OnInit { | ||
data = [ | ||
{ | ||
key : 1, | ||
name : 'John Brown sr.', | ||
age : 60, | ||
address : 'New York No. 1 Lake Park', | ||
children: [ { | ||
key : 11, | ||
name : 'John Brown', | ||
age : 42, | ||
address: 'New York No. 2 Lake Park', | ||
}, { | ||
key : 12, | ||
name : 'John Brown jr.', | ||
age : 30, | ||
address : 'New York No. 3 Lake Park', | ||
children: [ { | ||
key : 121, | ||
name : 'Jimmy Brown', | ||
age : 16, | ||
address: 'New York No. 3 Lake Park', | ||
} ], | ||
}, { | ||
key : 13, | ||
name : 'Jim Green sr.', | ||
age : 72, | ||
address : 'London No. 1 Lake Park', | ||
children: [ { | ||
key : 131, | ||
name : 'Jim Green', | ||
age : 42, | ||
address : 'London No. 2 Lake Park', | ||
children: [ { | ||
key : 1311, | ||
name : 'Jim Green jr.', | ||
age : 25, | ||
address: 'London No. 3 Lake Park', | ||
}, { | ||
key : 1312, | ||
name : 'Jimmy Green sr.', | ||
age : 18, | ||
address: 'London No. 4 Lake Park', | ||
} ], | ||
} ], | ||
} ], | ||
}, | ||
{ | ||
key : 2, | ||
name : 'Joe Black', | ||
age : 32, | ||
address: 'Sidney No. 1 Lake Park', | ||
} | ||
]; | ||
expandDataCache = {}; | ||
|
||
collapse(array, data, $event) { | ||
if ($event === false) { | ||
if (data.children) { | ||
data.children.forEach(d => { | ||
const target = array.find(a => a.key === d.key); | ||
target.expand = false; | ||
this.collapse(array, target, false); | ||
}); | ||
} else { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
convertTreeToList(root) { | ||
const stack = [], array = [], hashMap = {}; | ||
stack.push({ ...root, level: 0, expand: false }); | ||
|
||
while (stack.length !== 0) { | ||
const node = stack.pop(); | ||
this.visitNode(node, hashMap, array); | ||
if (node.children) { | ||
for (let i = node.children.length - 1; i >= 0; i--) { | ||
stack.push({ ...node.children[ i ], level: node.level + 1, expand: false, parent: node }); | ||
} | ||
} | ||
} | ||
|
||
return array; | ||
} | ||
|
||
visitNode(node, hashMap, array) { | ||
if (!hashMap[ node.key ]) { | ||
hashMap[ node.key ] = true; | ||
array.push(node); | ||
} | ||
} | ||
|
||
constructor() { | ||
} | ||
|
||
ngOnInit() { | ||
this.data.forEach(item => { | ||
this.expandDataCache[ item.key ] = this.convertTreeToList(item); | ||
}); | ||
} | ||
} | ||
|
73 changes: 73 additions & 0 deletions
73
src/showcase/nz-demo-table/nz-demo-table-expand.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'nz-demo-table-expand', | ||
template: ` | ||
<nz-table #nzTable [nzDataSource]="data" [nzPageSize]="10"> | ||
<thead nz-thead> | ||
<tr> | ||
<th nz-th [nzExpand]="true"></th> | ||
<th nz-th><span>Name</span></th> | ||
<th nz-th><span>Age</span></th> | ||
<th nz-th><span>Address</span></th> | ||
<th nz-th><span>Action</span></th> | ||
</tr> | ||
</thead> | ||
<tbody nz-tbody> | ||
<ng-template ngFor let-data [ngForOf]="nzTable.data"> | ||
<tr nz-tbody-tr> | ||
<td nz-td [nzExpand]="true"> | ||
<nz-row-expand-icon [(nzExpand)]="data.expand"></nz-row-expand-icon> | ||
</td> | ||
<td nz-td>{{data.name}}</td> | ||
<td nz-td>{{data.age}}</td> | ||
<td nz-td>{{data.address}}</td> | ||
<td nz-td> | ||
<span> | ||
<a href="#">Delete</a> | ||
</span> | ||
</td> | ||
</tr> | ||
<tr nz-tbody-tr *ngIf="data.expand"> | ||
<td nz-td></td> | ||
<td nz-td colspan="4"> | ||
{{data.description}} | ||
</td> | ||
</tr> | ||
</ng-template> | ||
</tbody> | ||
</nz-table>`, | ||
styles : [] | ||
}) | ||
export class NzDemoTableExpandComponent implements OnInit { | ||
data = [ | ||
{ | ||
name : 'John Brown', | ||
age : 32, | ||
expand : false, | ||
address : 'New York No. 1 Lake Park', | ||
description: 'My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.' | ||
}, | ||
{ | ||
name : 'Jim Green', | ||
age : 42, | ||
expand : false, | ||
address : 'London No. 1 Lake Park', | ||
description: 'My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park.' | ||
}, | ||
{ | ||
name : 'Joe Black', | ||
age : 32, | ||
expand : false, | ||
address : 'Sidney No. 1 Lake Park', | ||
description: 'My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park.' | ||
}, | ||
]; | ||
|
||
constructor() { | ||
} | ||
|
||
ngOnInit() { | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.