아래에서 작성한 프로젝트 이어서 작업
AppModule ├── (AppService) │ ├── HomeModule │ └── UserModule │ └── UserInfoComponent (UserTable)
UserInfoComponent
에서 유저 목록을 볼 수 있는 테이블을 만들어보자
angular material
을 계속 사용하기 위해서 Dokerfile에
RUN ng add @angular/material
추가# ../Dockerfile FROM node:14.21-alpine RUN mkdir /angular WORKDIR /angular RUN npm update & npm install -g @angular/cli@13 COPY package.json package-lock.json ./ COPY . . RUN ng add @angular/material
# data-class/user.ts export class User { id: number; username: string; created_at: string; constructor() { this.id = 0; this.username = ''; this.created_at = ''; } } export const USER_DATAS: Array<User> = [ { id: 1, username: 'username 1', created_at: '2023-01-01' }, { id: 2, username: 'username 2', created_at: '2023-01-02' }, { id: 3, username: 'username 3', created_at: '2023-01-03' }, { id: 4, username: 'username 4', created_at: '2023-01-04' }, { id: 5, username: 'username 5', created_at: '2023-01-05' }, { id: 6, username: 'username 6', created_at: '2023-01-06' }, ];
# user/user.module.ts ... import { MatTableModule } from '@angular/material/table'; @NgModule({ declarations: [ UserComponent, UserInfoComponent, UserInfoDetailComponent ], imports: [ ... MatTableModule ], }) export class UserModule {}
# user/user-info/user-info.component.ts ... import { UserInfoDetailComponent } from './user-info-detail/user-info-detail.component'; import { USER_DATAS } from 'src/app/data-class/user'; @Component({ selector: 'app-user-info', templateUrl: './user-info.component.html', styleUrls: ['./user-info.component.css'], }) export class UserInfoComponent implements OnInit { constructor() {} ngOnInit(): void {} displayedColumns: string[] = ['id', 'username', 'created_at']; dataSource = USER_DATAS; }
# user/user-info/user-info.component.html <table mat-table [dataSource]="dataSource" class="mat-elevation-z8"> <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="username"> <th mat-header-cell *matHeaderCellDef>Username</th> <td mat-cell *matCellDef="let element">{{ element.username }}</td> </ng-container> <ng-container matColumnDef="created_at"> <th mat-header-cell *matHeaderCellDef>Created_at</th> <td mat-cell *matCellDef="let element">{{ element.created_at }}</td> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr> </table>
구현 코드
참고