You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
2.6 KiB
Vue

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<div>
<el-alert
effect="dark"
:closable="false"
title="基于 Element 的 Table 组件进行二次封装,实现数据驱动,支持所有 Table 参数 -- 表尾合计行"
type="info"
style="margin-bottom: 20px"
/>
<com-table v-loading="loading" :columns="columns" :data="tableData" border show-summary />
<com-table
v-loading="loading"
:columns="columns1"
:data="tableData"
border
height="200"
:summary-method="getSummaries"
show-summary
style="margin-top: 20px"
/>
</div>
</template>
<script setup lang="ts" name="TotalTable">
import { ref } from 'vue'
const columns = [
{
field: 'id',
label: 'ID'
},
{
field: 'name',
label: ''
},
{
field: 'amount1',
label: '1',
sortable: true
},
{
field: 'amount2',
label: '2',
sortable: true
},
{
field: 'amount3',
label: '4',
sortable: true
}
]
const columns1 = [
{
field: 'id',
label: 'ID'
},
{
field: 'name',
label: ''
},
{
field: 'amount1',
label: '1'
},
{
field: 'amount2',
label: '2'
},
{
field: 'amount3',
label: '4'
}
]
const tableData = [
{
id: '12987122',
name: '',
amount1: '234',
amount2: '3.2',
amount3: 10
},
{
id: '12987123',
name: '',
amount1: '165',
amount2: '4.43',
amount3: 12
},
{
id: '12987124',
name: '',
amount1: '324',
amount2: '1.9',
amount3: 9
},
{
id: '12987125',
name: '',
amount1: '621',
amount2: '2.2',
amount3: 17
},
{
id: '12987126',
name: '',
amount1: '539',
amount2: '4.1',
amount3: 15
}
]
const loading = ref<boolean>(true)
setTimeout(() => {
loading.value = false
}, 1000)
function getSummaries(param: any) {
const { columns, data } = param
const sums: any[] = []
columns.forEach((column: any, index: number) => {
if (index === 0) {
sums[index] = '总价'
return
}
const values = data.map((item: any) => Number(item[column.property]))
if (!values.every((value: number) => isNaN(value))) {
sums[index] = values.reduce((prev: number, curr: number) => {
const value = Number(curr)
if (!isNaN(value)) {
return prev + curr
} else {
return prev
}
}, 0)
sums[index] += ' 元'
} else {
sums[index] = 'N/A'
}
})
return sums
}
</script>
<style></style>