Element UI Table 尾部统计示例
时间: 2024-04-02 17:34:12 浏览: 15
好的,以下是一个 Element UI Table 尾部统计的示例。
```html
<template>
<el-table :data="tableData" show-summary>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄" :summary-method="calculateSummary"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 18, address: '北京市海淀区' },
{ name: '李四', age: 22, address: '上海市浦东新区' },
{ name: '王五', age: 25, address: '广州市天河区' },
{ name: '赵六', age: 30, address: '深圳市南山区' }
]
};
},
methods: {
calculateSummary(data) {
const sum = data.reduce((total, current) => total + current.age, 0);
const avg = (sum / data.length).toFixed(2);
return { age: `总计:${sum},平均值:${avg}` };
}
}
};
</script>
```
在上面的示例中,我们在 `el-table` 元素上设置了 `show-summary` 属性,启用了尾部统计功能。在 `el-table-column` 元素上,我们设置了需要统计的列的 `prop` 属性,以及 `summary-method` 属性,指定了一个自定义的计算函数 `calculateSummary`。
在 `calculateSummary` 函数中,我们使用了数组的 `reduce` 方法来计算该列的总和,然后除以数组长度计算平均值,并将这两个值返回为一个对象,属性名为该列的 `prop` 值,属性值为统计结果的字符串。
这样,我们就可以在表格的尾部看到该列的总计和平均值了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)