动态行列合并
... 2022-3-9 About 2 min
# 动态行列合并
<avue-crud
:data="data"
:option="option"
:span-method="spanMethod"
></avue-crud>
<script>
export default {
data() {
return {
data: [
{
name: '王小虎',
sex:'男',
grade:'级别1',
amount1: '234',
amount2: '3.2',
amount3: 10
},
{
name: '王小虎',
sex:'男',
grade:'级别2',
amount1: '165',
amount2: '4.43',
amount3: 12
}, {
name: '王三虎',
sex:'女',
grade:'级别2',
amount1: '539',
amount2: '4.1',
amount3: 15
}, {
name: '王三虎',
sex:'女',
grade:'级别1',
amount1: '539',
amount2: '4.1',
amount3: 15
},
{
name: '王小虎',
sex:'男',
grade:'级别3',
amount1: '324',
amount2: '1.9',
amount3: 9
},
{
name: '王小虎',
sex:'女',
grade:'级别1',
amount1: '621',
amount2: '2.2',
amount3: 17
},
{
name: '王小虎',
sex:'女',
grade:'级别1',
amount1: '539',
amount2: '4.1',
amount3: 15
},
{
name: '王大虎',
sex:'男',
grade:'级别2',
amount1: '539',
amount2: '4.1',
amount3: 15
},{
name: '王小虎',
sex:'男',
grade:'级别1',
amount1: '539',
amount2: '4.1',
amount3: 15
}, {
name: '王大虎',
sex:'女',
grade:'级别2',
amount1: '539',
amount2: '4.1',
amount3: 15
}
],
option: {
border: true,
column: [
{
label: '姓名',
prop: 'name'
},
{
label: '年龄',
prop: 'sex'
},
{
label: '数值 1',
prop: 'amount1'
},
{
label: '数值 2',
prop: 'amount2'
},
{
label: '数值 3',
prop: 'amount3'
},{
label: '权限',
prop: 'grade'
},
]
},
spanArr: [{
prop: 'name',
span: []
}, {
prop: 'sex',
span: []
}, {
prop: 'grade',
span: []
}]
}
},
created(){
this.rowSort()
this.rowCalc();
},
methods: {
rowCalc() {
this.spanArr.forEach((ele, index) => {
let parent
if(index!==0)parent= this.spanArr[ele.parent || index - 1].span
ele.span = this.rowSpan(ele.prop,parent)
})
},
rowSort(list) {
let propList= this.spanArr.map(ele=>ele.prop)
this.spanArr.forEach((ele, index) => {
let key = ele.prop
this.data = this.data.sort((a, b) => {
let flag=true;
for(let i =0;i<index;i++){
let prop= this.spanArr[i].prop
flag = flag && a[prop] == b[prop]
}
if (flag){
if (a[key] < b[key]) { return 1; }
else if (a[key] > b[key]) { return -1; }
return 0;
}
return 0;
})
})
},
rowSpan(key, parent) {
let list = [];
let position = 0;
this.data.forEach((item, index) => {
if (index === 0) {
list.push(1)
let position = 0;
} else {
if (this.data[index][key] === this.data[index - 1][key]) {
if (parent && parent[index] !== 0) {
list.push(1)
position = index
} else {
list[position] += 1;
list.push(0)
}
} else {
list.push(1)
position = index
}
}
})
return list
},
spanMethod({ row, column, rowIndex, columnIndex }) {
for (let i = 0; i < this.spanArr.length; i++) {
const ele = this.spanArr[i]
if (column.property == ele.prop) {
const _row = ele.span[rowIndex];
const _col = _row > 0 ? 1 : 0;
return {
rowspan: _row,
colspan: _col
}
}
}
}
}
}
</script>
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
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
Expand Copy Copy