-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: table column sticky * refactor: workaround for custom stickyCssClass Co-authored-by: JounQin <admin@1stg.me>
- Loading branch information
Showing
9 changed files
with
263 additions
and
2 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
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,59 @@ | ||
import { | ||
AfterViewChecked, | ||
AfterViewInit, | ||
Directive, | ||
ElementRef, | ||
OnDestroy, | ||
} from '@angular/core'; | ||
import { fromEvent, Subject } from 'rxjs'; | ||
import { debounceTime, takeUntil } from 'rxjs/operators'; | ||
|
||
const CLASS_PREFIX = 'aui-table'; | ||
const WRAPPER_CLASS = `${CLASS_PREFIX}__scroll-wrapper`; | ||
const HAS_SCROLL_CLASS = `${WRAPPER_CLASS}--has-scroll`; | ||
const SCROLLING_CLASS = `${WRAPPER_CLASS}--scrolling`; | ||
const SCROLL_BEFORE_END_CLASS = `${WRAPPER_CLASS}--before-end`; | ||
|
||
@Directive({ | ||
selector: '[auiTableScrollWrapper]', | ||
host: { | ||
class: `${WRAPPER_CLASS} ${SCROLL_BEFORE_END_CLASS}`, | ||
}, | ||
}) | ||
export class TableScrollWrapperDirective | ||
implements AfterViewChecked, AfterViewInit, OnDestroy { | ||
scrollDis = 0; | ||
destroy$$ = new Subject<void>(); | ||
constructor(private readonly el: ElementRef<HTMLElement>) {} | ||
|
||
ngAfterViewChecked() { | ||
this.scrollDis = | ||
this.el.nativeElement.scrollWidth - this.el.nativeElement.offsetWidth; | ||
if (this.scrollDis > 0) { | ||
this.el.nativeElement.classList.add(HAS_SCROLL_CLASS); | ||
} | ||
} | ||
|
||
ngAfterViewInit() { | ||
fromEvent(this.el.nativeElement, 'scroll') | ||
.pipe(debounceTime(100), takeUntil(this.destroy$$)) | ||
.subscribe(() => { | ||
const scrollLeft = this.el.nativeElement.scrollLeft; | ||
if (scrollLeft > 0) { | ||
this.el.nativeElement.classList.add(SCROLLING_CLASS); | ||
} else { | ||
this.el.nativeElement.classList.remove(SCROLLING_CLASS); | ||
} | ||
if (scrollLeft === this.scrollDis) { | ||
this.el.nativeElement.classList.remove(SCROLL_BEFORE_END_CLASS); | ||
} else { | ||
this.el.nativeElement.classList.add(SCROLL_BEFORE_END_CLASS); | ||
} | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroy$$.next(); | ||
this.destroy$$.complete(); | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
stories/table/sticky-columns/sticky-columns-demo.component.html
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,94 @@ | ||
<div auiTableScrollWrapper> | ||
<aui-table [dataSource]="dataSource" #scrollTable> | ||
<ng-container auiTableColumnDef="no" [sticky]="true"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
No. | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item">{{ item[0] }}</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell1"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[1] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell2"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[2] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell3"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[3] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell4"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[4] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell5"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[5] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell6"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[6] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell7"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[7] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell8"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[8] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell9"> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[9] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<ng-container auiTableColumnDef="cell10" stickyEnd> | ||
<aui-table-header-cell *auiTableHeaderCellDef> | ||
header cell | ||
</aui-table-header-cell> | ||
<aui-table-cell *auiTableCellDef="let item"> | ||
{{ item[10] }} | ||
</aui-table-cell> | ||
</ng-container> | ||
<aui-table-header-row | ||
*auiTableHeaderRowDef="columns" | ||
></aui-table-header-row> | ||
<aui-table-row *auiTableRowDef="let row; columns: columns"></aui-table-row> | ||
</aui-table> | ||
</div> |
6 changes: 6 additions & 0 deletions
6
stories/table/sticky-columns/sticky-columns-demo.component.scss
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,6 @@ | ||
.aui-table__scroll-wrapper { | ||
.aui-table__header-cell, | ||
.aui-table__cell { | ||
flex: 1 0 100px; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
stories/table/sticky-columns/sticky-columns-demo.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,16 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
|
||
@Component({ | ||
templateUrl: 'sticky-columns-demo.component.html', | ||
styleUrls: ['sticky-columns-demo.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class StickyColumnsDemoComponent { | ||
columns = ['no'].concat( | ||
Array.from({ length: 10 }).map((_, index) => `cell${index + 1}`), | ||
); | ||
|
||
dataSource = Array.from({ length: 20 }).map((_, i) => | ||
['1'].concat(Array.from({ length: 10 }).map((_, j) => `cell${i}${j}`)), | ||
); | ||
} |