-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CL-343] Create a new table component for virtual scrolling (#10113)
This creates a new component called bit-table-scroll as it's a breaking change in how tables works. We could probably conditionally support both behaviors in the existing table component if we desire. Rather than iterating the rows in the consuming component, we now need to define a row definition, bitRowDef which provides access to the rows data through angular let- syntax. This allows the table component to own the behaviour which is needed in order to use the cdkVirtualFor directive which must be inside the cdk-virtual-scroll-viewport component.
- Loading branch information
Showing
8 changed files
with
173 additions
and
62 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,20 @@ | ||
<cdk-virtual-scroll-viewport | ||
scrollWindow | ||
[itemSize]="rowSize" | ||
[ngStyle]="{ paddingBottom: headerHeight + 'px' }" | ||
> | ||
<table [ngClass]="tableClass"> | ||
<thead | ||
class="tw-border-0 tw-border-b-2 tw-border-solid tw-border-secondary-300 tw-font-bold tw-text-muted" | ||
> | ||
<tr> | ||
<ng-content select="[header]"></ng-content> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr *cdkVirtualFor="let r of rows$; trackBy: trackBy" bitRow> | ||
<ng-container *ngTemplateOutlet="rowDef.template; context: { $implicit: r }"></ng-container> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</cdk-virtual-scroll-viewport> |
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,92 @@ | ||
import { | ||
AfterContentChecked, | ||
Component, | ||
ContentChild, | ||
Input, | ||
OnDestroy, | ||
TemplateRef, | ||
Directive, | ||
NgZone, | ||
AfterViewInit, | ||
ElementRef, | ||
TrackByFunction, | ||
} from "@angular/core"; | ||
|
||
import { TableComponent } from "./table.component"; | ||
|
||
/** | ||
* Helper directive for defining the row template. | ||
* | ||
* ```html | ||
* <ng-template bitRowDef let-row> | ||
* <td bitCell>{{ row.id }}</td> | ||
* </ng-template> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
selector: "[bitRowDef]", | ||
standalone: true, | ||
}) | ||
export class BitRowDef { | ||
constructor(public template: TemplateRef<any>) {} | ||
} | ||
|
||
/** | ||
* Scrollable table component. | ||
* | ||
* Utilizes virtual scrolling to render large datasets. | ||
*/ | ||
@Component({ | ||
selector: "bit-table-scroll", | ||
templateUrl: "./table-scroll.component.html", | ||
providers: [{ provide: TableComponent, useExisting: TableScrollComponent }], | ||
}) | ||
export class TableScrollComponent | ||
extends TableComponent | ||
implements AfterContentChecked, AfterViewInit, OnDestroy | ||
{ | ||
/** The size of the rows in the list (in pixels). */ | ||
@Input({ required: true }) rowSize: number; | ||
|
||
/** Optional trackBy function. */ | ||
@Input() trackBy: TrackByFunction<any> | undefined; | ||
|
||
@ContentChild(BitRowDef) protected rowDef: BitRowDef; | ||
|
||
/** | ||
* Height of the thead element (in pixels). | ||
* | ||
* Used to increase the table's total height to avoid items being cut off. | ||
*/ | ||
protected headerHeight = 0; | ||
|
||
/** | ||
* Observer for table header, applies padding on resize. | ||
*/ | ||
private headerObserver: ResizeObserver; | ||
|
||
constructor( | ||
private zone: NgZone, | ||
private el: ElementRef, | ||
) { | ||
super(); | ||
} | ||
|
||
ngAfterViewInit(): void { | ||
this.headerObserver = new ResizeObserver((entries) => { | ||
this.zone.run(() => { | ||
this.headerHeight = entries[0].contentRect.height; | ||
}); | ||
}); | ||
|
||
this.headerObserver.observe(this.el.nativeElement.querySelector("thead")); | ||
} | ||
|
||
override ngOnDestroy(): void { | ||
super.ngOnDestroy(); | ||
|
||
if (this.headerObserver) { | ||
this.headerObserver.disconnect(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
import { ScrollingModule } from "@angular/cdk/scrolling"; | ||
import { CommonModule } from "@angular/common"; | ||
import { NgModule } from "@angular/core"; | ||
|
||
import { CellDirective } from "./cell.directive"; | ||
import { RowDirective } from "./row.directive"; | ||
import { SortableComponent } from "./sortable.component"; | ||
import { BitRowDef, TableScrollComponent } from "./table-scroll.component"; | ||
import { TableBodyDirective, TableComponent } from "./table.component"; | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
imports: [CommonModule, ScrollingModule, BitRowDef], | ||
declarations: [ | ||
CellDirective, | ||
RowDirective, | ||
SortableComponent, | ||
TableBodyDirective, | ||
TableComponent, | ||
TableScrollComponent, | ||
], | ||
exports: [ | ||
BitRowDef, | ||
CellDirective, | ||
RowDirective, | ||
SortableComponent, | ||
TableBodyDirective, | ||
TableComponent, | ||
TableScrollComponent, | ||
], | ||
exports: [TableComponent, CellDirective, RowDirective, SortableComponent, TableBodyDirective], | ||
}) | ||
export class TableModule {} |
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