ElementUI安裝與使用指南
Cascader 級(jí)聯(lián)選擇器
點(diǎn)擊下載learnelementuispringboot項(xiàng)目源碼
效果圖
el-cascader.vue(Cascader 級(jí)聯(lián)選擇器)頁面效果圖
項(xiàng)目里el-cascader.vue代碼
<script>
let id = 0;
export default {
name: 'el_cascader',
data() {
return {
value: [],
options: [{
value: 'zhinan',
label: '指南',
children: [{
value: 'shejiyuanze',
label: '設(shè)計(jì)原則',
children: [{
value: 'yizhi',
label: '一致'
}, {
value: 'fankui',
label: '反饋'
}, {
value: 'xiaolv',
label: '效率'
}, {
value: 'kekong',
label: '可控'
}]
}, {
value: 'daohang',
label: '導(dǎo)航',
children: [{
value: 'cexiangdaohang',
label: '側(cè)向?qū)Ш?
}, {
value: 'dingbudaohang',
label: '頂部導(dǎo)航'
}]
}]
}, {
value: 'zujian',
label: '組件',
children: [{
value: 'basic',
label: 'Basic',
children: [{
value: 'layout',
label: 'Layout 布局'
}, {
value: 'color',
label: 'Color 色彩'
}, {
value: 'typography',
label: 'Typography 字體'
}, {
value: 'icon',
label: 'Icon 圖標(biāo)'
}, {
value: 'button',
label: 'Button 按鈕'
}]
}, {
value: 'form',
label: 'Form',
children: [{
value: 'radio',
label: 'Radio 單選框'
}, {
value: 'checkbox',
label: 'Checkbox 多選框'
}, {
value: 'input',
label: 'Input 輸入框'
}, {
value: 'input-number',
label: 'InputNumber 計(jì)數(shù)器'
}, {
value: 'select',
label: 'Select 選擇器'
}, {
value: 'cascader',
label: 'Cascader 級(jí)聯(lián)選擇器'
}, {
value: 'switch',
label: 'Switch 開關(guān)'
}, {
value: 'slider',
label: 'Slider 滑塊'
}, {
value: 'time-picker',
label: 'TimePicker 時(shí)間選擇器'
}, {
value: 'date-picker',
label: 'DatePicker 日期選擇器'
}, {
value: 'datetime-picker',
label: 'DateTimePicker 日期時(shí)間選擇器'
}, {
value: 'upload',
label: 'Upload 上傳'
}, {
value: 'rate',
label: 'Rate 評(píng)分'
}, {
value: 'form',
label: 'Form 表單'
}]
}, {
value: 'data',
label: 'Data',
children: [{
value: 'table',
label: 'Table 表格'
}, {
value: 'tag',
label: 'Tag 標(biāo)簽'
}, {
value: 'progress',
label: 'Progress 進(jìn)度條'
}, {
value: 'tree',
label: 'Tree 樹形控件'
}, {
value: 'pagination',
label: 'Pagination 分頁'
}, {
value: 'badge',
label: 'Badge 標(biāo)記'
}]
}, {
value: 'notice',
label: 'Notice',
children: [{
value: 'alert',
label: 'Alert 警告'
}, {
value: 'loading',
label: 'Loading 加載'
}, {
value: 'message',
label: 'Message 消息提示'
}, {
value: 'message-box',
label: 'MessageBox 彈框'
}, {
value: 'notification',
label: 'Notification 通知'
}]
}, {
value: 'navigation',
label: 'Navigation',
children: [{
value: 'menu',
label: 'NavMenu 導(dǎo)航菜單'
}, {
value: 'tabs',
label: 'Tabs 標(biāo)簽頁'
}, {
value: 'breadcrumb',
label: 'Breadcrumb 面包屑'
}, {
value: 'dropdown',
label: 'Dropdown 下拉菜單'
}, {
value: 'steps',
label: 'Steps 步驟條'
}]
}, {
value: 'others',
label: 'Others',
children: [{
value: 'dialog',
label: 'Dialog 對(duì)話框'
}, {
value: 'tooltip',
label: 'Tooltip 文字提示'
}, {
value: 'popover',
label: 'Popover 彈出框'
}, {
value: 'card',
label: 'Card 卡片'
}, {
value: 'carousel',
label: 'Carousel 走馬燈'
}, {
value: 'collapse',
label: 'Collapse 折疊面板'
}]
}]
}, {
value: 'ziyuan',
label: '資源',
children: [{
value: 'axure',
label: 'Axure Components'
}, {
value: 'sketch',
label: 'Sketch Templates'
}, {
value: 'jiaohu',
label: '組件交互文檔'
}]
}],
options2: [{
value: 'zhinan',
label: '指南',
disabled: true,
children: [{
value: 'shejiyuanze',
label: '設(shè)計(jì)原則',
children: [{
value: 'yizhi',
label: '一致'
}, {
value: 'fankui',
label: '反饋'
}, {
value: 'xiaolv',
label: '效率'
}, {
value: 'kekong',
label: '可控'
}]
}, {
value: 'daohang',
label: '導(dǎo)航',
children: [{
value: 'cexiangdaohang',
label: '側(cè)向?qū)Ш?
}, {
value: 'dingbudaohang',
label: '頂部導(dǎo)航'
}]
}]
}, {
value: 'zujian',
label: '組件',
children: [{
value: 'basic',
label: 'Basic',
children: [{
value: 'layout',
label: 'Layout 布局'
}, {
value: 'color',
label: 'Color 色彩'
}, {
value: 'typography',
label: 'Typography 字體'
}, {
value: 'icon',
label: 'Icon 圖標(biāo)'
}, {
value: 'button',
label: 'Button 按鈕'
}]
}, {
value: 'form',
label: 'Form',
children: [{
value: 'radio',
label: 'Radio 單選框'
}, {
value: 'checkbox',
label: 'Checkbox 多選框'
}, {
value: 'input',
label: 'Input 輸入框'
}],
}],
}
],
props: {multiple: true},
options3: [{
value: 1,
label: '東南',
children: [{
value: 2,
label: '上海',
children: [
{value: 3, label: '普陀'},
{value: 4, label: '黃埔'},
{value: 5, label: '徐匯'}
]
}, {
value: 7,
label: '江蘇',
children: [
{value: 8, label: '南京'},
{value: 9, label: '蘇州'},
{value: 10, label: '無錫'}
]
}, {
value: 12,
label: '浙江',
children: [
{value: 13, label: '杭州'},
{value: 14, label: '寧波'},
{value: 15, label: '嘉興'}
]
}]
}, {
value: 17,
label: '西北',
children: [{
value: 18,
label: '陜西',
children: [
{value: 19, label: '西安'},
{value: 20, label: '延安'}
]
}, {
value: 21,
label: '新疆維吾爾族自治區(qū)',
children: [
{value: 22, label: '烏魯木齊'},
{value: 23, label: '克拉瑪依'}
]
}]
}],
props2: {
lazy: true,
lazyLoad(node, resolve) {
const {level} = node;
setTimeout(() => {
const nodes = Array.from({length: level + 1})
.map(item => ({
value: ++id,
label: `選項(xiàng)${id}`,
leaf: level >= 2
}));
// 通過調(diào)用resolve將子節(jié)點(diǎn)數(shù)據(jù)返回,通知組件數(shù)據(jù)加載完成
resolve(nodes);
}, 1000);
}
},
}
},
methods: {
handleChange(value) {
console.log(value);
}
},
}
</script>
<template>
<div class="el_cascader_root">
<h2>Cascader 級(jí)聯(lián)選擇器</h2>
<h5>當(dāng)一個(gè)數(shù)據(jù)集合有清晰的層級(jí)結(jié)構(gòu)時(shí),可通過級(jí)聯(lián)選擇器逐級(jí)查看并選擇。</h5>
<h3>一、基礎(chǔ)用法</h3>
<h5>有兩種觸發(fā)子菜單的方式</h5>
<p>
只需為 Cascader 的options屬性指定選項(xiàng)數(shù)組即可渲染出一個(gè)級(jí)聯(lián)選擇器。通過props.expandTrigger可以定義展開子級(jí)菜單的觸發(fā)方式。
</p>
<div class="blockParent">
<!--
//justify-content: center; 讓子元素水平居中
//align-items: center; 讓子元素垂直居中
-->
<div class="block">
<span class="demonstration">默認(rèn) click 觸發(fā)子菜單</span>
<el-cascader
v-model="value"
:options="options"
@change="handleChange"></el-cascader>
</div>
<div class="block">
<span class="demonstration">hover 觸發(fā)子菜單</span>
<el-cascader
v-model="value"
:options="options"
:props="{ expandTrigger: 'hover' }"
@change="handleChange"></el-cascader>
</div>
</div>
<h3>二、禁用選項(xiàng)</h3>
<h5>通過在數(shù)據(jù)源中設(shè)置 disabled 字段來聲明該選項(xiàng)是禁用的</h5>
<p>
本例中,options指定的數(shù)組中的第一個(gè)元素含有disabled: true鍵值對(duì),因此是禁用的。在默認(rèn)情況下,Cascader
會(huì)檢查數(shù)據(jù)中每一項(xiàng)的disabled字段是否為true,
如果你的數(shù)據(jù)中表示禁用含義的字段名不為disabled,可以通過props.disabled屬性來指定(詳見下方 API
表格)。當(dāng)然,value、label和children這三個(gè)字段名也可以通過同樣的方式指定。
</p>
<el-cascader :props="props2"></el-cascader>
<el-cascader :options="options2"></el-cascader>
<h3>三、可清空</h3>
<h5>通過 clearable 設(shè)置輸入框可清空</h5>
<el-cascader :options="options" clearable></el-cascader>
<h3>四、僅顯示最后一級(jí)</h3>
<h5>可以僅在輸入框中顯示選中項(xiàng)最后一級(jí)的標(biāo)簽,而不是選中項(xiàng)所在的完整路徑。</h5>
<p>屬性show-all-levels定義了是否顯示完整的路徑,將其賦值為false則僅顯示最后一級(jí)</p>
<el-cascader :options="options" :show-all-levels="false"></el-cascader>
<h3>五、多選</h3>
<h5>可通過 props.multiple = true 來開啟多選模式</h5>
<p>在開啟多選模式后,默認(rèn)情況下會(huì)展示所有已選中的選項(xiàng)的Tag,你可以使用collapse-tags來折疊Tag</p>
<div class="blockParent">
<div class="block">
<span class="demonstration">默認(rèn)顯示所有Tag</span>
<el-cascader
:options="options3"
:props="props"
clearable></el-cascader>
</div>
<div class="block">
<span class="demonstration">折疊展示Tag</span>
<el-cascader
:options="options3"
:props="props"
collapse-tags
clearable></el-cascader>
</div>
</div>
<h3>六、選擇任意一級(jí)選項(xiàng)</h3>
<h5>
在單選模式下,你只能選擇葉子節(jié)點(diǎn);而在多選模式下,勾選父節(jié)點(diǎn)真正選中的都是葉子節(jié)點(diǎn)。啟用該功能后,可讓父子節(jié)點(diǎn)取消關(guān)聯(lián),選擇任意一級(jí)選項(xiàng)。</h5>
<p>可通過 props.checkStrictly = true 來設(shè)置父子節(jié)點(diǎn)取消選中關(guān)聯(lián),從而達(dá)到選擇任意一級(jí)選項(xiàng)的目的。</p>
<div class="blockParent">
<div class="block">
<span class="demonstration">單選選擇任意一級(jí)選項(xiàng)</span>
<el-cascader
:options="options"
:props="{ checkStrictly: true }"
clearable></el-cascader>
</div>
<div class="block">
<span class="demonstration">多選選擇任意一級(jí)選項(xiàng)</span>
<el-cascader
:options="options"
:props="{ multiple: true, checkStrictly: true }"
clearable></el-cascader>
</div>
</div>
<h3>七、動(dòng)態(tài)加載</h3>
<h5>當(dāng)選中某一級(jí)時(shí),動(dòng)態(tài)加載該級(jí)下的選項(xiàng)。</h5>
<p>
通過lazy開啟動(dòng)態(tài)加載,并通過lazyload來設(shè)置加載數(shù)據(jù)源的方法。lazyload方法有兩個(gè)參數(shù),第一個(gè)參數(shù)node為當(dāng)前點(diǎn)擊的節(jié)點(diǎn),第二個(gè)resolve為數(shù)據(jù)加載完成的回調(diào)(必須調(diào)用)。
為了更準(zhǔn)確的顯示節(jié)點(diǎn)的狀態(tài),還可以對(duì)節(jié)點(diǎn)數(shù)據(jù)添加是否為葉子節(jié)點(diǎn)的標(biāo)志位
(默認(rèn)字段為leaf,可通過props.leaf修改),否則會(huì)簡(jiǎn)單的以有無子節(jié)點(diǎn)來判斷是否為葉子節(jié)點(diǎn)。
</p>
<el-cascader :props="props2"></el-cascader>
<h3>八、可搜索</h3>
<h5>可以快捷地搜索選項(xiàng)并選擇。</h5>
<p>
將filterable賦值為true即可打開搜索功能,默認(rèn)會(huì)匹配節(jié)點(diǎn)的label或所有父節(jié)點(diǎn)的label(由show-all-levels決定)中包含輸入值的選項(xiàng)。
你也可以用filter-method自定義搜索邏輯,接受一個(gè)函數(shù),第一個(gè)參數(shù)是節(jié)點(diǎn)node,第二個(gè)參數(shù)是搜索關(guān)鍵詞keyword,通過返回布爾值表示是否命中。
</p>
<div class="blockParent">
<div class="block">
<span class="demonstration">單選可搜索</span>
<el-cascader
placeholder="試試搜索:指南"
:options="options"
filterable></el-cascader>
</div>
<div class="block">
<span class="demonstration">多選可搜索</span>
<el-cascader
placeholder="試試搜索:指南"
:options="options"
:props="{ multiple: true }"
filterable></el-cascader>
</div>
</div>
<h3>九、自定義節(jié)點(diǎn)內(nèi)容</h3>
<h5>可以自定義備選項(xiàng)的節(jié)點(diǎn)內(nèi)容</h5>
<p>
可以通過scoped slot對(duì)級(jí)聯(lián)選擇器的備選項(xiàng)的節(jié)點(diǎn)內(nèi)容進(jìn)行自定義,scoped slot會(huì)傳入兩個(gè)字段 node 和 data,分別表示當(dāng)前節(jié)點(diǎn)的
Node 對(duì)象和數(shù)據(jù)。
</p>
<el-cascader :options="options">
<template slot-scope="{ node, data }">
<span>{{ data.label }}</span>
<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
</template>
</el-cascader>
<h3>十、級(jí)聯(lián)面板</h3>
<h5>級(jí)聯(lián)面板是級(jí)聯(lián)選擇器的核心組件,與級(jí)聯(lián)選擇器一樣,有單選、多選、動(dòng)態(tài)加載等多種功能。</h5>
<p>和級(jí)聯(lián)選擇器一樣,通過options來指定選項(xiàng),也可通過props來設(shè)置多選、動(dòng)態(tài)加載等功能,具體詳情見下方API表格。</p>
<div style="width: 800px; border: solid #3ebcf1 1px;">
<el-cascader-panel :options="options"></el-cascader-panel>
</div>
</div>
</template>
<style>
.el_cascader_root {
margin-left: 300px;
margin-right: 300px;
text-align: left;
}
.blockParent {
width: 600px;
display: flex;
//border: 1px solid #409EFF; align-content: space-around;
}
.block {
width: 300px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.demonstration {
margin-bottom: 10px;
}
</style>
Cascader Attributes
Cascader Events
Cascader Methods
Cascader Slots
CascaderPanel Attributes
CascaderPanel Events
CascaderPanel Methods
CascaderPanel Slots
文章來源:http://www.zghlxwxcb.cn/news/detail-833302.html
Props
文章來源地址http://www.zghlxwxcb.cn/news/detail-833302.html
到了這里,關(guān)于ElementUI Form:Cascader 級(jí)聯(lián)選擇器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!