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

Cascader

원문 보기 위키 갱신

Cascader (캐스케이드 선택)

다단계로 나뉜 데이터를 선택할 때 쓰는 컴포넌트예요. 대표적인 장면은 시·도를 고르는 선택이에요. 위에서 아래로 단계를 따라 들어가며 최종 값을 고르는 구조예요.

출처: 문서

본문

소개 (Intro)

Cascader 컴포넌트는 다단계 데이터의 선택에 사용해요. 전형적인 장면은 시와 도(省市)의 선택이에요.

설치 (Install)

import { createApp } from 'vue'
import { Cascader } from '@nutui/nutui'

const app = createApp()
app.use(Cascader)

기본 사용법 (Basic Usage)

options 리스트를 넘겨 줘요.

demo: cascader basic

커스텀 속성 이름 (Custom attribute name)

textKey、valueKey、childrenKey로 속성 이름을 지정해요.

demo: cascader custom

비동기 로딩 (Async loading)

lazy로 데이터를 동적으로 가져와야 하는지 표시해요. 이때 options를 넘기지 않으면 모든 데이터를 lazyload로 로딩해야 한다는 뜻이에요. 첫 로딩은 root 속성으로 구분해요. 리프(leaf) 노드가 아닌 노드를 만나면 lazyload 메서드가 호출되는데, 파라미터는 현재 노드와 resolve 메서드예요. resolve 메서드는 반드시 호출해야 하고, 자식 노드를 넘겨 주지 않으면 그 노드는 리프 노드로 취급돼요.

demo: cascader lazy

일부 데이터의 비동기 로딩 (Async loading of partial data)

demo: cascader partial

데이터 자동 변환 (Automatic data conversion)

데이터가 평평한(flat) 구조여도 트리 구조로 변환할 수 있다면, convertConfig로 자동 변환이 필요하다고 컴포넌트에 알려 줄 수 있어요. convertConfig는 네 개의 파라미터를 받아요. topid는 최상위 노드의 부모 ID, idkey는 노드의 고유 ID, pidkey는 부모 노드 ID를 가리키는 속성 이름이고, sortkey가 있으면 Array.prototype.sort()로 같은 레벨 안에서 정렬해요.

demo: cascader convert

API

Props

Attribute Description Type Default
v-model Selected value, bidirectional binding Array -
v-model:visible selected layer boolean false
options Cascade data Array -
lazy Whether to enable dynamic loading boolean -
lazy-load Dynamic loading callback, which takes effect when dynamic loading is enabled Function -
value-key Customize the field of value in the options structure string -
text-key Customize the fields of text in the options structure string -
children-key Customize the fields of children in the options structure string -
convert-config When options is a flat structure that can be converted into a tree structure, configure the conversion rules object -
title Title string ''
close-icon-position Cancel the button position and inherit the popup component string "top-right"
closeable Whether to display the close button and inherit the popup component boolean false
poppable Whether to display the popup(After setting to false, the title is invalid) boolean true

Events

Event Description Arguments
change Triggered when the selected value changes (value, pathNodes)
path-change Triggered when the selected item changes (pathNodes)

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

Name Default Value
--nut-cascader-font-size var(--nut-font-size-2)
--nut-cascader-line-height 22px
--nut-cascader-tabs-item-padding 0 10px
--nut-cascader-bar-padding 24px 20px 17px
--nut-cascader-bar-font-size var(--nut-font-size-4)
--nut-cascader-bar-line-height 20px
--nut-cascader-bar-color var(--nut-title-color)
--nut-cascader-item-padding 10px 20px
--nut-cascader-item-color var(--nut-title-color)
--nut-cascader-item-font-size var(--nut-font-size-2)
--nut-cascader-item-active-color var(--nut-primary-color)

더 알아보기 (Learn more)