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

Tree

원문 보기 위키 갱신

Tree는 계층 구조 데이터를 트리 형태로 보여 주는 컴포넌트예요. 체크박스, 다중 선택, 드래그 앤 드롭, 비동기 로딩, 가상 스크롤까지 지원해서 큰 트리도 다룰 수 있죠. 솔직히 말하면 생물학은 잘 모르지만, 화면 위의 트리는 확실히 다룰 줄 알아요.

출처: 문서

본문

솔직히 말하면 저는 생물학을 잘 못해요. 몇 가지 종류의 나무(tree) 정도는 구별할 수 있죠.

게다가 생물학만이 아니라, 균형 트리(balanced tree)는 복습하고 조금 지나면 매번 잊어버려요.

Demos

basic.vue
custom-field.vue
multiple.vue
cascade.vue
filter.vue
drag-drop.vue
virtual.vue
async.vue
disabled.vue
prefix-and-suffix.vue
batch-render.vue
switcher-icon.vue
file-tree.vue
node-props.vue
show-line.vue
checkbox-placement.vue
override-click-behavior.vue

API

Tree Props

이름 타입 기본값 설명 버전
accordion boolean false 아코디언 확장 모드를 사용할지 여부예요. 2.31.0
allow-checking-not-loaded boolean false 아직 로드되지 않은 노드에도 연쇄(cascade) 체크를 허용할지 여부예요. 이걸 쓰려면 check-keys가 불완전할 수 있다는 점을 알고 있어야 해요. 특히 비활성화된 노드가 있을 때 naive의 체크 로직과 백엔드 체크 로직의 일관성도 신경 쓰세요. 2.28.1
allow-drop (info: { dropPosition: DropPosition, node: TreeOption, phase: 'drag' | 'drop' }) => boolean 리프 노드 안으로 드롭하는 것을 금지하는 함수. 드롭을 허용할지 여부예요.
animated boolean true 확장 애니메이션을 표시할지 여부예요. 2.33.4
block-line boolean false 노드가 행 전체로 퍼져요.
block-node boolean false 노드 이름이 행 전체로 퍼져요.
cancelable boolean true 노드의 선택 상태를 취소할 수 있는지 여부예요.
cascade boolean false 체크박스를 연쇄(cascade) 적용할지 여부예요.
check-strategy string 'all' checked 콜백의 keys 인자를 설정하는 전략이에요. all은 체크된 모든 노드를 설정해요. parent는 모든 자식 노드가 체크된 부모 노드를 설정해요. child는 모든 자식 노드를 설정해요.
checkable boolean false 선택 박스를 표시할지 여부예요.
checkbox-placement 'left' | 'right' 'left' 체크박스의 위치예요. 2.28.3
children-field string 'children' TreeOption의 children 필드예요.
checked-keys Array<string | number> undefined 트리의 체크된 키들이에요.
check-on-click boolean | ((node: TreeOption) => boolean) false checkable이 true일 때 노드 클릭으로 체크를 트리거하게 해 줘요. 2.31.0
data Array<TreeOption> [] 트리의 노드 데이터예요. data를 리셋하면 일부 비제어 상태가 지워져요. 데이터를 수정해야 한다면 트리를 제어(controlled) 방식으로 동작시키는 게 좋아요.
default-checked-keys Array<string | number> [] 기본으로 선택된 다중 옵션이에요.
default-expand-all boolean false 모든 옵션을 펼쳐요.
default-expanded-keys Array<string | number> [] 기본으로 펼쳐지는 항목들이에요.
default-selected-keys Array<string | number> [] 기본으로 선택되는 노드들이에요.
draggable boolean false 드래그할 수 있는지 여부예요.
expand-on-dragenter boolean true dragenter 이후 노드를 펼칠지 여부예요.
expand-on-click boolean false 클릭 후 노드를 펼치거나 접을지 여부예요. 2.29.1
expanded-keys Array<string | number> undefined 설정하면 펼침 상태가 제어(controlled) 방식으로 동작해요.
ellipsis boolean false 너비를 넘는 텍스트를 생략(ellipsis)할지 여부예요. 2.43.0
filter (pattern: string, node: TreeOption) => boolean 단순 문자열 기반 필터. 패턴으로 트리 노드를 필터하는 함수예요.
get-children (option: any) => unknown undefined 옵션의 children을 가져와요. 2.34.3
indent number 24 트리 각 레벨의 들여쓰기예요. 2.9.0
indeterminate-keys Array<string | number> undefined 트리의 indeterminate 키들이에요.
keyboard boolean true 키보드 조작을 지원할지 여부예요. 2.32.2
key-field string 'key' TreeOption의 key 필드예요.
label-field string 'label' TreeOption의 label 필드예요.
disabled-field string 'disabled' TreeOption의 disabled 필드예요. 2.32.2
node-props (info: { option: TreeOption }) => HTMLAttributes undefined 노드의 HTML 속성이에요. 2.25.0
multiple boolean false 노드 다중 선택을 허용할지 여부예요.
on-load (node: TreeOption) => Promise<void> undefined 데이터를 비동기로 로드할 때의 콜백 함수예요. 데이터가 로드되지 않았다면 프로미스가 false로 이행하거나 거부되게 해야 해요. 그렇지 않으면 로딩 애니메이션이 끝나지 않아요. Non void Promise 2.34.3
override-default-node-click-behavior (info: { option: TreeSelectOption }) => 'toggleExpand' | 'toggleSelect' | 'toggleCheck' | 'default' | 'none' undefined 기본 노드 클릭 동작을 덮어써요. 2.37.0
pattern string '' 기본으로 검색할 문자열이에요.
render-label (info: { option: TreeOption, checked: boolean, selected: boolean }) => VNodeChild undefined 모든 옵션 label의 렌더 함수예요.
render-prefix (info: { option: TreeOption, checked: boolean, selected: boolean }) => VNodeChild undefined 모든 옵션 prefix의 렌더 함수예요.
render-suffix (info: { option: TreeOption, checked: boolean, selected: boolean }) => VNodeChild undefined 모든 옵션 suffix의 렌더 함수예요.
render-switcher-icon (props: { option: TreeOption, expanded: boolean, selected: boolean }) => VNodeChild undefined 옵션 스위처 아이콘의 렌더 함수예요. 2.24.0, props 2.34.0
scrollbar-props ScrollbarProps undefined Scrollbar props를 참고하세요.
selectable boolean true 노드를 선택할 수 있는지 여부예요.
selected-keys Array<string | number> undefined 설정하면 선택 상태가 제어(controlled) 방식으로 동작해요.
show-irrelevant-nodes boolean true 트리가 필터 모드일 때 일치하지 않는 노드를 필터링(숨김)할지 여부예요. 2.28.1
show-line boolean false 연결선을 표시할지 여부예요. 2.35.0
spin-props { strokeWidth?: number, stroke?: string, scale?: number, radius?: number } undefined 로딩 아이콘 속성이에요. 2.44.0
virtual-scroll boolean false 가상 스크롤을 켤지 여부예요. 미리 트리의 스타일 높이를 적절히 설정해야 해요.
watch-props Array<'defaultCheckedKeys' | 'defaultSelectedKeys' |'defaultExpandedKeys'> undefined 감시할 기본 prop 이름들이에요. prop이 바뀌면 컴포넌트가 갱신돼요. 참고: watch-props 자체는 반응형이 아니에요.
on-dragend (data: { node: TreeOption, event: DragEvent }) => void undefined 노드가 드래그 동작을 마친 뒤의 콜백 함수예요.
on-dragenter (data: { node: TreeOption, event: DragEvent }) => void undefined 노드 드래그 앤 드롭 중의 콜백 함수예요.
on-dragleave (data: { node: TreeOption, event: DragEvent }) => void 드래그한 노드가 다른 노드를 떠난 뒤의 콜백 함수예요.
on-dragstart (data: { node: TreeOption, event: DragEvent }) => void undefined 특정 노드 드래그를 시작할 때의 콜백 함수예요.
on-drop (data: { node: TreeOption, dragNode: TreeOption, dropPosition: 'before' | 'inside' | 'after', event: DragEvent }) => void undefined 노드가 드래그 동작을 마친 뒤의 콜백 함수예요.
on-update:checked-keys (keys: Array<string | number>, option: Array<TreeOption | null>), meta: { node: TreeOption | null, action: 'check' | 'uncheck' }) => void undefined 노드 체크 옵션이 바뀔 때의 콜백 함수예요. meta 2.34.0
on-update:indeterminate-keys (keys: Array<string | number>, option: Array<TreeOption | null>) => void undefined 노드 indeterminate 옵션이 바뀔 때의 콜백 함수예요.
on-update:expanded-keys (keys: Array<string | number>, option: Array<TreeOption | null>), meta: { node: TreeOption | null, action: 'expand' | 'collapse' | 'filter' }) => void undefined 노드 확장 항목이 바뀔 때의 콜백 함수예요. meta 2.34.0
on-update:selected-keys (keys: Array<string | number>, option: Array<TreeOption | null>), meta: { node: TreeOption | null, action: 'select' | 'unselect' }) => void undefined 노드의 선택 항목이 바뀔 때의 콜백 함수예요. meta 2.34.0

TreeOption Properties

이름 타입 설명
key string | number 노드의 키예요. 고유해야 해요. key-field로 필드 이름을 바꿀 수 있어요.
label string 노드의 라벨이에요. label-field로 필드 이름을 바꿀 수 있어요.
checkboxDisabled? boolean 체크박스가 비활성화됐는지 여부예요.
children? TreeOption[] 노드의 자식 노드들이에요.
disabled? boolean 노드가 비활성화됐는지 여부예요.
isLeaf? boolean 노드가 리프인지 여부예요. 비동기 확장 모드에서는 필수예요.
prefix? string | (() => VNodeChild) 노드의 prefix예요.
suffix? string | (() => VNodeChild) 노드의 suffix예요.

Tree Slots

이름 파라미터 설명 버전
empty () 트리의 빈 상태 슬롯이에요.

Methods

Tree Methods

이름 파라미터 설명 버전
scrollTo ScrollTo 가상 스크롤 모드에서 특정 노드로 스크롤해요. 2.32.2, ScrollTo 2.37.0
getCheckedData () => { keys: Array<string | number>, options: Array<TreeOption | null> } 체크된 데이터를 가져와요. 2.34.1
getIndeterminateData () => { keys: Array<string | number>, options: Array<TreeOption | null> } indeterminate 데이터를 가져와요. 2.34.1

ScrollTo Type

interface ScrollTo {
  (x: number, y: number): void
  (options: { left?: number, top?: number, debounce?: boolean }): void
  (options: { index: number, debounce?: boolean }): void
  (options: { key: string | number, debounce?: boolean }): void
  (options: { position: 'top' | 'bottom', debounce?: boolean }): void
}

Others

  1. treeGetClickTarget: (e: MouseEvent) => ('checkbox' | 'switcher' | 'node'): 노드 클릭 이벤트의 클릭 대상을 가져와요. nodeProps.onClick에서 쓸 수 있어요. naive-ui 패키지에서 직접 import할 수 있어요.

더 알아보기 (Learn more)

데이터 필드 이름이 기본값과 다르면 key-field, label-field, children-field로 맞출 수 있어요. 트리에서 검색·필터를 쓴다면 pattern과 show-irrelevant-nodes 조합의 동작도 함께 확인해 보세요.