vue2.0基于vue-cli+element-ui制作樹形treeTable
該組件基于技術(shù)棧,主要涉及vue-cli生成的webpack項目腳手架,在此腳手架項目基礎(chǔ)上完成的,整合了element-ui開源vue的UI項目
1.vue-cli的安裝使用
npm install -g vue-cli
全局安裝vue-cli之后,使用該腳手架的相關(guān)命令,可快速生成一個比較規(guī)范的vue項目結(jié)構(gòu)
vue init <template-name> <project-name>
例子
vue init webpack treeTable
這樣一個快速的項目結(jié)構(gòu)生成,如下所示,中間一路回車就可以了,主要是設(shè)置了是否支持eslint等等

2.整合element-ui
cd treeTable
進(jìn)入剛剛生成的項目目錄中,安裝element-ui
npm i element-ui -S
在main.js中,
import ElementUI from 'element-ui' import 'element-ui/lib/theme-default/index.css' Vue.use(ElementUI)
整合就可以了,具體element-ui更多的使用和操作,可以去官網(wǎng)查看 http://element.eleme.io/#/zh-CN/component/quickstart
我這里主要是利用他的table組件來制作一個樹形結(jié)構(gòu)的table。

3.樹形table組件制作
在src目錄的components目錄中,

其中utils下面提供一些需要用的工具類
vue目錄下面是組件的源碼
index.js是外包入口
相關(guān)代碼
dataTranslate.js主要是提供了把數(shù)組數(shù)據(jù)轉(zhuǎn)換成樹形結(jié)構(gòu)的數(shù)據(jù),并且進(jìn)行相關(guān)屬性的添加
/*
* @Author: sunlandong
* @Date: 2017-03-11 12:06:49
* @Last Modified by: sunlandong
* @Last Modified time: 2017-03-11 16:30:03
*/
import Vue from 'vue'
function DataTransfer (data) {
if (!(this instanceof DataTransfer)) {
return new DataTransfer(data, null, null)
}
}
DataTransfer.treeToArray = function (data, parent, level, expandedAll) {
let tmp = []
Array.from(data).forEach(function (record) {
if (record._expanded === undefined) {
Vue.set(record, '_expanded', expandedAll)
}
if (parent) {
Vue.set(record, '_parent', parent)
}
let _level = 0
if (level !== undefined && level !== null) {
_level = level + 1
}
Vue.set(record, '_level', _level)
tmp.push(record)
if (record.children && record.children.length > 0) {
let children = DataTransfer.treeToArray(record.children, record, _level, expandedAll)
tmp = tmp.concat(children)
}
})
return tmp
}
export default DataTransfer
utils/index.js
/*
* @Author: sunlandong
* @Date: 2017-03-11 12:06:55
* @Last Modified by: sunlandong
* @Last Modified time: 2017-03-11 16:36:56
*/
import MSDataTransfer from './dataTranslate.js'
export default {
MSDataTransfer
}
TreeGrid.vue是樹形table組件的源碼
<template>
<el-table
:data="data"
border
style="width: 100%"
:row-style="showTr">
<el-table-column v-for="(column, index) in columns" :key="column.dataIndex"
:label="column.text">
<template scope="scope">
<span v-if="spaceIconShow(index)" v-for="(space, levelIndex) in scope.row._level" class="ms-tree-space"></span>
<button class="button is-outlined is-primary is-small" v-if="toggleIconShow(index,scope.row)" @click="toggle(scope.$index)">
<i v-if="!scope.row._expanded" class="el-icon-caret-right" aria-hidden="true"></i>
<i v-if="scope.row._expanded" class="el-icon-caret-bottom" aria-hidden="true"></i>
</button>
<span v-else-if="index===0" class="ms-tree-space"></span>
{{scope.row[column.dataIndex]}}
</template>
</el-table-column>
<el-table-column label="操作" v-if="treeType === 'normal'" width="260">
<template scope="scope">
<button type="button" class="el-button el-button--default el-button--small">
<router-link
:to="{ path: requestUrl + 'edit', query: {id: scope.row.Oid} }"
tag="span">
編輯
</router-link>
</button>
<el-button
size="small"
type="danger"
@click="handleDelete()">
刪除
</el-button>
<button type="button" class="el-button el-button--success el-button--small">
<router-link :to="{ path: requestUrl, query: {parentId: scope.row.parentOId} }"
tag="span">
添加下級樹結(jié)構(gòu)
</router-link>
</button>
</template>
</el-table-column>
</el-table>
</template>
<script>
import Utils from '../utils/index.js'
// import Vue from 'vue'
export default {
name: 'tree-grid',
props: {
// 該屬性是確認(rèn)父組件傳過來的數(shù)據(jù)是否已經(jīng)是樹形結(jié)構(gòu)了,如果是,則不需要進(jìn)行樹形格式化
treeStructure: {
type: Boolean,
default: function () {
return false
}
},
// 這是相應(yīng)的字段展示
columns: {
type: Array,
default: function () {
return []
}
},
// 這是數(shù)據(jù)源
dataSource: {
type: Array,
default: function () {
return []
}
},
// 這個作用是根據(jù)自己需求來的,比如在操作中涉及相關(guān)按鈕編輯,刪除等,需要向服務(wù)端發(fā)送請求,則可以把url傳過來
requestUrl: {
type: String,
default: function () {
return ''
}
},
// 這個是是否展示操作列
treeType: {
type: String,
default: function () {
return 'normal'
}
},
// 是否默認(rèn)展開所有樹
defaultExpandAll: {
type: Boolean,
default: function () {
return false
}
}
},
data () {
return {}
},
computed: {
// 格式化數(shù)據(jù)源
data: function () {
let me = this
if (me.treeStructure) {
let data = Utils.MSDataTransfer.treeToArray(me.dataSource, null, null, me.defaultExpandAll)
console.log(data)
return data
}
return me.dataSource
}
},
methods: {
// 顯示行
showTr: function (row, index) {
let show = (row._parent ? (row._parent._expanded && row._parent._show) : true)
row._show = show
return show ? '' : 'display:none;'
},
// 展開下級
toggle: function (trIndex) {
let me = this
let record = me.data[trIndex]
record._expanded = !record._expanded
},
// 顯示層級關(guān)系的空格和圖標(biāo)
spaceIconShow (index) {
let me = this
if (me.treeStructure && index === 0) {
return true
}
return false
},
// 點擊展開和關(guān)閉的時候,圖標(biāo)的切換
toggleIconShow (index, record) {
let me = this
if (me.treeStructure && index === 0 && record.children && record.children.length > 0) {
return true
}
return false
},
handleDelete () {
this.$confirm('此操作將永久刪除該記錄, 是否繼續(xù)?', '提示', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'error'
}).then(() => {
this.$message({
type: 'success',
message: '刪除成功!'
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消刪除'
})
})
}
}
}
</script>
<style scoped>
.ms-tree-space{position: relative;
top: 1px;
display: inline-block;
font-family: 'Glyphicons Halflings';
font-style: normal;
font-weight: 400;
line-height: 1;
width: 18px;
height: 14px;}
.ms-tree-space::before{content: ""}
table td{
line-height: 26px;
}
</style>
index.js
import TreeGrid from './vue/TreeGrid.vue'
module.exports = {
TreeGrid
}
使用
<template>
<div class="hello">
<tree-grid :columns="columns" :tree-structure="true" :data-source="dataSource"></tree-grid>
</div>
</template>
<script>
import {TreeGrid} from './treeTable'
export default {
name: 'hello',
data () {
return {
columns: [
{
text: '姓名',
dataIndex: 'name'
},
{
text: '年齡',
dataIndex: 'age'
},
{
text: '性別',
dataIndex: 'sex'
}
],
dataSource: [
{
id: 1,
parentId: 0,
name: '測試1',
age: 18,
sex: '男',
children: [
{
id: 2,
parentId: 1,
name: '測試2',
age: 22,
sex: '男'
}
]
},
{
id: 3,
parentId: 0,
name: '測試3',
age: 23,
sex: '女',
children: [
{
id: 4,
parentId: 3,
name: '測試4',
age: 22,
sex: '男'
},
{
id: 5,
parentId: 3,
name: '測試5',
age: 25,
sex: '男'
},
{
id: 6,
parentId: 3,
name: '測試6',
age: 26,
sex: '女',
children: [
{
id: 7,
parentId: 6,
name: '測試7',
age: 27,
sex: '男'
}
]
}
]
},
{
id: 18,
parentId: 0,
name: '測試8',
age: 18,
sex: '男'
}
]
}
},
components: {
TreeGrid
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
效果圖

https://github.com/sunlandong/treeTable github上下載源碼
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Vue3實現(xiàn)鼠標(biāo)滑動和滾輪控制的輪播
在這篇文章主要為大家詳細(xì)介紹了如何一步步地實現(xiàn)一個基于?Vue?3?的輪播組件,這個組件的特點是可以通過鼠標(biāo)滑動和滾輪來控制輪播圖的切換,感興趣的可以了解下2024-02-02
在Vant的基礎(chǔ)上實現(xiàn)添加表單驗證框架的方法示例
這篇文章主要介紹了在Vant的基礎(chǔ)上實現(xiàn)添加驗證框架的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Vue3中的element-plus表格實現(xiàn)代碼
這篇文章主要介紹了Vue3中的element-plus表格實現(xiàn)代碼,用組件屬性實現(xiàn)跳轉(zhuǎn)路由,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
Vue?+?Element?實現(xiàn)按鈕指定間隔時間點擊思路詳解
這篇文章主要介紹了Vue?+?Element?實現(xiàn)按鈕指定間隔時間點擊,實現(xiàn)思路大概是通過加一個本地緩存的時間戳,通過時間戳計算指定時間內(nèi)不能點擊按鈕,具體實現(xiàn)代碼跟隨小編一起看看吧2023-12-12
vue.js實現(xiàn)含搜索的多種復(fù)選框(附源碼)
這篇文章主要給大家介紹了利用vue.js實現(xiàn)含搜索的多種復(fù)選框的相關(guān)資料,文中給出了簡單的介紹,但提供了完整的實例源碼供大家下載學(xué)習(xí),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
Vue.js 2.0 移動端拍照壓縮圖片上傳預(yù)覽功能
這篇文章主要介紹了Vue.js 2.0 移動端拍照壓縮圖片上傳預(yù)覽功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03

