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
<template>
<div>
<draggable class="home-wrap" v-model="layoutList" :disabled="true" filter=".undraggable" @change="handleChange">
<div
class="cell-item draggable"
ref="cellItem"
v-for="(item, index) in calcLayoutList"
:key="index"
:style="{
'width': `calc((100% - ${(item.rowCount- 1) * 8}px) * ${item.width / 100})`,
'height': `calc(${item.height}% - 8px)`,
'margin-right': item.isRowLastTag ? '0' : '8px'
}"
>
<!-- @adjust="handleAdjust" -->
<component
:is="item.component" :componentName="item.component" style="width:100%; height:100%"
></component>
</div>
</draggable>
</div>
</template>
<script>
import Api from '~/service/Api';
import draggable from "vuedraggable";
import CommonSystem from './components/CommonSystem'
import ForeignExchangeRates from "./components/ForeignExchangeRates";
import NoticeAnnouncement from "./components/NoticeAnnouncement";
import Double from "./components/Double";
import { cloneDeep } from 'lodash'
export default {
name: "home",
components: {
draggable,
CommonSystem,
ForeignExchangeRates,
NoticeAnnouncement,
Double
},
data () {
return {
layoutList: [
{
component: 'Double',
width: 58,
height: 52
},
{
component: 'ForeignExchangeRates',
width: 42,
height: 52
},
{
component: 'NoticeAnnouncement',
width: 58,
height: 48
},
{
component: 'CommonSystem',
width: 42,
height: 48
},
],
calcLayoutList: []
}
},
mounted () {
this.handleCalcLayoutList()
},
methods: {
handleCalcLayoutList () {
let rowIndex = 0
let rowTotalWidth = 0
let list = cloneDeep(this.layoutList)
let curList = []
// 处理当前card处于哪一行,添加行索引
list.map((item) => {
if (rowTotalWidth + item.width < 100) {
rowTotalWidth += item.width
} else if (rowTotalWidth + item.width === 100) {
if (!rowTotalWidth) {
rowIndex++
}
rowTotalWidth = 100
} else {
rowTotalWidth = item.width
rowIndex++
}
item.rowIndex = rowIndex
})
let curInd = 0
// 处理每一行有几个card,为了方便计算显示的宽度和margin
list.map((item, index) => {
if (curInd !== item.rowIndex) {
curInd = item.rowIndex
}
let len = list.filter((fil) => {
return fil.rowIndex === curInd
}).length
item.rowCount = len
})
// 处理每一行最后一项的问题
list.map((item, index) => {
item.isRowLastTag = false
if (index < (list.length - 1) && (item.rowIndex != list[index + 1].rowIndex)) {
item.isRowLastTag = true
}
if (index === list.length - 1) {
item.isRowLastTag = true
}
})
this.calcLayoutList = list
},
async handleChange(e){
// console.log(e);
// console.log(this.layoutList)
// let params = {
// layoutList: this.layoutList
// }
// let res = await Api.post(`/business/home`, params);
// if (res.respCode == SUCCESS) {
// this.$message({type: 'success',message: '操作成功!'});
// }
this.handleCalcLayoutList()
},
// // 传回滑动值及当前组件名称
// handleAdjust(val) {
// // 改变对应组件的宽度
// this.layoutList.forEach(item=>{
// if(val.componentName === item.component) {
// item.width = val.value;
// }
// })
// this.handleCalcLayoutList()
// }
}
};
</script>
<style scoped>
.home-wrap {
width: 100%;
height: calc(100vh - 42px);
display: flex;
align-items: flex-start;
justify-content: flex-start;
flex-wrap: wrap;
overflow: auto;
padding: 8px 0;
box-sizing: border-box;
user-select: none;
min-width: 1000px;
overflow-x: auto;
}
.cell-item {
/* height: calc((100% - 50px) / 2); */
margin-bottom: 8px;
margin-right: 8px;
position: relative;
}
</style>