gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
как создать type script компонент грида, который основывается на material data grid?
- User
как создать type script компонент грида, который основывается на material data grid?
- Assistant
как создать type script компонент грида, который основывается на material data grid?
Для создания компонента грида на TypeScript, основанного на Material Data Grid, необходимо выполнить следующие действия:
Создать новый проект на Angular с помощью Angular CLI.
Установить пакеты для Material Data Grid с помощью npm:
npm install @angular/material @angular/cdk @angular/animations
ng generate component grid
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatGridListModule } from '@angular/material/grid-list';
import { MatTableModule } from '@angular/material/table';
import { AppComponent } from './app.component';
import { GridComponent } from './grid/grid.component';
@NgModule({
declarations: [
AppComponent,
GridComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
MatGridListModule,
MatTableModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
export interface GridModel {
id: number;
name: string;
email: string;
}
import { Injectable } from '@angular/core';
import { GridModel } from './grid.model';
@Injectable({
providedIn: 'root'
})
export class GridService {
constructor() { }
getGridData(): GridModel[] {
return [
{id: 1, name: 'John', email: 'john@example.com'},
{id: 2, name: 'Alice', email: 'alice@example.com'},
{id: 3, name: 'Bob', email: 'bob@example.com'}
];
}
}
import { Component, OnInit } from '@angular/core';
import { GridModel } from './grid.model';
import { GridService } from './grid.service';
@Component({
selector: 'app-grid',
templateUrl: './grid.component.html',
styleUrls: ['./grid.component.scss']
})
export class GridComponent implements OnInit {
public displayedColumns: string[] = ['id', 'name', 'email'];
public dataSource: GridModel[] = [];
constructor(private gridService: GridService) { }
ngOnInit(): void {
this.dataSource = this.gridService.getGridData();
}
}
<table mat-table [dataSource]="dataSource">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> ID </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef> Email </th>
<td mat-cell *matCellDef="let element"> {{element.email}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<app-grid></app-grid>
ng serve --open
После выполнения этих действий компонент грида на TypeScript, основанный на Material Data Grid, должен работать корректно.
Not yet included in a published release.