index.vue
17.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<!-- 表格+分页 -->
<template>
<div class="d2-table">
<el-table
:ref="tableParams.mutiSelectName + 'MutiTable' || 'mutiTable'"
:data="getListApi ? data : tableData"
:row-key="tableParams.mutiSelectUniqueKey || ''"
v-loading="loading"
:border="border"
:highlight-current-row="true"
@sort-change="sortChange"
@selection-change="handleSelect"
:tree-props="{ children: 'upgradeGoodsList' }"
:span-method="arraySpanMethod"
v-bind="$attrs"
v-scroll="scroll"
>
<el-table-column
v-if="tableParams.mutiSelectUniqueKey && tableParams.mutiSelectName"
type="selection"
align="center"
:selectable="selectableFn"
width="55"
/>
<el-table-column
v-if="tableParams.singleSelectUniqueKey"
align="center"
width="55"
>
<template slot-scope="scope">
<slot name="single-button" :scope="scope.row" />
</template>
</el-table-column>
<el-table-column
v-if="hasNum"
align="center"
width="60px"
label="序号"
type="index"
:index="
(index) =>
index +
((pagination.currentPage
? pagination.currentPage
: page.currentPage) -
1) *
(pagination.pageSize ? pagination.pageSize : page.pageSize) +
1
"
/>
<el-table-column
v-for="(item, index) in tableParams.tableColumns"
:key="index"
:label="item.label"
:prop="item.prop"
:width="item.width"
:min-width="item.minWidth"
:align="item.align || 'center'"
:sortable="item.sortable"
:render-header="item.renderHeader"
:show-overflow-tooltip="item.showOverFlow"
>
<template slot="header">
<span>{{ item.label }}</span>
<table-head-search
v-if="item.optionConfig"
:search-form="headSearchForm"
:option-config="item.optionConfig"
:base-data="baseData"
@handleTableHeadChange="handleTableHeadChange"
/>
</template>
<template slot-scope="scope">
<slot
v-if="item.addHtml"
:name="item.addHtml"
:scope="scope.row"
:index="scope.$index"
:prop="item.prop"
:width="item.width"
/>
<template v-else-if="item.type == 'ownSerialNum'">
<span>{{
scope.row[item.prop] >= 0
? scope.row[item.prop] +
((pagination.currentPage
? pagination.currentPage
: page.currentPage) -
1) *
(pagination.pageSize
? pagination.pageSize
: page.pageSize) +
1
: ""
}}</span>
</template>
<template v-else-if="item.tagLabel">
<el-tag
v-if="!item.tagName"
:type="tableParams.tag.type[scope.row[item.prop]]"
v-text="tableParams.tag.label[scope.row[item.prop]]"
/>
<el-tag
v-else
:type="tableParams[item.tagName].type[scope.row[item.prop]]"
v-text="tableParams[item.tagName].label[scope.row[item.prop]]"
/>
</template>
<template v-else-if="item.formatter">
<span v-if="item.html" v-html="item.formatter(scope.row)" />
<template v-if="!item.html">
<span :class="item.className ? item.className(scope.row) : ''" />
<span v-text="item.formatter(scope.row)" />
</template>
</template>
<template v-else-if="getListApi">
<span v-if="item.prefix">{{ item.prefix }}</span>
<span v-if="item.parentProp">{{
scope.row[item.parentProp]
? scope.row[item.parentProp][item.prop]
: ""
}}</span>
<!-- <span v-else>{{
scope.row[item.prop] != null || scope.row[item.prop] != ""
? scope.row[item.prop]
: "-"
}}</span> -->
<span v-else>{{ scope.row[item.prop] || "-" }}</span>
</template>
<template v-else>
<span
v-text="
tableData[scope.$index][item.prop] != null
? tableData[scope.$index][item.prop]
: '-'
"
/>
</template>
<template v-if="item.addSymbol">
<span v-text="item.addSymbol" />
</template>
</template>
</el-table-column>
<el-table-column
v-if="tableParams.tableOption"
label="操作"
fixed="right"
:align="tableParams.tableOption.align || 'center'"
:width="tableParams.tableOption.width || 'auto'"
>
<template slot-scope="scope">
<slot name="auth-button" :scope="scope.row" :index="scope.$index" />
<slot
name="auth-button-other"
:scope="scope.row"
:index="scope.$index"
/>
</template>
</el-table-column>
<div slot="empty" class="emptyBg">
<img class="noDataImg" :src="noDataImg" v-if="noDataImg" />
{{ noDataText }}
</div>
</el-table>
<el-pagination
v-if="hasPage"
background
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="page.currentPage"
:page-sizes="[5, 10, 20, 30, 40]"
:page-size="page.pageSize"
:total="getListApi ? total : tableTotal"
:layout="layout"
/>
</div>
</template>
<script>
import TableHeadSearch from "./TableHeadSearch"
export default {
directives: {
scroll: {
bind: function (el, binding) {
const ele = el.querySelector(".el-table__body-wrapper")
ele.addEventListener("scroll", function (e) {
binding.value(e)
})
}
}
},
name: "D2Table",
data() {
return {
loading: false,
data: [],
// 页码
page: {
pageSize: 10,
currentPage: 1
},
total: 10,
// 排序
sortColum: "",
sort: "",
pagination: {},
// 多选数组
mutiSelectArr: {}
}
},
components: {
TableHeadSearch
},
props: {
// 搜索条件
searchForm: {
type: Object,
default: () => {}
},
// 表格头部搜索条件 - start
headSearchForm: {
type: Object,
default: () => {}
},
// 选项列表Obj
baseData: {
type: Object,
default: () => {}
},
// 表格头部搜索条件 - end
// 表格数据-外部传入
tableData: {
type: Array,
default: () => []
},
tableTotal: {
type: Number,
default: 0
},
// 获取列表接口
getListApi: {
type: Function,
default: null
},
// 搜索前操作
beforeSearchFn: {
type: Function,
default: () => {
return new Promise((resolve, reject) => {
resolve()
})
}
},
// 分页,默认有
hasPage: {
type: Boolean,
default: true
},
// 分页组件配置
layout: {
type: String,
default: "sizes,total,->,prev,pager,next,jumper"
},
pageSize: {
// 分页码数
type: Number,
default: 10
},
// 序号,默认有
hasNum: {
type: Boolean,
default: true
},
// 排序列表对象
sortObj: {
type: Object,
default: () => {}
},
tableParams: {
type: Object,
default: () => {
return {
// 多选-判断来自哪个页面
// mutiSelectName: {
// type: String,
// default: ''
// },
// 多选-唯一标识
// mutiSelectUniqueKey: {
// type: String,
// default: ''
// },
// 搜索按钮id;new Date().getTime() + parseInt(Math.random() * 1000)
// btnId: {
// type: String,
// default: ''
// },
// 后端返回列表参数名
listName: {
type: String,
default: ""
},
// 标签-默认标签名为tag;若有多个,那么需要在表格栏的列中加“tagName”
// tag: {
// type: Array,
// default: () => []
// },
// 表格操作,默认没有
// tableOption: {
// type: Object,
// default: null
// },
// 表格栏
tableColumns: {
type: Array,
default: () => []
}
}
}
},
// 获取CheckBox是否可以勾选
selectableFn: {
type: Function,
default: null
},
// 列表数据需特殊处理
filterListData: {
type: Boolean,
default: false
},
// 合并单元格方法
arraySpanMethod: {
type: Function,
default: () => {}
},
// 边框
border: {
type: Boolean,
default: true
},
noDataImg: {
type: String,
default: ""
},
noDataText: {
type: String,
default: "暂无数据"
}
},
mounted() {
const btn = document.getElementById(this.tableParams.btnId)
btn && btn.addEventListener("click", this.refreshEmit)
this.page.pageSize = this.pageSize
this.getListApi && this.refreshTable()
},
methods: {
// 排序
sortChange({ column, prop, order }) {
this.sortColum = order ? prop : ""
this.sort = order
if (this.getListApi) {
this.refreshTable(true, true)
} else {
// this.$emit('sortChange', this.sortObj[this.sortColum][this.sort], this.sortColum)
this.$emit("sortChange", this.sortObj, this.sortColum, this.sort)
}
},
// 多选
handleSelect(val) {
this.mutiSelectArr[this.tableParams.mutiSelectName] = val
// 多选
this.$emit("selectionTable", val)
},
// 清空多选
clearMutiSelect() {
this.mutiSelectArr[this.tableParams.mutiSelectName] = []
this.$refs[this.tableParams.mutiSelectName + "MutiTable"].clearSelection()
},
// 发布事件,以期查询条件改变后,页面图表需要刷新
refreshEmit() {
this.$emit("searchChange")
this.refreshTable(true, true)
},
refreshTable(clearMuti = false, resetCurrentPage = false) {
this.beforeSearchFn().then((res) => {
// console.log(res)
// 清空多选
clearMuti && this.clearMutiSelect()
this.loading = true
resetCurrentPage && (this.page.currentPage = 1)
var params = {
...this.page,
...this.searchForm
}
if (this.sort) {
// params.sortColum = this.sortColum
// params.sort = this.sort === 'descending' ? 1 : 0
params.orderByType = this.sortObj[this.sortColum][this.sort]
// params.orderByKey = this.sortColum
}
this.getListApi(params)
.then((res) => {
this.pagination = {
currentPage: res.pageNum,
pageSize: res.pageSize
}
// this.tableParams.needUnique && (res[this.tableParams.listName].forEach((item) => {
// item[this.tableParams.mutiSelectUniqueKey]
// }))
//
res[this.tableParams.listName] &&
res[this.tableParams.listName].forEach((val, index) => {
if (!val.goodsInfo) val.goodsInfo = {}
const shopPriceArr = []
val.ownSerialNum = index
if (this.filterListData) {
val.upgradeGoodsList &&
val.upgradeGoodsList.forEach((v) => {
const {
goodsTypeName,
secondaryGoodsTypeName,
thirdLevelGoodsTypeName
} = val.goodsInfo
const types = [
goodsTypeName,
secondaryGoodsTypeName,
thirdLevelGoodsTypeName
]
v.goodsInfo = {}
shopPriceArr.push(v.upgradePrice)
v.listShowShopPrice = v.upgradePrice
let specificationTypeSequenceArr = v.specificationTypeSequence?.split(';~;')
let specificationValueSequenceArr = v?.specificationData?.split(';~;')
const suffixMapping = {
1: '年',
2: '(km/万)',
3: '万'
}
if (specificationTypeSequenceArr?.length) {
specificationValueSequenceArr?.forEach((val, index) => {
const suffix = suffixMapping[specificationTypeSequenceArr[index]]
if (suffix) {
specificationValueSequenceArr[index] = `${val}${suffix}`
}
})
v.listShowSpecificationData = specificationValueSequenceArr?.join('/')
} else {
v.listShowSpecificationData = v?.specificationData?.replace(RegExp(';~;', 'g'), '/')
}
// v.listShowSpecificationData =
// val.goodsSpecificationNameList?.length > 1
// ? v.specificationData &&
// v.specificationData?.replace(/;~;/g, " ")
// : "-"
v.goodsInfo.goodsName = v.upgradeGoodsName
// v.goodsInfo.goodsTypeName = val.goodsInfo?.goodsTypeName
v.goodsInfo.goodsTypeName = types
.filter((type) => type)
.join("-")
v.goodsInfo.type = v.type || "-"
v.goodsInfo.thumbUrl = val.goodsInfo.thumbUrl
v.goodsSpecificationNameList =
val.goodsSpecificationNameList
v.createdDate = val.createdDate
v.updatedDate = val.updatedDate
})
const minPrice = shopPriceArr.length
? Math.min(...shopPriceArr)
: 0
const maxPrice = shopPriceArr.length
? Math.max(...shopPriceArr)
: 0
// 父商品规格数量
val.listShowSpecificationData =
val.goodsSpecificationNameList?.length > 1 &&
val.upgradeGoodsList?.length > 0
? "X" + val.upgradeGoodsList.length
: "-"
val.listShowShopPrice =
minPrice == maxPrice ? maxPrice : minPrice + "~" + maxPrice
val.goodsInfo.type = "-"
}
})
this.data = res[this.tableParams.listName]
if (this.tableParams.needUnique) {
let temp = 1
this.data.forEach((item) => {
item[this.tableParams.mutiSelectUniqueKey] = temp
temp = temp + 1
if (item.hasOwnProperty("upgradeGoodsList")) {
item.upgradeGoodsList.forEach((subItem) => {
subItem[this.tableParams.mutiSelectUniqueKey] = temp
temp = temp + 1
})
}
})
}
this.total = res.total
this.loading = false
this.$emit("setTableFinishFun", this.data)
})
.catch(() => {
this.loading = false
})
})
},
// 表头搜索项-改变
handleTableHeadChange(obj) {
this.$emit("handleTableHeadChange", obj)
},
// 每页条数
handleSizeChange(value) {
this.page.pageSize = value
this.getListApi
? this.refreshTable(false)
: this.$emit("refreshTable", this.page)
this.$emit("pageChange", this.page)
},
// 页码
handleCurrentChange(value) {
this.page.currentPage = value
this.getListApi
? this.refreshTable(false)
: this.$emit("refreshTable", this.page)
this.$emit("pageChange", this.page)
},
// 重置请求
resetRefreshTable() {
this.refreshTable_noApi()
this.refreshTable()
},
refreshTable_noApi() {
this.page = {
pageSize: 10,
currentPage: 1
}
},
scroll(e) {
this.$emit("scroll", e)
}
}
}
</script>
<style lang="scss">
.a-click {
cursor: pointer;
}
.el-table .cell {
line-height: 18px;
}
.d2-table p {
margin: 10px 0;
}
.d2-table .head-search {
cursor: pointer;
}
@mixin spotBase {
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 5px;
display: inline-block;
}
.ashSpot {
@include spotBase();
background: #c6c8cc;
}
.greenSpot {
@include spotBase();
background: #2cca74;
}
.yellowSpot {
@include spotBase();
background: #fdb757;
}
.redSpot {
@include spotBase();
background: #eb4e4e;
}
.orgenSpot {
@include spotBase();
background: #eb4e4e;
}
.blueSpot {
@include spotBase();
background: #5850ff;
}
.deepBlueSpot {
@include spotBase();
background: #185efe;
}
.emptyBg {
display: flex;
flex-direction: column;
align-items: center;
.noDataImg {
width: 270px;
height: 120px;
margin-bottom: 8px;
}
}
</style>