Vxe UI vue vxe-table vxe-grid 单元格与表尾单元格如何格式化数据
查看 github
vxe-table 官网
单元格内容格式化
通过 formatter 属性自定义格式化方法
<template>
<div>
<vxe-grid v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script setup>
import { reactive } from 'vue'
import XEUtils from 'xe-utils'
const sexList = [
{ label: '女', value: '0' },
{ label: '男', value: '1' }
]
const gridOptions = reactive({
border: true,
columns: [
{ type: 'seq', width: 70 },
{ field: 'name', title: 'Name' },
{
field: 'num',
title: 'Num',
formatter ({ cellValue }) {
return XEUtils.commafy(Number(cellValue), { digits: 2 })
}
},
{
field: 'sex',
title: 'Sex',
formatter ({ cellValue }) {
const item = sexList.find(item => item.value === cellValue)
return item ? item.label : cellValue
}
},
{
field: 'time',
title: 'Time',
formatter ({ cellValue }) {
return XEUtils.toDateString(cellValue, 'yyyy-MM-dd HH:ss:mm')
}
}
],
data: [
{ id: 10001, name: 'Test1', bankCard: '6222525678789432', sex: '0', time: 1599320111520, date: '2020-11-14T07:14:41.000Z', amount: 998.3, num: 863.345, num7: 863.345, num8: 863.345, num9: 863.345 },
{ id: 10002, name: 'Test2', bankCard: '6222525675674564', sex: '1', time: 1590820967410, date: '2022-10-24T08:14:18.000Z', amount: 777776536.3, num: 854.7789, num7: 854.7789, num8: 854.7789, num9: 854.7789 },
{ id: 10003, name: 'Test3', bankCard: '6222525477686963', sex: '0', time: 1599390785410, date: '2020-09-04T06:08:25.000Z', amount: 253.486, num: 963.1456, num7: 963.1456, num8: 963.1456, num9: 963.1456 },
{ id: 10004, name: 'Test4', bankCard: '6222525678678946', sex: '1', time: 1597385230710, date: '2019-10-20T20:40:20.000Z', amount: 9990000.66, num: 963.9856, num7: 963.9856, num8: 963.9856, num9: 963.9856 }
]
})
</script>
表尾单元格内容格式化
通过 footerFormatter 属性自定义格式化方法
<template>
<div>
<vxe-grid v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const gridOptions = reactive({
border: true,
showFooter: true,
height: 300,
columns: [
{ field: 'seq', type: 'seq', width: 70 },
{ field: 'name', title: 'Name' },
{ field: 'sex', title: 'Sex' },
{
field: 'num',
title: 'Num',
footerFormatter ({ itemValue }) {
return `¥${itemValue}元`
}
},
{ field: 'address', title: 'Address', showOverflow: true }
],
data: [
{ id: '10001', name: 'Test1', role: 'Develop', sex: 'Man', num: '28', address: 'test abc' },
{ id: '10002', name: 'Test2', role: 'Test', sex: 'Women', num: '22', address: 'Guangzhou' },
{ id: '10003', name: 'Test3', role: 'PM', sex: 'Man', num: '32', address: 'Shanghai' },
{ id: '10004', name: 'Test4', role: 'Designer', sex: 'Women', num: '24', address: 'Shanghai' },
{ id: '10005', name: 'Test5', role: 'Develop', sex: 'Man', num: '42', address: 'Guangzhou' },
{ id: '10006', name: 'Test6', role: 'Test', sex: 'Women', num: '39', address: 'Shanghai' },
{ id: '10007', name: 'Test7', role: 'Develop', sex: 'Man', num: '46', address: 'Shanghai' },
{ id: '10008', name: 'Test8', role: 'PM', sex: 'Women', num: '49', address: 'Guangzhou' }
],
footerData: [
{ seq: '合计', num: '282' }
]
})
</script>