vue表单增加合计,有两种方式:
第一种前端获取所有数据:
将 show-summary 设置为true就会在表格尾部展示合计行。 默认情况下,对于合计行,第一列不进行数据求合操作,而是显示「合计」二字(可通过sum-text配置),其余列会将本列所有数值进行求合操作,并显示出来。
<el-table :data="tableData" border show-summary style="width: 100%">
<el-table-column prop="id" label="ID" width="180" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="amount1" sortable label="Amount 1" />
<el-table-column prop="amount2" sortable label="Amount 2" />
<el-table-column prop="amount3" sortable label="Amount 3" />
</el-table>
优点:方法简单
缺点:只能统计当前页的数据,所有数据合计无法做到
第二种方式,后端访问统计数据,前端使用getSummaries展示到表单
先看效果:
<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" show-summary :summary-method="getSummaries">
<el-table-column label="账单日期" align="center" prop="billingDate" sortable width="120px"/>
<el-table-column label="门店名称" align="center" prop="storeName" sortable width="180px"/>
<el-table-column label="订单号" align="center" prop="orderNumber" sortable width="180px"/>
<el-table-column label="商家应收款" align="center" prop="merchantIncome" sortable />
<el-table-column label="打包袋" align="center" prop="packingBagFee" sortable />
</el-table>
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0)
const summary_data = ref() // 汇总数据
const getList = async () => {
loading.value = true
try {
const data = await SrSellOrderDetailCompleteApi.page(queryParams)
const summary = await SrSellOrderDetailCompleteApi.summary(queryParams)
list.value = data.list
total.value = data.total
summary_data.value = summary
} finally {
loading.value = false
}
}
const getSummaries = ({ columns }) => {
const sums = [];
columns.forEach((column, index) => {
if (index === 0) {
sums[index] = '合计';
return;
}
const prop = column.property;
if (summary_data.value && prop in summary_data.value) {
sums[index] = summary_data.value[prop];
} else {
sums[index] = 'N/A';
}
});
return sums;
};