## 프로젝트 개요
실시간 데이터 정렬 및 필터링 기능이 필요한 웹 애플리케이션의 핵심 컴포넌트로, 사용자에게 명확한 시각적 피드백을 제공합니다.
## 주요 기능
- 실시간 검색 필터링
- 컬럼별 정렬 기능
- 데이터 로딩 중 표시
- 네트워크 오류 처리
- 테이블 레이아웃 최적화
## 사용 방법
1. Angular CLI로 프로젝트 생성 후 `app.module.ts`에 `HttpClientModule` 추가
2. `data.service.ts`에서 API 연결 설정
3. `table.component.ts`에서 컴포넌트 로직 구현
4. `table.component.html`에서 템플릿 작성
5. AppModule에 컴포넌트 등록 후 실행
## 확장 가능성
- 추가 컬럼 타입 지원 (날짜, 숫자 등)
- 데이터 엑셀 다운로드 기능
- 무한 스크롤 구현
🅰️ 동적 데이터 표 구현
Angular 예제
중급
난이도
예제
타입
11/03
등록일
동적 데이터 표 구현
중급태그
코드 예제
// data.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class DataService {
constructor(private http: HttpClient) {}
getData(): Observable<any[]> {
return this.http.get<any[]>('https://jsonplaceholder.typicode.com/users');
}
}
// table.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-table',
templateUrl: './table.component.html'
})
export class TableComponent implements OnInit {
@Input() columns = ['id', 'name', 'email'];
data: any[] = [];
filteredData: any[] = [];
loading = false;
error: string | null = null;
constructor(private dataService: DataService) {}
ngOnInit(): void {
this.loadTable();
}
loadTable(): void {
this.loading = true;
this.error = null;
this.dataService.getData().subscribe({
next: (response) => {
this.data = response;
this.filteredData = [...this.data];
this.loading = false;
},
error: (err) => {
this.error = '데이터 로딩 중 오류가 발생했습니다.';
this.loading = false;
}
});
}
filterData(searchTerm: string): void {
if (!searchTerm.trim()) {
this.filteredData = [...this, data];
return;
}
const filtered = this.data.filter(item =>
Object.values(item).some(val =>
String(val).toLowerCase().includes(searchTerm.toLowerCase())
)
);
this.filteredData = filtered;
}
sortData(column: string): void {
this.filteredData.sort((a, b) => {
const valueA = a[column];
const valueB = b[column];
if (valueA < valueB) return -1;
if (valueA > valueB) return 1;
return 0;
});
}
}
// table.component.html
<table>
<thead>
<tr>
<th *ngFor="let column of columns">{{ column }}</th>
<th>검색</th>
</tr>
</thead>
<tbody>
<tr *ngIf="loading">
<td colspan="{{ columns.length + 1 }}" style="text-align:center;">로딩 중...</td>
</tr>
<tr *ngFor="let item of filteredData">
<td *ngFor="let column of columns">{{ item[column] }}</td>
</tr>
<tr *ngIf="!loading && !error && filteredData.length === 0">
<td colspan="{{ columns.length + 1 }}" style="text-align:center;">데이터가 없습니다.</td>
</tr>
</tbody>
</table>
<input type="text" (input)="filterData($event.target.value)">
등록일: 2025년 11월 03일 02:38
언어 정보
언어
Angular
카테고리
Web
인기도
#13
학습 팁
코드를 직접 실행해보세요
변수를 바꿔가며 실험해보세요
오류가 나도 포기하지 마세요
다른 예제도 찾아보세요