TreeNode.vue
6.1 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<template>
<div class="menuCollapse">
<div v-for="(item,index) in list" :key="index">
<el-collapse v-model="activeName" accordion @change="hanleClick(item,index)">
<el-collapse-item title="" :name="index" element-loading-text="数据加载中" element-loading-spinner="el-icon-loading">
<div slot="title">
<span>
<el-checkbox v-model="item.checkbox" :indeterminate="item.isIndeterminate" @change="handleCheckAllChange({ val: item, checked: $event })">
{{ item.name }}
</el-checkbox>
</span>
</div>
<div class="children-item">
<div class="menuList">
<ul v-if="item.resources.length>0">
<li v-for="val in item.resources" :key="val.id"><el-checkbox v-model="val.checkbox" @change="hanleClickBox(val)"><span :title="val.name">{{ ellipsis(val.name, 5) }}</span></el-checkbox></li>
</ul>
<div v-else class="noData">
暂时还没有按钮数据哦~
</div>
</div>
<template>
<tree-list :list="item.children" :role="menuData" />
</template>
</div>
</el-collapse-item>
</el-collapse>
</div>
</div>
</template>
<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { ellipsis } from '@/utils'
// api接口
import { getmenuList } from '@/pages/menu/api/index'
@Component({
name: 'tree-list'
})
export default class extends Vue {
@Prop() private list!: []
@Prop() private resourceId!:[]
@Prop() private menuData!:[]
private roleData = []
private activeName= '1'
private clientToken:any
private checkedVal=[]
private checkboxData = []
private datas = []
private role=[]
private allCheckboxIds = [] as any
@Watch('list', { deep: true })
getMenuData(val:any) {
this.role = val
}
@Watch('roleData', { immediate: true })
getRoleData(val:any) {
// 反选
if (val.length > 0) {
this.$store.state.menubtnList.forEach(async(obj:any) => {
// if (obj.parendId === this.roledata.id) {
val.forEach((ele:any) => {
if (obj === ele.id) {
ele.checkbox = true
}
})
// }
})
}
}
// 功能
// 获取按钮列表
private async getmenList(id:string) {
const resourceId = this.$store.state.menuData
const { data } = await getmenuList({ menuId: id })
this.roleData = data.data.records.map((obj: any) => ({ ...obj, checkbox: false }))
// 反选
if (resourceId.length > 0) {
resourceId.forEach(async(obj:any) => {
this.roleData.forEach((ele:any) => {
if (obj === ele.id) {
ele.checkbox = true
}
})
})
}
}
// ui
// 获取按钮列表
hanleClick(val:any) {
}
handleCheckAllChange(data:any) {
let { val, checked } = data
if (val.children.length > 0) {
// 处理下级
this.findChildren(val.children, checked)
} else {
// 处理本级
val.children.forEach((v:any) => {
v.checkbox = checked
})
}
if (val.parentId !== -1) {
// 处理上级
this.findParent(this.list, val.parentId)
}
val.isIndeterminate = false
}
// 设置子级
findChildren(list:any, checked:any) {
list.forEach((child:any) => {
child.checkbox = checked
child.isIndeterminate = false
if (child.children.length > 0) {
this.findChildren(child.children, checked)
}
})
}
// 设置这一整条线
findParent(list:any, parentId:any) {
list.forEach((k:any) => {
this.handleList(k, parentId)
})
}
// 设置这一整条线具体方法
handleList(child:any, parentId:any) {
let parentCheckedLength = 0
let parentIndeterminateLength = 0
if (child.id === parentId) {
child.children.forEach((children:any) => {
if (children.isIndeterminate) {
parentIndeterminateLength++
} else if (children.checkbox) {
parentCheckedLength++
}
})
child.checkbox = parentCheckedLength === child.children.length
child.isIndeterminate = (parentIndeterminateLength > 0 || parentCheckedLength > 0) && parentCheckedLength < child.children.length
if (child.parentId !== -1) {
this.findParent(this.list, child.parentId)
}
} else if (child.children.length > 0) {
this.findParent(child.children, parentId)
}
}
// 递归 根据不同的菜单id筛选获取详情已选择的选项
async filterMenu(arr:any, id:string) {
let arrRes = [] as any
for (let i = 0, length = arr.length; i < length; i++) {
let node = arr[i]
// node.checkbox = false
if (node.id === id) {
node.checkbox = true
arrRes.push(node)
// break
} else {
if (node.children) {
this.filterMenu(node.children, id)
}
if (node.resources) {
this.filterMenu(node.resources, id)
}
arrRes.push(node)
/** */
}
}
return arrRes
}
hanleClickBox(val:any) {
this.$set(this.list, val.checkbox, [])
this.$forceUpdate()
}
findChildrenresources(list:any, val:any) {
list.forEach((child:any) => {
if (child.id === val.id) {
child.checkbox = val.checkbox
}
})
}
// 重新递归设置半选状态
setTreeIndeterminateStatus(data:any) {
data.forEach((v:any) => {
let parentCheckedLength = 0
let parentIndeterminateLength = 0
v.children.forEach((children:any) => {
if (children.isIndeterminate) {
parentIndeterminateLength++
} else if (children.checkbox) {
parentCheckedLength++
}
})
v.isIndeterminate = (parentIndeterminateLength > 0 || parentCheckedLength > 0) && parentCheckedLength < v.children.length
if (v.children && v.children.length) {
this.setTreeIndeterminateStatus(v.children)
}
})
}
// 内容控制字数,多出的用省略号
ellipsis(value: any, num: any) {
return ellipsis(value, num)
}
}
</script>
<style lang="scss"></style>