index.vue
3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<template>
<el-table-column :prop="prop" v-bind="$attrs">
<template slot-scope="scope">
<span @click.prevent="toggleHandle(scope.$index, scope.row)" :style="childStyles(scope.row)">
<i v-if="hasChild(scope.row)" :class="iconClasses(scope.row)" :style="iconStyles(scope.row)"></i>
{{ scope.row[prop] }}
</span>
</template>
</el-table-column>
</template>
<script>
import isArray from 'lodash/isArray'
export default {
name: 'table-tree-column',
data(){
return {
flag : true
}
},
props: {
prop: {
type: String
},
treeKey: {
type: String,
default: 'id'
},
parentKey: {
type: String,
default: 'parentId'
},
levelKey: {
type: String,
default: 'level'
},
childKey: {
type: String,
default: 'children'
},
hasChildKey: {
type: String,
default: 'hasChild'
},
},
methods: {
childStyles (row) {
return { 'padding-left': (row[this.levelKey] * 25) + 'px' }
},
iconClasses (row) {
return [ !row._expanded ? 'el-icon-caret-right' : 'el-icon-caret-bottom' ]
},
iconStyles (row) {
return { 'visibility': this.hasChild(row) ? 'visible' : 'hidden' }
},
hasChild (row) {
// console.log(row[this.hasChildKey],'hasChildKey')
// return (isArray(row[this.childKey]) && row[this.childKey].length >= 1) || false
return row[this.hasChildKey]
},
// 切换处理
toggleHandle (index, row) {
// if (this.hasChild(row)) {
var data = this.$parent.store.states.data.slice(0)
console.log(row,'是否已经点击了')
if(!data[index].hasClick){ //判断是否已点击加载完成
data[index]._expanded = !data[index]._expanded
if (data[index]._expanded) {
// console.log('kai')
if(data[index].hasAsyc == 1){ //判断是否异步加载
// console.log(data,'展开 但不请求数据')
data = data.splice(0, index + 1).concat(this.addChildNode(row[this.childKey])).concat(data)
}else{
// console.log(data,'展开,请求数据data')
this.$emit('table-click',this,index,row,data);
}
} else {
// console.log(data[index].children,'guan')
// console.log(row[this.treeKey],'guan')
data = this.removeChildNode(data, row[this.treeKey])
}
this.$nextTick(() => {
this.$parent.store.commit('setData', data)
this.$parent.doLayout()
})
}
// }
},
addChildNode (row) {
var arr = []
this.forE(row,arr);
return arr
},
forE(row,arr){
var _this = this;
if(row){
row.forEach(item => {
if(item._expanded){
arr.push(item);
_this.forE(item[_this.childKey],arr)
}else{
arr.push(item);
}
});
}
},
// 移除子节点
removeChildNode (data, parentId) {
var parentIds = isArray(parentId) ? parentId : [parentId]
if (parentId.length <= 0) {
return data
}
var ids = []
for (var i = 0; i < data.length; i++) {
if (parentIds.indexOf(data[i][this.parentKey]) !== -1 && parentIds.indexOf(data[i][this.treeKey]) === -1) {
ids.push(data.splice(i, 1)[0][this.treeKey])
i--
}
}
return this.removeChildNode(data, ids)
}
}
}
</script>