parent
700e08b795
commit
00f4714b43
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* @Author: jianglei
|
||||
* @Date: 2017-10-12 12:06:49
|
||||
*/
|
||||
'use strict'
|
||||
import Vue from 'vue'
|
||||
export default function treeToArray(data, expandedAll, parent, level) {
|
||||
let tmp = []
|
||||
Array.from(data).forEach(function(record) {
|
||||
if (record._expanded === undefined) {
|
||||
Vue.set(record, '_expanded', expandedAll)
|
||||
}
|
||||
let _level = 1
|
||||
if (level !== undefined && level !== null) {
|
||||
_level = level + 1
|
||||
}
|
||||
Vue.set(record, '_level', _level)
|
||||
// 如果有父元素
|
||||
if (parent) {
|
||||
Vue.set(record, 'parent', parent)
|
||||
}
|
||||
tmp.push(record)
|
||||
if (record.children && record.children.length > 0) {
|
||||
const children = treeToArray(record.children, expandedAll, record, _level)
|
||||
tmp = tmp.concat(children)
|
||||
}
|
||||
})
|
||||
return tmp
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
<template>
|
||||
<el-table :data="formatData"
|
||||
:row-style="showRow">
|
||||
<el-table-column v-for="(column, index) in columns" :key="column.value"
|
||||
:label="column.text" :width="column.width">
|
||||
<template slot-scope="scope">
|
||||
<span v-if="index === 0" v-for="space in scope.row._level"
|
||||
class="ms-tree-space"></span>
|
||||
<span class="tree-ctrl" v-if="toggleIconShow(index,scope.row)"
|
||||
@click="toggleExpanded(scope.$index)">
|
||||
<i v-if="!scope.row._expanded" class="el-icon-plus"></i>
|
||||
<i v-else class="el-icon-minus"></i>
|
||||
</span>
|
||||
{{scope.row[column.value]}}
|
||||
</template>
|
||||
</el-table-column>
|
||||
<slot name="extend"></slot>
|
||||
</el-table>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
/**
|
||||
* 传进来的数据格式
|
||||
* columns:{
|
||||
* [value:'',text:'']
|
||||
* }
|
||||
/**
|
||||
Auth: Lei.j1ang
|
||||
Created: 2018/1/19-13:59
|
||||
*/
|
||||
import treeToArray from './eval'
|
||||
export default {
|
||||
name: 'tree-table',
|
||||
props: {
|
||||
data: [Object, Array],
|
||||
columns: {
|
||||
type: Array,
|
||||
required: true
|
||||
},
|
||||
evalFunc: Function,
|
||||
evalArgs: Array,
|
||||
expandAll: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
// 格式化数据源
|
||||
formatData: function() {
|
||||
const func = this.evalFunc || treeToArray
|
||||
const args = this.evalArgs ? Array.concat([this.data], this.evalArgs) : [this.data, this.expandAll]
|
||||
console.log(args)
|
||||
return func.apply(null, args)
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
showRow: function(row) {
|
||||
const show = (row.row.parent ? (row.row.parent._expanded && row.row.parent._show) : true)
|
||||
row.row._show = show
|
||||
return show ? '' : 'display:none;'
|
||||
},
|
||||
// 切换下级是否展开
|
||||
toggleExpanded: function(trIndex) {
|
||||
const record = this.formatData[trIndex]
|
||||
record._expanded = !record._expanded
|
||||
},
|
||||
// 图标显示
|
||||
toggleIconShow(index, record) {
|
||||
return (index === 0 && record.children && record.children.length > 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" rel="stylesheet/scss" scoped>
|
||||
$color-blue: #2196F3;
|
||||
$space-width: 18px;
|
||||
.ms-tree-space {
|
||||
position: relative;
|
||||
top: 1px;
|
||||
display: inline-block;
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
line-height: 1;
|
||||
width: $space-width;
|
||||
height: 14px;
|
||||
&::before {
|
||||
content: ""
|
||||
}
|
||||
}
|
||||
.processContainer{
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
table td {
|
||||
line-height: 26px;
|
||||
}
|
||||
|
||||
.tree-ctrl{
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
color: $color-blue;
|
||||
margin-left: -$space-width;
|
||||
}
|
||||
</style>
|
|
@ -28,6 +28,8 @@ export default {
|
|||
dragTable: 'Drag Table',
|
||||
inlineEditTable: 'Inline Edit',
|
||||
complexTable: 'Complex Table',
|
||||
treeTable: 'treeTable',
|
||||
customTreeTable: 'Custom TreeTable',
|
||||
tab: 'Tab',
|
||||
form: 'Form',
|
||||
createForm: 'Create Form',
|
||||
|
|
|
@ -28,6 +28,8 @@ export default {
|
|||
dragTable: '拖拽table',
|
||||
inlineEditTable: 'table内编辑',
|
||||
complexTable: '综合table',
|
||||
treeTable: '树表',
|
||||
customTreeTable: '自定义树表',
|
||||
tab: 'Tab',
|
||||
form: '表单',
|
||||
createForm: '创建表单',
|
||||
|
|
|
@ -151,7 +151,9 @@ export const asyncRouterMap = [
|
|||
{ path: 'dynamic-table', component: _import('example/table/dynamicTable/index'), name: 'dynamicTable', meta: { title: 'dynamicTable' }},
|
||||
{ path: 'drag-table', component: _import('example/table/dragTable'), name: 'dragTable', meta: { title: 'dragTable' }},
|
||||
{ path: 'inline-edit-table', component: _import('example/table/inlineEditTable'), name: 'inlineEditTable', meta: { title: 'inlineEditTable' }},
|
||||
{ path: 'complex-table', component: _import('example/table/complexTable'), name: 'complexTable', meta: { title: 'complexTable' }}
|
||||
{ path: 'complex-table', component: _import('example/table/complexTable'), name: 'complexTable', meta: { title: 'complexTable' }},
|
||||
{ path: 'treeTable', component: _import('example/table/treeTable/treeTable'), name: 'treeTable', meta: { title: 'treeTable' }},
|
||||
{ path: 'customTreeTable', component: _import('example/table/treeTable/customTreeTable'), name: 'customTreeTable', meta: { title: 'customTreeTable' }}
|
||||
]
|
||||
},
|
||||
{ path: 'tab/index', icon: 'tab', component: _import('example/tab/index'), name: 'tab', meta: { title: 'tab' }}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
* @Author: jianglei
|
||||
* @Date: 2017-10-12 12:06:49
|
||||
*/
|
||||
'use strict'
|
||||
import Vue from 'vue'
|
||||
const marLTemp = {}
|
||||
export default function treeToArray(data, parent, level, expandedAll, item) {
|
||||
let tmp = []
|
||||
let rootId
|
||||
Array.from(data).forEach(function(record) {
|
||||
if (record.children) {
|
||||
rootId = record.id
|
||||
}
|
||||
if (record._expanded === undefined) {
|
||||
Vue.set(record, '_expanded', expandedAll)
|
||||
}
|
||||
let _level = 1
|
||||
if (level !== undefined && level !== null) {
|
||||
_level = level + 1
|
||||
}
|
||||
Vue.set(record, '_level', _level)
|
||||
// 如果有父元素
|
||||
if (parent) {
|
||||
Vue.set(record, 'parent', parent)
|
||||
// 如果父元素有偏移量,需要计算在this的偏移量中
|
||||
// 偏移量还与前面同级元素有关,需要加上前面所有元素的长度和
|
||||
if (!marLTemp[rootId]) {
|
||||
marLTemp[rootId] = []
|
||||
}
|
||||
if (!marLTemp[rootId][_level]) {
|
||||
marLTemp[rootId][_level] = 0
|
||||
}
|
||||
Vue.set(record, '_marginLeft', marLTemp[rootId][_level] + parent._marginLeft)
|
||||
Vue.set(record, '_width', record[item] / parent[item] * parent._width)
|
||||
// 在本次计算过偏移量后加上自己长度,以供下一个元素使用
|
||||
marLTemp[rootId][_level] += record._width
|
||||
} else {
|
||||
// 如果为根
|
||||
// 初始化偏移量存储map
|
||||
marLTemp[record.id] = []
|
||||
// map中是一个数组,存储的是每级的长度和
|
||||
// 初始情况下为0
|
||||
marLTemp[record.id][_level] = 0
|
||||
Vue.set(record, '_marginLeft', 0)
|
||||
Vue.set(record, '_width', 1)
|
||||
}
|
||||
tmp.push(record)
|
||||
if (record.children && record.children.length > 0) {
|
||||
const children = treeToArray(record.children, record, _level, expandedAll, item)
|
||||
tmp = tmp.concat(children)
|
||||
}
|
||||
})
|
||||
return tmp
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
<template>
|
||||
<tree-table :data="data" :columns="columns" :evalFunc="func" :evalArgs="args">
|
||||
<template slot="extend">
|
||||
<el-table-column label="时间线" width="500" >
|
||||
<template slot-scope="scope">
|
||||
<el-tooltip effect="dark" :content="scope.row.timeLine+'ms'" placement="left">
|
||||
<div class="processContainer">
|
||||
<div class="process" :style= "{ width:scope.row._width * 500+'px',
|
||||
background:scope.row._width>0.5?'rgba(233,0,0,.5)':'rgba(0,0,233,0.5)',
|
||||
marginLeft:scope.row._marginLeft * 500+'px' }">
|
||||
<span style="display:inline-block"></span>
|
||||
</div>
|
||||
</div>
|
||||
</el-tooltip>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="操作" width="200">
|
||||
<template slot-scope="scope">
|
||||
<el-button type="text" @click="message(scope.row)">点击</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</template>
|
||||
</tree-table>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
/**
|
||||
Auth: Lei.j1ang
|
||||
Created: 2018/1/19-14:54
|
||||
*/
|
||||
import treeTable from '@/components/TreeTable'
|
||||
import treeToArray from './customEval'
|
||||
export default {
|
||||
name: 'tree',
|
||||
components: { treeTable },
|
||||
data() {
|
||||
return {
|
||||
func: treeToArray,
|
||||
columns: [
|
||||
{
|
||||
text: '事件',
|
||||
value: 'event'
|
||||
},
|
||||
{
|
||||
text: 'ID',
|
||||
value: 'id'
|
||||
},
|
||||
{
|
||||
text: '时间线',
|
||||
value: 'timeLine'
|
||||
},
|
||||
{
|
||||
text: '备注',
|
||||
value: 'comment'
|
||||
}
|
||||
],
|
||||
data: [
|
||||
{
|
||||
id: 0,
|
||||
event: '事件0',
|
||||
timeLine: 50,
|
||||
comment: '无'
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
event: '事件1',
|
||||
timeLine: 100,
|
||||
comment: '无',
|
||||
children: [
|
||||
{
|
||||
id: 2,
|
||||
event: '事件2',
|
||||
timeLine: 10,
|
||||
comment: '无'
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
event: '事件3',
|
||||
timeLine: 90,
|
||||
comment: '无',
|
||||
children: [
|
||||
{
|
||||
id: 4,
|
||||
event: '事件4',
|
||||
timeLine: 5,
|
||||
comment: '无'
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
event: '事件5',
|
||||
timeLine: 10,
|
||||
comment: '无'
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
event: '事件6',
|
||||
timeLine: 75,
|
||||
comment: '无',
|
||||
children: [
|
||||
{
|
||||
id: 7,
|
||||
event: '事件7',
|
||||
timeLine: 50,
|
||||
comment: '无',
|
||||
children: [
|
||||
{
|
||||
id: 71,
|
||||
event: '事件71',
|
||||
timeLine: 25,
|
||||
comment: 'xx'
|
||||
},
|
||||
{
|
||||
id: 72,
|
||||
event: '事件72',
|
||||
timeLine: 5,
|
||||
comment: 'xx'
|
||||
},
|
||||
{
|
||||
id: 73,
|
||||
event: '事件73',
|
||||
timeLine: 20,
|
||||
comment: 'xx'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
event: '事件8',
|
||||
timeLine: 25,
|
||||
comment: '无'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
args: [null, null, true, 'timeLine']
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
message(row) {
|
||||
console.log(row)
|
||||
this.$message.info(row.event)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
|
@ -0,0 +1,121 @@
|
|||
<template>
|
||||
<tree-table :data="data" :columns="columns">
|
||||
</tree-table>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
/**
|
||||
Auth: Lei.j1ang
|
||||
Created: 2018/1/19-14:54
|
||||
*/
|
||||
import treeTable from '@/components/TreeTable'
|
||||
export default {
|
||||
name: 'tree',
|
||||
components: { treeTable },
|
||||
data() {
|
||||
return {
|
||||
columns: [
|
||||
{
|
||||
text: '事件',
|
||||
value: 'event',
|
||||
width: 200
|
||||
},
|
||||
{
|
||||
text: 'ID',
|
||||
value: 'id'
|
||||
},
|
||||
{
|
||||
text: '时间线',
|
||||
value: 'timeLine'
|
||||
},
|
||||
{
|
||||
text: '备注',
|
||||
value: 'comment'
|
||||
}
|
||||
],
|
||||
data: [
|
||||
{
|
||||
id: 0,
|
||||
event: '事件1',
|
||||
timeLine: 50,
|
||||
comment: '无'
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
event: '事件1',
|
||||
timeLine: 100,
|
||||
comment: '无',
|
||||
children: [
|
||||
{
|
||||
id: 2,
|
||||
event: '事件2',
|
||||
timeLine: 10,
|
||||
comment: '无'
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
event: '事件3',
|
||||
timeLine: 90,
|
||||
comment: '无',
|
||||
children: [
|
||||
{
|
||||
id: 4,
|
||||
event: '事件4',
|
||||
timeLine: 5,
|
||||
comment: '无'
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
event: '事件5',
|
||||
timeLine: 10,
|
||||
comment: '无'
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
event: '事件6',
|
||||
timeLine: 75,
|
||||
comment: '无',
|
||||
children: [
|
||||
{
|
||||
id: 7,
|
||||
event: '事件7',
|
||||
timeLine: 50,
|
||||
comment: '无',
|
||||
children: [
|
||||
{
|
||||
id: 71,
|
||||
event: '事件71',
|
||||
timeLine: 25,
|
||||
comment: 'xx'
|
||||
},
|
||||
{
|
||||
id: 72,
|
||||
event: '事件72',
|
||||
timeLine: 5,
|
||||
comment: 'xx'
|
||||
},
|
||||
{
|
||||
id: 73,
|
||||
event: '事件73',
|
||||
timeLine: 20,
|
||||
comment: 'xx'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
event: '事件8',
|
||||
timeLine: 25,
|
||||
comment: '无'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
Loading…
Reference in New Issue