Vue Element前端應(yīng)用開(kāi)發(fā)之樹(shù)列表組件
1、常規(guī)樹(shù)列表控件的使用
眾所周知,一般界面很多情況涉及到樹(shù)列表的處理,如類(lèi)型展示,如果是一層的,可以用下拉列表代替,如果是多個(gè)層級(jí)的,采用樹(shù)控件展示會(huì)更加直觀(guān)。
在Element里面也有一個(gè)el-tree的控件,如下所示,這里主要對(duì)它的各種屬性和方法進(jìn)行介紹。

簡(jiǎn)單的代碼如下所示
<el-tree :data="data" @node-click="handleNodeClick"></el-tree>
主要在script部分里面指定它的data數(shù)據(jù),以及單擊節(jié)點(diǎn)的事件處理,結(jié)合卡片控件的展示,我們可以把樹(shù)放在其中進(jìn)行展示

界面代碼如下所示,通過(guò)default-expand-all 可以設(shè)置全部展開(kāi),icon-class 指定節(jié)點(diǎn)圖標(biāo)(也可以默認(rèn)不指定)
<el-card class="box-card">
<div slot="header" class="clearfix">
<span>樹(shù)列表</span>
<el-button style="float: right; padding: 3px 0" type="text">操作按鈕</el-button>
</div>
<div>
<el-tree
style="padding-top:10px"
:data="treedata"
node-key="id"
default-expand-all
icon-class="el-icon-price-tag"
highlight-current
@node-click="handleNodeClick"
>
<span slot-scope="{ node, data }" class="custom-tree-node">
<span>
<i :class="node.icon ? node.icon : 'el-icon-price-tag'" />
{{ node.label }}
</span>
</span>
</el-tree>
</div>
</el-card>
其中界面里面,我們通過(guò)class="custom-tree-node",來(lái)指定樹(shù)列表的展現(xiàn)內(nèi)容,可以加入圖標(biāo)等信息
而在script里面,定義了一個(gè)treedata的屬性
// 初始化樹(shù)列表
treedata: [
{
label: '一級(jí) 1',
id: '1',
children: [{
id: '1-1',
label: '二級(jí) 1-1',
children: [{
label: '三級(jí) 1-1-1',
id: '1-1-1'
}, {
label: '三級(jí) 1-1-2',
id: '1-1-2'
}, {
label: '三級(jí) 1-1-3',
id: '1-1-3'
}]
}]
}
]
如果設(shè)置有選擇框,得到界面如下所示。

主要設(shè)置show-checkbox 和@check-change="handleCheckChange" 即可。
界面代碼如下所示
<el-tree
style="padding-top:10px"
:data="treedata"
node-key="id"
default-expand-all
highlight-current
show-checkbox
:default-checked-keys="['1-1-1']"
@node-click="handleNodeClick"
@check-change="handleCheckChange"
>
<span slot-scope="{ node, data }" class="custom-tree-node">
<span>
<i :class="node.icon ? node.icon : 'el-icon-price-tag'" />
{{ node.label }}
</span>
</span>
</el-tree>
而對(duì)于樹(shù)列表,可以進(jìn)行一個(gè)過(guò)濾處理操作,如下界面所示。

在內(nèi)容區(qū)增加一個(gè)input的文本框進(jìn)行過(guò)濾處理,并綁定對(duì)應(yīng)的屬性變量
<el-input v-model="filterText" placeholder="輸入關(guān)鍵字進(jìn)行過(guò)濾" clearable prefix-icon="el-icon-search" />
樹(shù)列表控件需要增加過(guò)濾函數(shù)綁定:filter-node-method="filterNode",如下代碼所示。
<el-tree
ref="tree"
class="filter-tree"
style="padding-top:10px"
:data="treedata"
node-key="id"
default-expand-all
highlight-current
show-checkbox
:filter-node-method="filterNode"
@check-change="handleCheckChange"
@node-click="handleNodeClick"
>
<span slot-scope="{ node, data }" class="custom-tree-node">
<span>
<i :class="node.icon ? node.icon : 'el-icon-price-tag'" />
{{ node.label }}
</span>
</span>
</el-tree>
script的處理代碼如下所示,需要watch過(guò)濾的綁定值,變化就進(jìn)行過(guò)濾處理。

為了在列表結(jié)合中進(jìn)行快速的過(guò)濾,我們可以在上次介紹的列表界面里面增加一個(gè)樹(shù)列表的快速查詢(xún)處理。如下界面所示。

這里列表里面增加了一個(gè)第三方組件splitpanes,用來(lái)劃分區(qū)塊展示,而且可以拖動(dòng),非常不錯(cuò),地址是:
https://github.com/antoniandre/splitpanes
這個(gè)組件的Demo展示地址如下所示:https://antoniandre.github.io/splitpanes
效果大概如下所示

npm安裝如下所示
npm i --S splitpanes
安裝成功后,然后在vue文件的script部分里面引入即可
import { Splitpanes, Pane } from 'splitpanes'
import 'splitpanes/dist/splitpanes.css'
它的使用代碼也很簡(jiǎn)單
<splitpanes style="height: 400px">
<pane min-size="20">1</pane>
<pane>
<splitpanes horizontal>
<pane>2</pane>
<pane>3</pane>
<pane>4<pane>
</splitpanes>
</pane>
<pane>5</pane>
</splitpanes>
我的列表界面使用了兩個(gè)Panel即可實(shí)現(xiàn)左側(cè)樹(shù)的展示,和右側(cè)常規(guī)列表查詢(xún)的處理。

2、下拉框樹(shù)列表的處理
除了常規(guī)的樹(shù)列表展示內(nèi)容外,我們也需要一個(gè)在下拉列表中展示樹(shù)內(nèi)容的界面組件。
這里又得引入一個(gè)第三方的界面組件,因此Element的Select組件不支持樹(shù)列表。
GitHub地址:https://github.com/riophae/vue-treeselect
官網(wǎng)地址:https://vue-treeselect.js.org/
NPM安裝:
npm install --save @riophae/vue-treeselect
界面代碼如下所示。
<template>
<div id="app">
<treeselect v-model="value" :multiple="true" :options="options" />
</div>
</template>
這里的value就是選中的集合,options則是樹(shù)列表的節(jié)點(diǎn)數(shù)據(jù)。
<script>
// import the component
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
// register the component
components: { Treeselect },
data() {
return {
// define the default value
value: null,
// define options
options: [ {
id: 'a',
label: 'a',
children: [ {
id: 'aa',
label: 'aa',
}, {
id: 'ab',
label: 'ab',
} ],
}, {
id: 'b',
label: 'b',
}, {
id: 'c',
label: 'c',
} ],
}
},
}
</script>
我的測(cè)試界面代碼如下所示
<div style="height:180px">
<!--
v-model 綁定選中的集合
options 樹(shù)節(jié)點(diǎn)數(shù)據(jù)
defaultExpandLevel 展開(kāi)層次,Infinity為所有
flat 為子節(jié)點(diǎn)不影響父節(jié)點(diǎn),不關(guān)聯(lián)
-->
<treeselect
v-model="value"
:options="treedata"
:multiple="true"
:flat="true"
:default-expand-level="Infinity"
:open-on-click="true"
:open-on-focus="true"
clearable
:max-height="200"
/>
</div>
<script>
// import vue-treeselect component
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
name: 'Tree',
components: { Treeselect },
data() {
return {
// 過(guò)濾條件
filterText: '',
// 初始化樹(shù)列表
treedata: [
{
label: '一級(jí) 1',
id: '1',
children: [{
id: '1-1',
label: '二級(jí) 1-1',
children: [{
label: '三級(jí) 1-1-1',
id: '1-1-1'
}, {
label: '三級(jí) 1-1-2',
id: '1-1-2'
}, {
label: '三級(jí) 1-1-3',
id: '1-1-3'
}]
}]
}
],
value: ['1-1-2']
}
},
................
}
</script>
來(lái)一張幾個(gè)樹(shù)列表一起的對(duì)比展示界面。

以上就是普通樹(shù)列表和下拉列表樹(shù)展示的界面效果,往往我們一些特殊的界面處理,就需要多利用一些封裝良好的第三方界面組件實(shí)現(xiàn),可以豐富我們的界面展示效果
以上就是Vue Element前端應(yīng)用開(kāi)發(fā)之樹(shù)列表組件的詳細(xì)內(nèi)容,更多關(guān)于Vue Element樹(shù)列表組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Vue+elementUI實(shí)現(xiàn)動(dòng)態(tài)展示列表的數(shù)據(jù)
- 利用vue+elementUI設(shè)置省市縣三級(jí)聯(lián)動(dòng)下拉列表框的詳細(xì)過(guò)程
- Vue中使用elementui與Sortable.js實(shí)現(xiàn)列表拖動(dòng)排序
- Vue組件庫(kù)ElementUI實(shí)現(xiàn)表格列表分頁(yè)效果
- Vue Element UI自定義描述列表組件
- Vue Element前端應(yīng)用開(kāi)發(fā)之表格列表展示
- Vue element商品列表的增刪改功能實(shí)現(xiàn)
相關(guān)文章
Vue中 key keep-alive的實(shí)現(xiàn)原理
這篇文章主要介紹了Vue中 key keep-alive的實(shí)現(xiàn)原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue向后端傳數(shù)據(jù)后端接收為null問(wèn)題及解決
這篇文章主要介紹了Vue向后端傳數(shù)據(jù)后端接收為null問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
vue實(shí)現(xiàn)簡(jiǎn)易的雙向數(shù)據(jù)綁定
這篇文章主要介紹了vue如何實(shí)現(xiàn)簡(jiǎn)易的雙向數(shù)據(jù)綁定,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
vuedraggable+element ui實(shí)現(xiàn)頁(yè)面控件拖拽排序效果
這篇文章主要為大家詳細(xì)介紹了vuedraggable+element ui實(shí)現(xiàn)頁(yè)面控件拖拽排序效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
vue中radio根據(jù)動(dòng)態(tài)值綁定checked無(wú)效的解決
這篇文章主要介紹了vue中radio根據(jù)動(dòng)態(tài)值綁定checked無(wú)效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Ant design vue table 單擊行選中 勾選checkbox教程
這篇文章主要介紹了Ant design vue table 單擊行選中 勾選checkbox教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
整理項(xiàng)目中vue.config.js打包優(yōu)化配置方法
這篇文章主要介紹了整理項(xiàng)目中vue.config.js打包優(yōu)化,包括配置?webpack-bundle-analyzer?插件查看文件大小及配置compression-webpack-plugin?用gzip壓縮打包的文件大小,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
使用VitePress搭建及部署vue組件庫(kù)文檔的示例詳解
這篇文章主要介紹了使用VitePress搭建及部署vue組件庫(kù)文檔,本文以element-plus作為示例來(lái)搭建一個(gè)文檔,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
用Vue.js方法創(chuàng)建模板并使用多個(gè)模板合成
在本篇文章中小編給大家分享了關(guān)于如何使用Vue.js方法創(chuàng)建模板并使用多個(gè)模板合成的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-06-06

