refine tree table

This commit is contained in:
liugq 2019-02-11 16:39:40 +08:00
parent 0b6e7515ce
commit a42f471208
4 changed files with 184 additions and 179 deletions

View File

@ -1,27 +1,23 @@
/**
* @Author: jianglei
* @Date: 2017-10-12 12:06:49
*/
'use strict'
import Vue from 'vue' import Vue from 'vue'
export default function treeToArray(data, expandAll, parent = null, level = null) {
// 给数据添加额外的几个属性,并且扁平化数组
export default function formatData(
data,
{ parent = null, leavel = 0, expand = false } = {}
) {
console.log('data', data)
let tmp = [] let tmp = []
Array.from(data).forEach(function(record) { data.forEach(item => {
if (record._expanded === undefined) { Vue.set(item, '__leavel', leavel)
Vue.set(record, '_expanded', expandAll) Vue.set(item, '__expand', expand)
} Vue.set(item, '__parent', parent)
let _level = 1
if (level !== undefined && level !== null) { tmp.push(item)
_level = level + 1 if (item.children && item.children.length > 0) {
} const children = formatData(item.children, {
Vue.set(record, '_level', _level) parent: item,
// 如果有父元素 leavel: leavel + 1
if (parent) { })
Vue.set(record, 'parent', parent)
}
tmp.push(record)
if (record.children && record.children.length > 0) {
const children = treeToArray(record.children, expandAll, record, _level)
tmp = tmp.concat(children) tmp = tmp.concat(children)
} }
}) })

View File

@ -1,41 +1,37 @@
<template> <template>
<el-table :data="formatData" :row-style="showRow" v-bind="$attrs"> <el-table :data="res" :row-style="isShowRow" border style="width: 100%">
<el-table-column v-if="columns.length===0" width="150"> <el-table-column
v-for="(item,idx) in columns"
:label="item.label"
:key="item.key"
:width="item.width"
align="center"
>
<template slot-scope="scope"> <template slot-scope="scope">
<span v-for="space in scope.row._level" :key="space" class="ms-tree-space"/> <span
<span v-if="iconShow(0,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)"> v-if="isNotLastChild(idx,scope.row)"
<i v-if="!scope.row._expanded" class="el-icon-plus"/> class="tree-ctrl"
<i v-else class="el-icon-minus"/> @click="toggleExpanded(scope.$index)"
>
<i
v-if="!scope.row.__expand"
:style="{'padding-left':+scope.row.__leavel*50 + 'px'} "
class="el-icon-plus"
/>
<i v-else :style="{'padding-left':+scope.row.__leavel*50 + 'px'} " class="el-icon-minus"/>
</span> </span>
{{ scope.$index }} <slot :scope="scope" :name="item.key">{{ scope.row[item.key] }}</slot>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column v-for="(column, index) in columns" v-else :key="column.value" :label="column.text" :width="column.width">
<template slot-scope="scope">
<!-- Todo -->
<!-- eslint-disable-next-line vue/no-confusing-v-for-v-if -->
<span v-for="space in scope.row._level" v-if="index === 0" :key="space" class="ms-tree-space"/>
<span v-if="iconShow(index,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)">
<i v-if="!scope.row._expanded" class="el-icon-plus"/>
<i v-else class="el-icon-minus"/>
</span>
{{ scope.row[column.value] }}
</template>
</el-table-column>
<slot/>
</el-table> </el-table>
</template> </template>
<script> <script>
/** import treeToArray from './eval.js'
Auth: Lei.j1ang
Created: 2018/1/19-13:59
*/
import treeToArray from './eval'
export default { export default {
name: 'TreeTable', name: 'TreeTable',
props: { props: {
/* eslint-disable */
data: { data: {
type: [Array, Object], type: [Array, Object],
required: true required: true
@ -44,16 +40,16 @@ export default {
type: Array, type: Array,
default: () => [] default: () => []
}, },
evalFunc: Function, /* eslint-disable */
evalArgs: Array, evalFunc: {
expandAll: { type: Function
type: Boolean, },
default: false evalArgs: Array
} /* eslint-enable */
}, },
computed: { computed: {
// //
formatData: function() { res() {
let tmp let tmp
if (!Array.isArray(this.data)) { if (!Array.isArray(this.data)) {
tmp = [this.data] tmp = [this.data]
@ -61,67 +57,77 @@ export default {
tmp = this.data tmp = this.data
} }
const func = this.evalFunc || treeToArray const func = this.evalFunc || treeToArray
const args = this.evalArgs ? Array.concat([tmp, this.expandAll], this.evalArgs) : [tmp, this.expandAll] const args = { ...this.evalArgs }
return func.apply(null, args) return func(tmp, args)
} }
}, },
methods: { methods: {
showRow: function(row) { isShowRow: function(row) {
const show = (row.row.parent ? (row.row.parent._expanded && row.row.parent._show) : true) const show = row.row.__parent ? row.row.__parent.__expand : true
row.row._show = show return show
return show ? 'animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;' : 'display:none;' ? 'animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;'
: 'display:none;'
}, },
// isNotLastChild(index, record) {
toggleExpanded: function(trIndex) { return index === 0 && record.children && record.children.length > 0
const record = this.formatData[trIndex]
record._expanded = !record._expanded
}, },
// toggleExpanded(trIndex) {
iconShow(index, record) { const record = this.res[trIndex]
return (index === 0 && record.children && record.children.length > 0) const expand = !record.__expand
record.__expand = expand
//
if (!expand) {
this.expandRecursion(record, expand)
}
},
expandRecursion(row, expand) {
if (row.children && row.children.length > 0) {
row.children.map(child => {
child.__expand = expand
this.expandRecursion(child, expand)
})
}
} }
} }
} }
</script> </script>
<style rel="stylesheet/css">
@keyframes treeTableShow {
from {opacity: 0;}
to {opacity: 1;}
}
@-webkit-keyframes treeTableShow {
from {opacity: 0;}
to {opacity: 1;}
}
</style>
<style lang="scss" rel="stylesheet/scss" scoped> <style lang="scss" rel="stylesheet/scss" scoped>
$color-blue: #2196F3; @keyframes treeTableShow {
$space-width: 18px; from {
.ms-tree-space { opacity: 0;
position: relative;
top: 1px;
display: inline-block;
font-style: normal;
font-weight: 400;
line-height: 1;
width: $space-width;
height: 14px;
&::before {
content: ""
}
} }
.processContainer{ to {
width: 100%; opacity: 1;
height: 100%;
} }
table td { }
line-height: 26px; @-webkit-keyframes treeTableShow {
from {
opacity: 0;
} }
to {
opacity: 1;
}
}
$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: "";
}
}
.tree-ctrl{ .tree-ctrl {
position: relative; position: relative;
cursor: pointer; cursor: pointer;
color: $color-blue; color: $color-blue;
margin-left: -$space-width; }
}
</style> </style>

View File

@ -1,89 +1,87 @@
## 写在前面 ## 写在前面
此组件仅提供一个创建TreeTable的解决思路
## prop说明 此组件仅提供一个创建 TreeTable 的解决思路
#### *data*
**必填**
原始数据,要求是一个数组或者对象 ## prop 说明
```javascript
[{ - data(**必填**)
key1: value1,
key2: value2, 原始数据,要求是一个数组或者对象
children: [{
```js
const data = [
{
key1: value1,
key2: value2,
children: [
{
key1: value1 key1: value1
}, },
{ {
key1: value1 key1: value1
}] }
}, ]
{ },
key1: value1 {
}] key1: value1
``` }
或者 ]
```javascript ```
{
key1: value1,
key2: value2,
children: [{
key1: value1
},
{
key1: value1
}]
}
```
#### columns 或者
列属性,要求是一个数组
1. text: 显示在表头的文字 ```javascript
2. value: 对应data的key。treeTable将显示相应的value {
3. width: 每列的宽度,为一个数字(可选) key1: value1,
key2: value2,
children: [{
key1: value1
},
{
key1: value1
}]
}
```
如果你想要每个字段都有自定义的样式或者嵌套其他组件columns可不提供直接像在el-table一样写即可如果没有自定义内容提供columns将更加的便捷方便 - columns
如果你有几个字段是需要自定义的几个不需要那么可以将不需要自定义的字段放入columns将需要自定义的内容放入到slot中详情见后文 列属性,要求是一个数组
```javascript
[{
value:string,
text:string,
width:number
},{
value:string,
text:string,
width:number
}]
```
#### expandAll 1. label: 显示在表头的文字
是否默认全部展开boolean值默认为false 2. key: 对应 data 的 key。treeTable 将显示相应的 value
3. width: 每列的宽度,为一个数字(可选)
树表组件将会根据 columns 的 key 属性生成具名插槽,如果你需要对列数据进行自定义,通过插槽即可实现
```javascript
const columns = [
// 建议第一列做展开收缩操作
{ label: '', key: '__spread', width: '200' },
{
value: string,
text: string,
width: number
},
{
value: string,
text: string,
width: number
}
]
```
#### evalFunc #### evalFunc
解析函数function非必须
如果不提供,将使用默认的[evalFunc](./eval.js) 解析函数function非必须
如果提供了evalFunc,那么会用提供的evalFunc去解析data并返回treeTable渲染所需要的值。如何编写一个evalFunc请参考[*eval.js*](https://github.com/PanJiaChen/vue-element-admin/blob/master/src/components/TreeTable/eval.js)或[*customEval.js*](https://github.com/PanJiaChen/vue-element-admin/blob/master/src/views/table/treeTable/customEval.js) 如果不提供,将使用默认的[evalFunc](./eval.js)
如果提供了 evalFunc,那么会用提供的 evalFunc 去解析 data并返回 treeTable 渲染所需要的值。
#### evalArgs #### evalArgs
解析函数的参数,是一个数组
**请注意自定义的解析函数参数第一个为this.data第二个参数为 this.expandAll,你不需要在evalArgs填写。一定记住这两个参数是强制性的并且位置不可颠倒** *this.data为需要解析的数据this.expandAll为是否默认展开* 解析函数的参数,是一个对象,如果需要展开所有的数据,那么就传入`{expand:true}`
如你的解析函数需要的参数为`(this.data, this.expandAll,1,2,3,4)`,那么你只需要将`[1,2,3,4]`赋值给`evalArgs`就可以了 ## 其他
如果你的解析函数参数只有`(this.data, this.expandAll)`,那么就可以不用填写evalArgs了 如果有其他的需求,请参考[el-table](http://element-cn.eleme.io/#/en-US/component/table)的 api 自行修改 index.vue
具体可参考[*customEval.js*](https://github.com/PanJiaChen/vue-element-admin/blob/master/src/views/table/treeTable/customEval.js)的函数参数和[customTreeTable](https://github.com/PanJiaChen/vue-element-admin/blob/master/src/views/table/treeTable/customTreeTable.vue)的`evalArgs`属性值
## slot
这是一个自定义列的插槽。
默认情况下treeTable只有一行行展示数据的功能。但是一般情况下我们会要给行加上一个操作按钮或者根据当行数据展示不同的样式这时我们就需要自定义列了。请参考[customTreeTable](https://github.com/PanJiaChen/vue-element-admin/blob/master/src/views/table/treeTable/customTreeTable.vue)[实例效果](https://panjiachen.github.io/vue-element-admin/#/table/tree-table)
`slot`和`columns属性`可同时存在,columns里面的数据列会在slot自定义列的左边展示
## 其他
如果有其他的需求,请参考[el-table](http://element-cn.eleme.io/#/en-US/component/table)的api自行修改index.vue

View File

@ -24,21 +24,26 @@ export default {
return { return {
columns: [ columns: [
{ {
text: '事件', label: '',
value: 'event', key: '__sperad'
},
{
label: '事件',
key: 'event',
width: 200 width: 200
}, },
{ {
text: 'ID', label: 'ID',
value: 'id' key: 'id'
}, },
{ {
text: '时间线', label: '时间线',
value: 'timeLine' key: 'timeLine'
}, },
{ {
text: '备注', label: '备注',
value: 'comment' key: 'comment'
} }
], ],
data: [ data: [