Skip to content

Commit 8456128

Browse files
author
Kenneth Cheng
committed
add format & badge
1 parent eea1ac3 commit 8456128

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/VueExcelColumn.vue

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
field: {type: String, default: 'dummy'},
99
label: {type: String, default: null},
1010
type: {type: String, default: 'string'},
11-
initStyle: {type: Object, default () {return {}}},
11+
initStyle: {type: Object, default: null},
1212
width: {type: String, default: '100px'},
1313
invisible: {type: Boolean, default: false},
1414
readonly: {type: Boolean, default: null},
@@ -21,6 +21,7 @@ export default {
2121
validate: {type: Function, default: null},
2222
change: {type: Function, default: null},
2323
link: {type: Function, default: null},
24+
format: {type: String, default: 'text'},
2425
cellClick: {type: Function, default: null},
2526
autoFillWidth: {type: Boolean, default: false},
2627
@@ -34,6 +35,8 @@ export default {
3435
sort: {type: Function, default: null},
3536
masking: {type: String, default: ''},
3637
placeholder: {type: String, default: ''},
38+
color: {type: [String, Function], default: null},
39+
bgcolor: {type: [String, Function], default: null},
3740
3841
toValue: {
3942
type: Function,
@@ -113,6 +116,8 @@ export default {
113116
return this.masking.repeat(val?.length || 0)
114117
case 'action':
115118
return this.placeholder || ''
119+
case 'badge':
120+
return `<span class='badge'>${val}</span>`
116121
default:
117122
return val
118123
}
@@ -126,7 +131,7 @@ export default {
126131
methods: {
127132
init () {
128133
const self = this
129-
let style = this.initStyle
134+
let style = this.initStyle || {}
130135
let validate = this.validate
131136
let allowKeys = this.allowKeys
132137
let lengthLimit = this.lengthLimit
@@ -209,6 +214,12 @@ export default {
209214
case 'action':
210215
this._listByClick = true
211216
break
217+
case 'badge':
218+
this._color = 'white'
219+
this._bgcolor = 'blue'
220+
this._format = 'html'
221+
style.textAlign = 'center'
222+
break
212223
default:
213224
throw new Error('VueExcelColumn: Not supported type:' + this.type)
214225
}
@@ -259,12 +270,15 @@ export default {
259270
options: this.options,
260271
summary: this.summary,
261272
masking: this.masking,
273+
format: this._format || this.format,
262274
toValue: this.toValue,
263275
toText: this.toText,
264276
register: this.register,
265277
placeholder: this.placeholder,
266278
cellClick: this.cellClick,
267-
listByClick: this.listByClick || this._listByClick
279+
listByClick: this.listByClick || this._listByClick,
280+
color: this.color || this._color,
281+
bgcolor: this.bgcolor || this._bgcolor
268282
})
269283
}
270284
}

src/VueExcelEditor.vue

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,15 @@
118118
link: item.link,
119119
select: item.options,
120120
datepick: item.type == 'date',
121-
'sticky-column': item.sticky
121+
stickyColumn: item.sticky
122122
}"
123123
:key="p"
124-
:style="Object.assign(cellStyle(record, item), renderColumnCellStyle(item))"
124+
:style="Object.assign(cellStyle(record, item), renderColumnCellStyle(item, record))"
125125
@mouseover="cellMouseOver"
126-
@mousemove="cellMouseMove">{{ item.toText(record[item.name]) }}</td>
126+
@mousemove="cellMouseMove">
127+
<template v-if="item.format=='html'"><span v-html="item.toText(record[item.name])" /></template>
128+
<template v-else>{{ item.toText(record[item.name]) }}</template>
129+
</td>
127130
<td v-if="vScroller.buttonHeight < vScroller.height" class="last-col"></td>
128131
</tr>
129132
</tbody>
@@ -904,10 +907,14 @@ export default defineComponent({
904907
const instance = getCurrentInstance()
905908
instance?.proxy?.$forceUpdate()
906909
},
907-
renderColumnCellStyle (field) {
910+
renderColumnCellStyle (field, record) {
908911
let result = field.initStyle
909912
if (field.readonly) result = Object.assign(result, this.readonlyStyle)
910913
if (field.left) result = Object.assign(result, {left: field.left})
914+
if (record && field.bgcolor)
915+
result = Object.assign(result, {'--bgcolor': typeof field.bgcolor === 'function' ? field.bgcolor(record) : field.bgcolor })
916+
if (record && field.color)
917+
result = Object.assign(result, {'--color': typeof field.color === 'function' ? field.color(record) : field.color })
911918
return result
912919
},
913920
localeDate (d) {
@@ -2299,7 +2306,7 @@ export default defineComponent({
22992306
const row = e.target.parentNode
23002307
const colPos = Array.from(row.children).indexOf(e.target) - 1
23012308
const currentField = this.fields[colPos]
2302-
if (currentField.type === 'action')
2309+
if (currentField?.type === 'action')
23032310
cursor = 'pointer'
23042311
e.target.style.cursor = cursor
23052312
},
@@ -2970,8 +2977,16 @@ input:focus, input:active:focus, input.active:focus {
29702977
white-space: nowrap;
29712978
overflow-x: hidden;
29722979
text-overflow: ellipsis;
2980+
color: var(--color);
29732981
/* animation: fadein 0.2s; */
29742982
}
2983+
.systable tbody td :deep(.badge) {
2984+
padding: 0px 10px;
2985+
border-radius: 10px;
2986+
background-color: var(--bgcolor);
2987+
font-weight: 600
2988+
}
2989+
29752990
.systable tbody td.link {
29762991
color: blue;
29772992
cursor: pointer !important;

0 commit comments

Comments
 (0)