豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 44e81e1

Browse files
committed
feat(apps): create the base of the pull request table
Create a table and base column definition for the pull request table to allow for the table columns to be established incramentally.
1 parent 7842990 commit 44e81e1

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Directive, ViewChild} from '@angular/core';
2+
import {MatCellDef, MatHeaderCellDef} from '@angular/material/table';
3+
4+
// A Directive is used to satisfy the Angular compiler which wants a decorated class
5+
// even though its abstract.
6+
@Directive()
7+
export abstract class BaseColumn {
8+
/** The cell definintion for a row. */
9+
@ViewChild(MatCellDef, {static: true}) cell!: MatCellDef;
10+
/** The header cell definintion for a row. */
11+
@ViewChild(MatHeaderCellDef, {static: true}) headerCell!: MatHeaderCellDef;
12+
/** The name of the column. */
13+
abstract name: string;
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<mat-table [dataSource]="dataSource">
2+
<mat-header-row *matHeaderRowDef="[]"></mat-header-row>
3+
<mat-row *matRowDef="let row; columns: [];"></mat-row>
4+
</mat-table>

apps/prs/src/app/pr-table/pr-table.component.scss

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {ComponentFixture, TestBed} from '@angular/core/testing';
2+
3+
import {PrTableComponent} from './pr-table.component';
4+
5+
describe('PrTableComponent', () => {
6+
let component: PrTableComponent;
7+
let fixture: ComponentFixture<PrTableComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [PrTableComponent],
12+
}).compileComponents();
13+
});
14+
15+
beforeEach(() => {
16+
fixture = TestBed.createComponent(PrTableComponent);
17+
component = fixture.componentInstance;
18+
fixture.detectChanges();
19+
});
20+
21+
it('should create', () => {
22+
expect(component).toBeTruthy();
23+
});
24+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {CdkColumnDef} from '@angular/cdk/table';
2+
import {Component, Injector, AfterViewInit, ViewChild, ViewContainerRef, Type} from '@angular/core';
3+
import {Firestore, collectionData, collectionGroup, query, limit} from '@angular/fire/firestore';
4+
import {
5+
MatColumnDef,
6+
MatHeaderRowDef,
7+
MatRowDef,
8+
MatTable,
9+
MatTableDataSource,
10+
} from '@angular/material/table';
11+
import {PullRequest} from '../../../../shared/models/app-models';
12+
import {BaseColumn} from './columns/base';
13+
14+
@Component({
15+
selector: 'pr-table',
16+
templateUrl: './pr-table.component.html',
17+
styleUrls: ['./pr-table.component.scss'],
18+
})
19+
export class PrTableComponent implements AfterViewInit {
20+
/** The columns used in the PR table. */
21+
columns: Type<BaseColumn>[] = [];
22+
/** Data source for the table providing the list of pull requests/ */
23+
dataSource: MatTableDataSource<PullRequest> = new MatTableDataSource();
24+
25+
/** The table. */
26+
@ViewChild(MatTable, {static: true}) table!: MatTable<PullRequest>;
27+
/** The row definintion. */
28+
@ViewChild(MatRowDef, {static: true}) tableRow!: MatRowDef<PullRequest>;
29+
/** The header row definition. */
30+
@ViewChild(MatHeaderRowDef, {static: true}) tableHeaderRow!: MatHeaderRowDef;
31+
32+
constructor(private injector: Injector, private vcr: ViewContainerRef) {}
33+
34+
ngAfterViewInit(): void {
35+
const columns = this.columns.map((column) => {
36+
/** The column definition for the generated column. */
37+
const columnDef = new MatColumnDef();
38+
/**
39+
* The injector to provide to the BaseColumn derived class, allowing it to be provided to the
40+
* cell definitions in the template.
41+
*/
42+
const injector = Injector.create({
43+
providers: [{provide: CdkColumnDef, useValue: columnDef}],
44+
parent: this.injector,
45+
});
46+
// The name, cell and headerCell are obtained from the generated component and set on the
47+
// generated column definition.
48+
const {name, cell, headerCell} = this.vcr.createComponent(column, {injector}).instance;
49+
columnDef.name = name;
50+
columnDef.cell = cell;
51+
columnDef.headerCell = headerCell;
52+
this.table.addColumnDef(columnDef);
53+
return name;
54+
});
55+
// Set the columns for the rows in the table.
56+
this.tableRow.columns = columns;
57+
this.tableHeaderRow.columns = columns;
58+
}
59+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {NgModule} from '@angular/core';
2+
import {CommonModule} from '@angular/common';
3+
4+
import {PrTableComponent} from './pr-table.component';
5+
import {MatTableModule} from '@angular/material/table';
6+
import {ColumnsModule} from './columns/columns.module';
7+
8+
@NgModule({
9+
declarations: [PrTableComponent],
10+
exports: [PrTableComponent],
11+
imports: [CommonModule, MatTableModule, ColumnsModule],
12+
})
13+
export class PrTableModule {}

0 commit comments

Comments
 (0)