Tailwind CSS 데이터테이블 (DataTables)
Flowbite은 정렬, 검색, 페이지네이션, 필터링, CSV 내보내기까지 갖춘 완전한 기능의 데이터테이블 컴포넌트를 제공합니다. 커스텀 Tailwind CSS 클래스와 Flowbite 복잡한 유틸리티 클래스를 사용해 시작할 수 있으며, 표 안의 데이터를 자바스크립트 배열로 하드코딩하거나 쿼리로 가져올 수 있습니다.
출처: 문서
본문
데이터테이블은 검색·정렬·필터링·페이지네이션 같은 유틸리티를 쓸 수 있게 해주며, 다음과 같은 기능을 지원합니다.
- Search — 표 안의 데이터를 쿼리로 검색
- Sorting — 열 제목을 클릭해 데이터 정렬
- Filtering — 다양한 옵션으로 데이터 필터링
- Pagination — 여러 페이지로 데이터 분할
- Selection — 특정 행을 선택해 CSV 파일로 내보내기
시작하려면 프로젝트에 다음 설정파일들을 추가하면 됩니다.
폴더 구조:
flowbite-datatables/
└── public/
└── index.html
index.html 파일에는 DataTables DataList 객체를 초기화하고 스타일링하는 HTML과 JavaScript가 들어갑니다.
HTML 코드 (index.html):
<!Doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Document</title>
<script src="https://cdn.tailwindcss.com"></script>
<!-- add extra components to the datatable plugin -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/2.3.0/datatables.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/2.3.0/flowbite.min.css" />
</head>
<body>
<div class="dark:bg-gray-900">
<div class="px-4 pt-4">
<div class="overflow-x-auto">
<div class="items-start justify-between md:flex">
<div class="w-full md:w-1/2">
<div class="flex">
<form class="flex items-center w-full">
<label for="simple-search" class="sr-only">Search</label>
<div class="relative w-full">
<div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd"></path></svg>
</div>
<input data-table="basic_data_table" type="text" id="simple-search" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full ps-10 p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Search line items..." required>
</div>
</form>
<button data-table-action="export" type="button" class="ms-3 flex items-center justify-center text-white bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 font-medium rounded-lg text-sm px-4 py-2.5 dark:bg-primary-600 dark:hover:bg-primary-700 focus:outline-none dark:focus:ring-primary-800">
<svg class="h-3.5 w-3.5 mr-2" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
<path d="M11 3a1 1 0 100 2h2.586l-6.293 6.293a1 1 0 101.414 1.414L15 6.414V9a1 1 0 102 0V4a1 1 0 00-1-1h-5z"></path>
<path d="M18 12a1 1 0 00-1 1v2a1 1 0 01-1 1H4a1 1 0 01-1-1v-2a1 1 0 10-2 0v2a3 3 0 003 3h12a3 3 0 003-3v-2a1 1 0 00-1-1z"></path>
</svg>
Export .csv
</button>
</div>
</div>
</div>
<table data-table="basic_data_table" class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="p-4">
<div class="flex items-center">
<input id="checkbox-all" type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
<label for="checkbox-all" class="sr-only">checkbox</label>
</div>
</th>
<th scope="col" class="px-6 py-3">
Product name
</th>
<th scope="col" class="px-6 py-3">
Color
</th>
<th scope="col" class="px-6 py-3">
Category
</th>
<th scope="col" class="px-6 py-3">
Price
</th>
<th scope="col" class="px-6 py-3">
<span class="sr-only">Action</span>
</th>
</tr>
</thead>
<tbody>
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600">
<td class="w-4 p-4">
<div class="flex items-center">
<input id="checkbox-table-search-1" type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
<label for="checkbox-table-search-1" class="sr-only">checkbox</label>
</div>
</td>
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
Apple MacBook Pro 17"
</th>
<td class="px-6 py-4">
Silver
</td>
<td class="px-6 py-4">
Laptop
</td>
<td class="px-6 py-4">
$2999
</td>
<td class="px-6 py-4">
<a href="#" class="font-medium text-blue-600 dark:text-blue-500 hover:underline">Edit</a>
</td>
</tr>
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600">
<td class="w-4 p-4">
<div class="flex items-center">
<input id="checkbox-table-search-2" type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
<label for="checkbox-table-search-2" class="sr-only">checkbox</label>
</div>
</td>
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
Microsoft Surface Pro
</th>
<td class="px-6 py-4">
White
</td>
<td class="px-6 py-4">
Laptop PC
</td>
<td class="px-6 py-4">
$1999
</td>
<td class="px-6 py-4">
<a href="#" class="font-medium text-blue-600 dark:text-blue-500 hover:underline">Edit</a>
</td>
</tr>
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600">
<td class="w-4 p-4">
<div class="flex items-center">
<input id="checkbox-table-search-3" type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
<label for="checkbox-table-search-3" class="sr-only">checkbox</label>
</div>
</td>
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
Magic Mouse 2
</th>
<td class="px-6 py-4">
Black
</td>
<td class="px-6 py-4">
Accessories
</td>
<td class="px-6 py-4">
$99
</td>
<td class="px-6 py-4">
<a href="#" class="font-medium text-blue-600 dark:text-blue-500 hover:underline">Edit</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/2.3.0/datatables.min.js"></script>
<script>
const dataTable = new DataTable(null, {
table: "basic_data_table",
searchable: true,
fixedHeight: true,
showInfo: false,
});
</script>
</body>
</html>
검색(Search): searchable: true 속성을 켜면 표 우측 상단에 검색 입력창이 나타나 표 안의 데이터를 검색할 수 있습니다.
가져오기(Import) / 내보내기(Export): export: true 속성이나 data-table-action="export" 속성을 가진 버튼으로 표 데이터를 CSV 파일로 내보낼 수 있습니다.
더 알아보기 (Learn more)
- DataTables 문서 — 공식 가이드와 전체 예시 코드
- Flowbite JavaScript 설정 — 인터랙티브 요소 활성화