ant design vue table组件表头计算此列数据总和

时间: 2023-09-09 07:07:25 浏览: 58
Ant Design Vue 的 Table 组件提供了 `customRow` 和 `customHeaderRow` 属性,可以自定义每行和表头的渲染。我们可以利用这个特性,在表头最后一列加上一个求和单元格,并在每一行的对应列单元格中填入数据。 具体实现步骤如下: 1. 在表头的最后一列加上一个求和列,代码如下: ```html <a-table-column title="总计" dataIndex="total" key="total" customHeaderRow={(columns, index) => { return { on: {}, props: { colSpan: columns.length - 1, }, }; }}> <template slot-scope="text"> {{ text }} </template> </a-table-column> ``` 这里使用了 `customHeaderRow` 属性,将求和列合并到表头的最后一列,并且不设置 `dataIndex` 和 `key` 属性,以避免它被渲染为真实数据的列。 2. 在每一行的对应列单元格中填入数据,并且在最后一列单元格中计算总和,代码如下: ```html <a-table-column title="数量" dataIndex="quantity" key="quantity" customRow={(record, index) => { return { on: {}, props: {}, children: record.quantity, onCell: () => { return { style: { textAlign: 'right', }, }; }, }; }} /> <a-table-column title="单价" dataIndex="price" key="price" customRow={(record, index) => { return { on: {}, props: {}, children: record.price, onCell: () => { return { style: { textAlign: 'right', }, }; }, }; }} /> <a-table-column title="小计" key="subtotal" customRow={(record, index) => { const subtotal = record.quantity * record.price; if (index === dataSource.length - 1) { // 计算总和 let sum = 0; dataSource.forEach((item) => { sum += item.quantity * item.price; }); return { on: {}, props: {}, children: sum.toFixed(2), onCell: () => { return { style: { textAlign: 'right', fontWeight: 'bold', }, }; }, }; } else { return { on: {}, props: {}, children: subtotal.toFixed(2), onCell: () => { return { style: { textAlign: 'right', }, }; }, }; } }} /> ``` 这里使用了 `customRow` 属性,自定义每一行的渲染。在对应列的单元格中填入数据,同时在最后一列单元格中计算总和。 完整的示例代码如下: ```html <template> <a-table :columns="columns" :dataSource="dataSource" bordered /> </template> <script> export default { data() { return { columns: [ { title: '名称', dataIndex: 'name', key: 'name', }, { title: '数量', dataIndex: 'quantity', key: 'quantity', customRow: (record, index) => { return { on: {}, props: {}, children: record.quantity, onCell: () => { return { style: { textAlign: 'right', }, }; }, }; }, }, { title: '单价', dataIndex: 'price', key: 'price', customRow: (record, index) => { return { on: {}, props: {}, children: record.price, onCell: () => { return { style: { textAlign: 'right', }, }; }, }; }, }, { title: '小计', key: 'subtotal', customRow: (record, index) => { const subtotal = record.quantity * record.price; if (index === this.dataSource.length - 1) { // 计算总和 let sum = 0; this.dataSource.forEach((item) => { sum += item.quantity * item.price; }); return { on: {}, props: {}, children: sum.toFixed(2), onCell: () => { return { style: { textAlign: 'right', fontWeight: 'bold', }, }; }, }; } else { return { on: {}, props: {}, children: subtotal.toFixed(2), onCell: () => { return { style: { textAlign: 'right', }, }; }, }; } }, }, ], dataSource: [ { name: '商品 1', quantity: 2, price: 10.5, }, { name: '商品 2', quantity: 3, price: 5.2, }, { name: '商品 3', quantity: 1, price: 6.8, }, ], }; }, }; </script> ```

相关推荐

最新推荐

recommend-type

Vue多种方法实现表头和首列固定的示例代码

有时表格太大,滚动时信息查看不方便,需要对表格进行全局表头、首列固定, 上效果: 一、创建多个表格进行覆盖 思路:当页面滚动到临界值时,出现固定表头、首列 先创建一个活动表格 &lt;!DOCTYPE html&gt; &...
recommend-type

vue中获取滚动table的可视页面宽度调整表头与列对齐(每列宽度不都相同)

主要介绍了vue中获取滚动table的可视页面宽度,调整表头与列对齐(每列宽度不都相同),需要的朋友可以参考下
recommend-type

vue中如何让子组件修改父组件数据

主要介绍了vue中子组件修改父组件数据的相关资料,文中介绍了vue中watch的认识,关于子组件修改父组件属性认识,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

vue父组件向子组件(props)传递数据的方法

vue页面结构 在做项目的时候常常有这样的一个情况,这个页面的...其实vue是有三种方式可以组件之间传递数据(props,组件通信,slot),这次就说第一种方式如下: a父组件内容: 引入b子组件import b form ‘b.vue’
recommend-type

vue element 中的table动态渲染实现(动态表头)

主要介绍了vue element 中的table动态渲染实现(动态表头),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。