Skip to content

Commit

Permalink
feat(module:table): support fixed header
Browse files Browse the repository at this point in the history
close #86 close #174
  • Loading branch information
执衡 committed Sep 1, 2017
1 parent 414241c commit 6567705
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 30 deletions.
61 changes: 46 additions & 15 deletions src/components/table/nz-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
ElementRef,
AfterViewInit,
EventEmitter,
Output
Output,
ContentChild,
ChangeDetectorRef,
TemplateRef
} from '@angular/core';

@Component({
Expand All @@ -19,6 +22,8 @@ import {
<div>
<div
class="ant-table"
[class.ant-table-fixed-header]="nzScroll"
[class.ant-table-scroll-position-left]="nzScroll"
[class.ant-table-bordered]="nzBordered"
[class.ant-table-large]="(nzSize!=='middle')&&(nzSize!=='small')"
[class.ant-table-middle]="nzSize=='middle'"
Expand All @@ -27,19 +32,32 @@ import {
<ng-content select="[nz-table-title]"></ng-content>
</div>
<div class="ant-table-content">
<div class="ant-table-body">
<table>
<ng-content></ng-content>
</table>
</div>
<div class="ant-table-placeholder" *ngIf="data.length==0 && !nzCustomNoResult">
<span>没有数据</span>
</div>
<div class="ant-table-placeholder" *ngIf="data.length==0 && nzCustomNoResult">
<ng-content select="[noResult]"></ng-content>
</div>
<div class="ant-table-footer" *ngIf="nzShowFooter">
<ng-content select="[nz-table-footer]"></ng-content>
<div [class.ant-table-scroll]="nzScroll">
<div class="ant-table-header" style="margin-bottom: -15px; padding-bottom: 0px;" *ngIf="nzScroll">
<table>
<colgroup>
<col *ngFor="let th of ths" [style.width.px]="th.nzWidth" [style.minWidth.px]="th.nzWidth">
</colgroup>
<ng-template [ngTemplateOutlet]="fixedHeader"></ng-template>
</table>
</div>
<div class="ant-table-body" [style.maxHeight.px]="nzScroll?.y" [style.overflowY]="nzScroll?.y?'scroll':''">
<table>
<colgroup *ngIf="nzScroll">
<col [style.width.px]="th.nzWidth" [style.minWidth.px]="th.nzWidth" *ngFor="let th of ths">
</colgroup>
<ng-content></ng-content>
</table>
</div>
<div class="ant-table-placeholder" *ngIf="data.length==0 && !nzCustomNoResult">
<span>没有数据</span>
</div>
<div class="ant-table-placeholder" *ngIf="data.length==0 && nzCustomNoResult">
<ng-content select="[noResult]"></ng-content>
</div>
<div class="ant-table-footer" *ngIf="nzShowFooter">
<ng-content select="[nz-table-footer]"></ng-content>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -67,13 +85,15 @@ import {
export class NzTableComponent implements AfterViewInit {
/** public data for ngFor tr */
data = [];
_scroll;
_el: HTMLElement;
_current = 1;
_total: number;
_pageSize = 10;
_dataSet = [];
_isInit = false;
_isAjax = false;
ths = [];
@Output() nzPageSizeChange: EventEmitter<any> = new EventEmitter();
@Output() nzPageIndexChange: EventEmitter<any> = new EventEmitter();
@Output() nzDataChange: EventEmitter<any> = new EventEmitter();
Expand All @@ -88,6 +108,17 @@ export class NzTableComponent implements AfterViewInit {
@Input() nzShowTotal = false;
@Input() nzShowFooter = false;
@Input() nzShowTitle = false;
@ContentChild('nzFixedHeader') fixedHeader: TemplateRef<any>;

@Input()
set nzScroll(value) {
this._scroll = value;
this._cd.detectChanges();
}

get nzScroll() {
return this._scroll;
}

/** async data */
@Input()
Expand Down Expand Up @@ -172,7 +203,7 @@ export class NzTableComponent implements AfterViewInit {
}
}

constructor(private _elementRef: ElementRef) {
constructor(private _elementRef: ElementRef, private _cd: ChangeDetectorRef) {
this._el = this._elementRef.nativeElement;
}

Expand Down
19 changes: 16 additions & 3 deletions src/components/table/nz-th.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@ import {
Directive,
Input,
HostBinding,
ElementRef
ElementRef, OnInit, OnDestroy
} from '@angular/core';
import { NzTableComponent } from './nz-table.component';

@Directive({
selector: '[nz-th]'
})
export class NzThDirective {
export class NzThDirective implements OnInit, OnDestroy {
_el: HTMLElement;
@Input() nzWidth;
@Input() @HostBinding(`class.ant-table-selection-column`) nzCheckbox;

constructor(private _elementRef: ElementRef) {
constructor(private _elementRef: ElementRef, private nzTableComponent: NzTableComponent) {
this._el = this._elementRef.nativeElement;
}

ngOnInit() {
this.nzTableComponent.ths.push(this);
}

ngOnDestroy() {
const index = this.nzTableComponent.ths.indexOf(this);
if (index > -1) {
this.nzTableComponent.ths.splice(index, 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, OnInit } from '@angular/core';
@Component({
selector: 'nz-demo-input-basic',
template: `
<nz-input [(ngModel)]="inputValue" [nzPlaceHolder]="'Basic usage'" (ngModelChange)="_console($event)"></nz-input>
<nz-input [(ngModel)]="inputValue" [nzDisabled]="true" [nzPlaceHolder]="'Basic usage'" [nzSize]="'small'" (ngModelChange)="_console($event)"></nz-input>
<p>{{inputValue}}</p>`,

styles: []
Expand Down
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-colspan-rowspan',
template: `
<nz-table #nzTable [nzDataSource]="data" [nzPageSize]="10" [nzBordered]="true">
<thead nz-thead>
<tr>
<th nz-th><span>Name</span></th>
<th nz-th><span>Age</span></th>
<th nz-th colspan="2"><span>Home phone</span></th>
<th nz-th><span>Address</span></th>
</tr>
</thead>
<tbody nz-tbody>
<tr nz-tbody-tr *ngFor="let data of nzTable.data; index as i;">
<td nz-td>
<a>{{data.name}}</a>
</td>
<td nz-td [attr.colspan]="i==4?5:1">{{data.age}}</td>
<td nz-td [attr.rowspan]="i==2?2:1" *ngIf="(i!=3)&&(i!=4)">{{data.tel}}</td>
<td nz-td *ngIf="i!=4">{{data.phone}}</td>
<td nz-td *ngIf="i!=4">{{data.address}}</td>
</tr>
</tbody>
</nz-table>`,
styles : []
})
export class NzDemoTableColspanRowspanComponent implements OnInit {
data = [ {
key : '1',
name : 'John Brown',
age : 32,
tel : '0571-22098909',
phone : 18889898989,
address: 'New York No. 1 Lake Park',
}, {
key : '2',
name : 'Jim Green',
tel : '0571-22098333',
phone : 18889898888,
age : 42,
address: 'London No. 1 Lake Park',
}, {
key : '3',
name : 'Joe Black',
age : 32,
tel : '0575-22098909',
phone : 18900010002,
address: 'Sidney No. 1 Lake Park',
}, {
key : '4',
name : 'Jim Red',
age : 18,
tel : '0575-22098909',
phone : 18900010002,
address: 'London No. 2 Lake Park',
}, {
key : '5',
name : 'Jake White',
age : 18,
tel : '0575-22098909',
phone : 18900010002,
address: 'Dublin No. 2 Lake Park',
} ];

constructor() {
}

ngOnInit() {
}
}

42 changes: 42 additions & 0 deletions src/showcase/nz-demo-table/nz-demo-table-fixed-header.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'nz-demo-table-fixed-header',
template: `
<nz-table #nzTable [nzDataSource]="data" [nzPageSize]="50" [nzScroll]="{ y: 240 }">
<ng-template #nzFixedHeader>
<thead nz-thead>
<tr>
<th nz-th [nzWidth]="150"><span>Name</span></th>
<th nz-th [nzWidth]="150"><span>Age</span></th>
<th nz-th><span>Address</span></th>
</tr>
</thead>
</ng-template>
<tbody nz-tbody>
<tr nz-tbody-tr *ngFor="let data of nzTable.data">
<td nz-td>{{data.name}}</td>
<td nz-td>{{data.age}}</td>
<td nz-td>{{data.address}}</td>
</tr>
</tbody>
</nz-table>`,
styles : []
})
export class NzDemoTableFixedHeaderComponent implements OnInit {
data = [];

constructor() {
}

ngOnInit() {
for (let i = 0; i < 100; i++) {
this.data.push({
name : `Edward King ${i}`,
age : 32,
address: `London, Park Lane no. ${i}`,
});
}
}
}

31 changes: 22 additions & 9 deletions src/showcase/nz-demo-table/nz-demo-table-size.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ import { Component, OnInit } from '@angular/core';
<nz-switch [(ngModel)]="_footer" [ngModelOptions]="{standalone: true}"></nz-switch>
</div>
</div>
<div nz-row nz-form-item>
<div nz-form-label>
<label>Fixed Header</label>
</div>
<div nz-form-control>
<nz-switch [(ngModel)]="_fixHeader" [ngModelOptions]="{standalone: true}"></nz-switch>
</div>
</div>
<div nz-row nz-form-item>
<div nz-form-label>
<label>Size</label>
Expand All @@ -75,6 +83,7 @@ import { Component, OnInit } from '@angular/core';
</div>
<nz-table
#nzTable
[nzScroll]="_fixHeader?{ y: 240 }:null"
[nzDataSource]="_dataSet"
[nzPageSize]="10"
[nzBordered]="_bordered"
Expand All @@ -84,14 +93,16 @@ import { Component, OnInit } from '@angular/core';
[nzShowTitle]="_title"
[nzSize]="_size">
<span nz-table-title>Here is Title</span>
<thead nz-thead *ngIf="_header">
<tr>
<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>
<ng-template #nzFixedHeader [ngIf]="_header&&!_fixHeader">
<thead nz-thead>
<tr>
<th nz-th [nzWidth]="150"><span>Name</span></th>
<th nz-th [nzWidth]="70"><span>Age</span></th>
<th nz-th [nzWidth]="360"><span>Address</span></th>
<th nz-th><span>Action</span></th>
</tr>
</thead>
</ng-template>
<tbody nz-tbody>
<tr nz-tbody-tr *ngFor="let data of nzTable.data">
<td nz-td>{{data.name}}</td>
Expand Down Expand Up @@ -130,6 +141,7 @@ import { Component, OnInit } from '@angular/core';
.components-table-demo-control-bar {
margin-bottom: 10px;
}
.components-table-demo-control-bar ::ng-deep .ant-form-item {
margin-right: 16px;
margin-bottom: 8px;
Expand All @@ -139,12 +151,13 @@ import { Component, OnInit } from '@angular/core';
})
export class NzDemoTableSizeComponent implements OnInit {
_dataSet = [];
_bordered = false;
_bordered = true;
_loading = false;
_pagination = true;
_header = true;
_title = true;
_footer = true;
_fixHeader = false;
_size = 'small';

constructor() {
Expand Down
2 changes: 2 additions & 0 deletions src/showcase/nz-demo-table/nz-demo-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Component, OnInit, ViewEncapsulation } from '@angular/core';
})
export class NzDemoTableComponent implements OnInit {
NzDemoTableBasicCode = require('!!raw-loader!./nz-demo-table-basic.component');
NzDemoTableFixedHeaderCode = require('!!raw-loader!./nz-demo-table-fixed-header.component');
NzDemoTableColspanRowspanCode = require('!!raw-loader!./nz-demo-table-colspan-rowspan.component');
NzDemoTableSortCode = require('!!raw-loader!./nz-demo-table-sort.component');
NzDemoTableSelectionCode = require('!!raw-loader!./nz-demo-table-selection.component');
NzDemoTableSelectionAndOperationCode = require('!!raw-loader!./nz-demo-table-selection-and-operation.component');
Expand Down
Loading

0 comments on commit 6567705

Please sign in to comment.