본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

DataTable — 데이터 테이블

원문 보기 위키 갱신

정렬, 필터, 페이지네이션, 선택, 확장, 편집, 컬럼 재정렬/리사이즈, 행 재정렬, 그룹핑, 고정 섹션, CSV 내보내기, 트리 모드를 갖춘 스타일 없는 데이터 테이블이에요.

출처: 문서

본문

마크업, 레이아웃, 스타일링을 완전히 제어하며 완전히 커스텀한 테이블을 조합해 보세요. 모든 기능은 선택 사항이고 데이터 속성으로 구동돼요.

기능 (Features)

  • 하위 컴포넌트를 갖춘 복합 컴포넌트 API: Root, Header, TableContainer, Table, THead, THeadRow, THeadCell, TBody, FrozenTBody, Row, Cell, TFoot, TFootRow, TFootCell, Footer, Sort, SortIndicator, SortOrder, Filter, Pagination, Selection, RowToggle, RowToggleIndicator, RowExpansion, RowGroupHeader, RowGroupFooter, CellEditor, CellEditorDisplay, CellEditorContent, RowEditor, RowEditorInit, RowEditorSave, RowEditorCancel, ColumnToggle, ColumnReorder, ColumnReorderTarget, ColumnResizer, ColumnResizeIndicator, RowReorder, Export, Loading, EmptyTBody
  • 정렬(단일, 다중, 제거 가능), 컬럼별 행·메뉴 필터, 선택한 필드에 걸친 전역 필터
  • 내장 슬라이싱 또는 지연 모드의 페이지네이션
  • 단일, 다중 체크박스, 다중 행, 라디오 모드 선택. Shift로 범위, Ctrl/Meta로 추가·제거
  • Tab 탐색, 외부 클릭 커밋, Escape 취소가 있는 셀 또는 행 편집
  • 드래그 핸들을 통한 컬럼 재정렬, 컬럼 리사이즈(fit 또는 expand), 행 재정렬
  • Cell과 THeadCell의 frozen prop을 통한 왼쪽·오른쪽 고정 컬럼
  • 트리 모드는 같은 컴포넌트를 재사용해요. Root에서 treeMode를 켜고 { key, data, children? } 노드를 전달해요
  • 커스텀 필드와 헤더가 있는 CSV 내보내기

사용법 (Usage)

import { DataTable } from 'primereact/datatable';
<DataTable.Root data={products} dataKey="id">
    <DataTable.TableContainer>
        <DataTable.Table>
            <DataTable.THead>
                <DataTable.THeadRow>
                    <DataTable.THeadCell>Name</DataTable.THeadCell>
                    <DataTable.THeadCell>Category</DataTable.THeadCell>
                </DataTable.THeadRow>
            </DataTable.THead>
            <DataTable.TBody>
                {({ item }) => (
                    <DataTable.Row>
                        <DataTable.Cell>{item.name}</DataTable.Cell>
                        <DataTable.Cell>{item.category}</DataTable.Cell>
                    </DataTable.Row>
                )}
            </DataTable.TBody>
        </DataTable.Table>
    </DataTable.TableContainer>
</DataTable.Root>

동작 (Behavior)

렌더 함수 children (Render Function Children)

DataTable.TBody는 각 행에 대해 { item, index, groupMeta }를 받는 렌더 함수를 받아요. 상태ful 하위 컴포넌트(Selection, Pagination, Filter, Sort, RowToggle, CellEditor)도 렌더-prop 슬롯 컨텍스트를 노출해서 실시간 상태를 읽고 노출된 메서드를 호출할 수 있어요.

<DataTable.Sort field="price">{({ isSorted, sortOrder }) => <span>Price {isSorted ? (sortOrder === 1 ? '↑' : '↓') : ''}</span>}</DataTable.Sort>

다형성 렌더링 (Polymorphic Rendering)

어느 하위 컴포넌트에서든 as를 사용해 기본 요소를 바꾸거나 다른 컴포넌트를 통해 렌더링해요 (예: Sort 트리거에 스타일이 적용된 Button).

<DataTable.RowEditorInit as={Button} variant="text" />

정렬 (Sort)

헤더 콘텐츠를 DataTable.Sort로 감싸고 field를 제공해요. 클릭하면서 Ctrl / Cmd를 누르면 다중 정렬해요. Root에서 removableSort를 활성화하면 3-상태 클릭 사이클을 사용할 수 있어요.

<DataTable.THeadCell>
    <DataTable.Sort field="price">
        Price <DataTable.SortIndicator match="asc">↑</DataTable.SortIndicator>
    </DataTable.Sort>
</DataTable.THeadCell>

필터 (Filter)

필터링할 각 컬럼에 DataTable.Filter를 렌더링하고 globalFilter에 바인딩된 별도의 키워드 입력을 넣어요. 변경마다 적용되는 인라인 필터에는 display="row"를 사용하고, 여러 제약 조건, AND/OR 연산자, 명시적 Apply / Clear가 있는 오버레이에는 display="menu"를 사용해요.

<DataTable.Filter field="name" display="row" dataType="text">
    {({ value, onChange }) => <input value={value ?? ''} onChange={(e) => onChange(e, e.target.value)} />}
</DataTable.Filter>

선택 (Selection)

Root에서 selectionMode를 'single' 또는 'multiple'로 설정하고 각 셀 또는 헤더 트리거를 DataTable.Selection으로 감싸요. 슬롯 컨텍스트는 행에 isSelected / toggle, 헤더에 isAllSelected / isSomeSelected / toggleAll을 노출해요.

<DataTable.Selection>{({ isSelected, toggle }) => <Checkbox checked={isSelected} onCheckedChange={toggle} />}</DataTable.Selection>

확장 & 트리 모드 (Expansion & Tree Mode)

DataTable.RowToggle은 확장 상태를 자동으로 읽고, 트리 모드에서 행에 자식이 없으면 리프 스페이서(leaf-spacer)를 렌더링해서 들여쓰기가 정렬되도록 해요. Root에서 treeMode를 설정하면 테이블이 role="treegrid"로 바뀌고 모든 행에 트리 ARIA가 찍혀요.

<DataTable.Root treeMode dataKey="key" data={nodes}>
    {/* same Table / THead / TBody markup; rows now indent automatically */}
</DataTable.Root>

셀 및 행 편집 (Cell and Row Editing)

각 편집 가능한 셀을 DataTable.CellEditor로 감싸고 field / rowIndex / rowData를 주며, Display(읽기 모드)와 Content(편집 모드) 두 슬롯을 제공해요. editMode="row"일 때는 전용 셀에서 RowEditor.Init / Save / Cancel 트리거로 셀별 스위치를 대체해요.

<DataTable.CellEditor field="price" rowIndex={index} rowData={item}>
    <DataTable.CellEditorDisplay>{item.price}</DataTable.CellEditorDisplay>
    <DataTable.CellEditorContent>
        <input defaultValue={item.price} />
    </DataTable.CellEditorContent>
</DataTable.CellEditor>

컬럼 재정렬 & 리사이즈 (Column Reorder & Resize)

Root에서 reorderableColumns를 설정하고 각 THeadCell 안에 DataTable.ColumnReorder 핸들을 추가해요. 리사이즈하려면 resizableColumns를 설정하고 헤더 안에 DataTable.ColumnResizer를 놓고, 실시간 드래그 선을 위해 ColumnResizeIndicator와 짝지어요.

행 재정렬 (Row Reorder)

Root에서 reorderableRows를 설정하고 행 셀에 DataTable.RowReorder 핸들을 추가해요. 훅은 포인터-다운에서 주변 <tr>을 드래그 소스로 삼아 새 배열과 함께 onRowReorder를 발생시켜요.

페이지네이션 (Pagination)

테이블 아래 어디든 DataTable.Pagination을 렌더링해요. page, pageCount, rows, totalRecords, first, canPrev, canNext, onPageChange, onRowsChange를 노출하는 렌더-prop 래퍼로 동작해요.

<DataTable.Pagination>{({ page, pageCount, onPageChange }) => <div>{/* custom paginator UI */}</div>}</DataTable.Pagination>

패스 스루 (Pass Through)

<DocPTViewer name="datatable-pt" components={['DataTableRoot']} />

API

DataTableRoot

Attribute Value
data-scope "datatable"
data-part "root"
data-loading Present while loading
data-row-hover Present when rowHover is true
data-highlight-on-select Present when selected rows should highlight
data-selection-mode "single" | "multiple"
data-size "small" | "large"
data-striped-rows Present when stripedRows is true
data-show-gridlines Present when showGridlines is true
Label Type Description
root DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
header DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the header's DOM element (above the table).
tableContainer DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the table container's DOM element.
table DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the table's DOM element.
head DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the thead's DOM element.
theadRow DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the thead row's DOM element.
theadCell DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the thead cell's DOM element.
body DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the tbody's DOM element.
frozenBody DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the frozen tbody's DOM element.
row DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row's DOM element.
cell DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the cell's DOM element.
foot DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the tfoot's DOM element.
tfootRow DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the tfoot row's DOM element.
tfootCell DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the tfoot cell's DOM element.
footer DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the footer's DOM element (below the table).
emptyTBody DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the empty body's DOM element.
loading DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the loading overlay's DOM element.
sort DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the sort trigger's DOM element.
sortIndicator DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the sort indicator's DOM element.
sortOrder DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the multi-sort order badge's DOM element.
filter DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the filter's DOM element.
rowToggle DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row toggle's DOM element.
rowToggleIndicator DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row toggle indicator's DOM element.
rowExpansion DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row expansion's DOM element.
rowGroupHeader DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row group header's DOM element.
rowGroupFooter DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row group footer's DOM element.
cellEditor DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the cell editor's DOM element.
cellEditorDisplay DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the cell editor display element.
cellEditorContent DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the cell editor content element.
rowEditorInit DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row editor init button.
rowEditorSave DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row editor save button.
rowEditorCancel DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row editor cancel button.
rowReorder DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the row reorder handle.
columnReorder DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the column reorder handle.
columnResizer DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the column resizer handle.
columnResizeIndicator DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the column resize indicator's DOM element.
export DataTableRootPassThroughType<HTMLAttributes> Used to pass attributes to the export trigger's DOM element.

DataTableHeader

Label Type Description
root DataTableHeaderPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTableContainer

Attribute Value
data-scope "datatable"
data-part "table-container"
Label Type Description
root DataTableTableContainerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTable

Attribute Value
data-scope "datatable"
data-part "table"
role "table" or "treegrid"
Label Type Description
root DataTableTablePassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTHead

Label Type Description
root DataTableTHeadPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTHeadRow

Label Type Description
root DataTableTHeadRowPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTHeadCell

Attribute Value
data-frozen Present when the cell is frozen
data-align-frozen "left" | "right"
Label Type Description
root DataTableTHeadCellPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTBody

Label Type Description
root DataTableTBodyPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableFrozenTBody

Label Type Description
root DataTableFrozenTBodyPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableRow

Attribute Value
data-scope "datatable"
data-part "row"
data-index Zero-based row index in the rendered slice
data-selected Present when the row is selected
aria-selected Reflects selection state when selection mode is active
aria-level Tree depth (one-based) in tree mode
aria-expanded Reflects expansion when the row has children
aria-posinset One-based position among siblings (tree mode)
aria-setsize Total siblings at the same depth (tree mode)
data-drag-source Present while dragging this row
data-dragpoint-top Present when drop indicator targets the top edge
data-dragpoint-bottom Present when drop indicator targets the bottom edge
Label Type Description
root DataTableRowPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableCell

Attribute Value
data-scope "datatable"
data-part "cell"
role "gridcell"
data-frozen Present when the cell is frozen
data-align-frozen "left" | "right"
data-editable-cell Present when wrapped by a CellEditor
Label Type Description
root DataTableCellPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTFoot

Label Type Description
root DataTableTFootPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTFootRow

Label Type Description
root DataTableTFootRowPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableTFootCell

Label Type Description
root DataTableTFootCellPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableFooter

Label Type Description
root DataTableFooterPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableSort

Attribute Value
data-scope "datatable"
data-part "sort"
role "button"
data-sorted Present when this column is part of the active sort
data-unsorted Present when this column is not part of the sort
data-sort-order "asc" | "desc" when sorted
aria-sort "ascending" | "descending" | "none"
Label Type Description
root DataTableSortPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableSortIndicator

Label Type Description
root DataTableSortIndicatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableSortOrder

Label Type Description
root DataTableSortOrderPassThroughType<HTMLAttributes>

DataTableFilter

Attribute Value
data-scope "datatable"
data-part "filter"
data-display "row" | "menu"
data-active Present when the column has an active filter
Label Type Description
root DataTableFilterPassThroughType<HTMLAttributes>

DataTablePagination

Label Type Description
root DataTablePaginationPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableSelection

Label Type Description
root DataTableSelectionPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableRowToggle

Attribute Value
data-scope "datatable"
data-part "row-toggle" (button) or "row-toggle-spacer"
data-tree-level Zero-based tree depth (tree mode only)
aria-expanded Reflects current expansion state
Label Type Description
root DataTableRowTogglePassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableRowToggleIndicator

Label Type Description
root DataTableRowToggleIndicatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableRowExpansion

Label Type Description
root DataTableRowExpansionPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableRowGroupHeader / DataTableRowGroupFooter

<DataTable.RowGroupHeader>와 <DataTable.RowGroupFooter>는 Root에 groupField가 설정되면 그룹핑된 데이터에 대한 헤더 / 푸터 행을 렌더링해요. 모든 props를 <tr> 요소로 전달하고, 헤드리스 그룹핑 메타데이터가 보고하는 각 그룹의 시작(헤더) / 끝(푸터)에서만 해당 행을 렌더링해요.

<DataTable.TBody>
    {({ item, index, groupMeta }) => (
        <>
            {groupMeta?.isGroupStart && (
                <DataTable.RowGroupHeader>
                    <DataTable.Cell colSpan={3}>{String(groupMeta.groupValue)}</DataTable.Cell>
                </DataTable.RowGroupHeader>
            )}
            <DataTable.Row>{/* … */}</DataTable.Row>
            {groupMeta?.isGroupEnd && (
                <DataTable.RowGroupFooter>
                    <DataTable.Cell colSpan={3}>Total: {groupMeta.groupCount}</DataTable.Cell>
                </DataTable.RowGroupFooter>
            )}
        </>
    )}
</DataTable.TBody>

DataTableCellEditor

Attribute Value
data-scope "datatable"
data-part "cell-editor"
data-row-index The row index (used by Tab traversal)
data-field The field name (used by Tab traversal)
data-row-key The row key (used to track edits across sort)
data-editing Present while the cell is in edit mode
Label Type Description
root DataTableCellEditorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableCellEditorDisplay

Label Type Description
root DataTableCellEditorDisplayPassThroughType<HTMLAttributes>

DataTableCellEditorContent

Label Type Description
root DataTableCellEditorContentPassThroughType<HTMLAttributes>

DataTableRowEditor

Label Type Description
root DataTableRowEditorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableRowEditorInit

Label Type Description
root DataTableRowEditorInitPassThroughType<ButtonHTMLAttributes>

DataTableRowEditorSave

Label Type Description
root DataTableRowEditorSavePassThroughType<ButtonHTMLAttributes>

DataTableRowEditorCancel

Label Type Description
root DataTableRowEditorCancelPassThroughType<ButtonHTMLAttributes>

DataTableColumnToggle

Label Type Description
root DataTableColumnTogglePassThroughType<HTMLAttributes>

DataTableColumnReorder

Label Type Description
root DataTableColumnReorderPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableColumnReorderTarget

Label Type Description
root DataTableColumnReorderTargetPassThroughType<HTMLAttributes>

DataTableColumnResizer

Label Type Description
root DataTableColumnResizerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableColumnResizeIndicator

Label Type Description
root DataTableColumnResizeIndicatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableRowReorder

Label Type Description
root DataTableRowReorderPassThroughType<HTMLAttributes>

DataTableExport

Label Type Description
root DataTableExportPassThroughType<HTMLAttributes>

DataTableLoading

Label Type Description
root DataTableLoadingPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

DataTableEmptyTBody

Label Type Description
root DataTableEmptyTBodyPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

접근성 (Accessibility)

스크린 리더 (Screen Reader)

DataTable.Table은 평면 데이터에 role="table"을 적용하고 treeMode가 활성화되면 role="treegrid"를 적용해요. 각 행은 role="row"와 함께 aria-rowindex(1부터 시작)를 노출하고, 트리 모드에서는 aria-level, aria-expanded, aria-posinset, aria-setsize를 노출해요. 셀은 role="gridcell"을 사용해요. 정렬 트리거는 role="button", aria-sort, 설명적인 aria-label을 노출해요. 선택 래퍼는 기본 입력의 체크박스/라디오 의미론을 전달해요. 테이블에 보이는 제목이 없을 때는 Root에 aria-label 또는 aria-labelledby로 접근 가능한 이름을 제공해요.

키보드 지원 (Keyboard Support)

Key Function
tab Moves focus through interactive elements (sort triggers, filter inputs, rows).
arrow up / arrow down Moves focus between rows when a row is focused.
shift + arrow up / arrow down Extends row selection while moving focus.
arrow left In tree mode, collapses the focused row, or moves focus to its parent if already collapsed.
arrow right In tree mode, expands the focused row, or moves focus to its first child if already expanded.
home / end Moves focus to the first / last row.
space Toggles selection on the focused row.
enter Toggles selection on the focused row, or opens the first editable cell in cell edit mode.
escape Cancels the active cell edit and returns focus to the row.
tab (in cell edit) Commits the edit and moves to the next editable cell, wrapping across rows.
shift + tab (in cell edit) Commits the edit and moves to the previous editable cell.
enter (in cell edit) Commits the edit and stays in place.

더 알아보기 (Learn more)