vue2.0基于vue-cli+element-ui制作树形treeTable
- 作者: 聊天请叫我妈丶
- 来源: 51数据库
- 2021-08-23
该组件基于技术栈,主要涉及vue-cli生成的webpack项目脚手架,在此脚手架项目基础上完成的,整合了element-ui开源vue的ui项目
1.vue-cli的安装使用
npm install -g vue-cli
全局安装vue-cli之后,使用该脚手架的相关命令,可快速生成一个比较规范的vue项目结构
vue init <template-name> <project-name>
例子
vue init webpack treetable
这样一个快速的项目结构生成,如下所示,中间一路回车就可以了,主要是设置了是否支持eslint等等

2.整合element-ui
cd treetable
进入刚刚生成的项目目录中,安装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更多的使用和操作,可以去官网查看 http://element.eleme.io/#/zh-cn/component/quickstart
我这里主要是利用他的table组件来制作一个树形结构的table。

3.树形table组件制作
在src目录的components目录中,

其中utils下面提供一些需要用的工具类
vue目录下面是组件的源码
index.js是外包入口
相关代码
datatranslate.js主要是提供了把数组数据转换成树形结构的数据,并且进行相关属性的添加
/*
* @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">
添加下级树结构
</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: {
// 该属性是确认父组件传过来的数据是否已经是树形结构了,如果是,则不需要进行树形格式化
treestructure: {
type: boolean,
default: function () {
return false
}
},
// 这是相应的字段展示
columns: {
type: array,
default: function () {
return []
}
},
// 这是数据源
datasource: {
type: array,
default: function () {
return []
}
},
// 这个作用是根据自己需求来的,比如在操作中涉及相关按钮编辑,删除等,需要向服务端发送请求,则可以把url传过来
requesturl: {
type: string,
default: function () {
return ''
}
},
// 这个是是否展示操作列
treetype: {
type: string,
default: function () {
return 'normal'
}
},
// 是否默认展开所有树
defaultexpandall: {
type: boolean,
default: function () {
return false
}
}
},
data () {
return {}
},
computed: {
// 格式化数据源
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
},
// 显示层级关系的空格和图标
spaceiconshow (index) {
let me = this
if (me.treestructure && index === 0) {
return true
}
return false
},
// 点击展开和关闭的时候,图标的切换
toggleiconshow (index, record) {
let me = this
if (me.treestructure && index === 0 && record.children && record.children.length > 0) {
return true
}
return false
},
handledelete () {
this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
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>
效果图

http://www.51sjk.com/Upload/Articles/1/0/287/287285_20210712001621907.jpg github上下载源码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
