<template>
	<el-card class="box-card">
		<!-- 只合计不做其他处理 -->
		<h2>只合计不做其他处理</h2>
		<el-table :data="tableData6" border show-summary style="width: 100%">
			<el-table-column prop="id" label="ID" width="180" align="center">
			</el-table-column>
			<el-table-column prop="name" label="产品" align="center">
			</el-table-column>
			<el-table-column prop="number" label="数量" align="center">
			</el-table-column>
			<el-table-column prop="price" label="价格" align="center">
			</el-table-column>
			<el-table-column prop="profit" label="利润率" align="center">
			</el-table-column>
		</el-table>

		<!-- 简单求和实例处理 -->
		<h2 style="margin-top:100px">简单求和实例处理</h2>
		<el-table :data="tableData6" border :summary-method="getSummaries01" show-summary style="width: 100%;">
			<el-table-column prop="id" label="ID" width="180" align="center">
			</el-table-column>
			<el-table-column prop="name" label="产品" align="center">
			</el-table-column>
			<el-table-column prop="number" label="数量" align="center">
			</el-table-column>
			<el-table-column prop="price" label="价格" align="center">
			</el-table-column>
			<el-table-column prop="profit" label="利润率" align="center">
			</el-table-column>
		</el-table>

		<!-- 指定列求和实例处理 -->
		<h2 style="margin-top:100px">指定列求和实例处理</h2>
		<el-table :data="tableData6" border :summary-method="getSummaries02" show-summary style="width: 100%;">
			<el-table-column prop="id" label="ID" width="180" align="center">
			</el-table-column>
			<el-table-column prop="name" label="产品" align="center">
			</el-table-column>
			<el-table-column prop="number" label="数量" align="center">
			</el-table-column>
			<el-table-column prop="price" label="价格" align="center">
				<template slot-scope="scope">
					{{$utils.formSum(scope.row.price, 2, '.', ',')}}
				</template>
			</el-table-column>
			<el-table-column prop="profit" label="利润率" align="center">
				<template slot-scope="scope">
					{{Number(scope.row.profit).toFixed(2)}} %
				</template>
			</el-table-column>
		</el-table>
	</el-card>
</template>

<script>
	export default {
		data() {
			return {
				tableData6: [{
					id: '12987122',
					name: '袜子',
					number: '234',
					price: '3',
					profit: '10.9'
				}, {
					id: '12987123',
					name: '鞋子',
					number: '165',
					price: '343.8',
					profit: '12.00'
				}, {
					id: '12987124',
					name: '裤子',
					number: '324',
					price: '249.0',
					profit: '9'
				}, {
					id: '12987125',
					name: '腰带',
					number: '621',
					price: '342.0',
					profit: '17.00'
				}, {
					id: '12987126',
					name: '手镯',
					number: '539',
					price: '43421',
					profit: '153333'
				}]
			};
		},
		methods: {
			//格式化数字,格式化金额
			// formSum: function(number, decimals, dec_point, thousands_sep) {
			//   /*   
			//     * 参数说明:

			//     * number:要格式化的数字

			//     * decimals:保留几位小数

			//     * dec_point:小数点符号

			//     * thousands_sep:千分位符号

			//     * */
			//   number = (number + '').replace(/[^0-9+-Ee.]/g, '');
			//   var n = !isFinite(+number) ? 0 : +number,
			//     prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
			//     sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
			//     dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
			//     s = '',
			//     toFixedFix = function(n, prec) {
			//       var k = Math.pow(10, prec);
			//       return '' + Math.ceil(n * k) / k;
			//     };
			//   s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
			//   var re = /(-?\d+)(\d{3})/;
			//   while (re.test(s[0])) {
			//     s[0] = s[0].replace(re, "$1" + sep + "$2");
			//   }
			//   if ((s[1] || '').length < prec) {
			//     s[1] = s[1] || '';
			//     s[1] += new Array(prec - s[1].length + 1).join('0');
			//   }
			//   return s.join(dec);
			// }
			/**
			 * 将show-summary设置为true就会在表格尾部展示合计行。
			 * 默认情况下,对于合计行,第一列不进行数据求合操作,
			 * 而是显示「合计」二字(可通过sum-text配置),
			 * 其余列会将本列所有数值进行求合操作,并显示出来。
			 * 当然,你也可以定义自己的合计逻辑。
			 * 使用summary-method并传入一个方法,返回一个数组,
			 * 这个数组中的各项就会显示在合计行的各列中,具体可以参考本例中的第二个表格。
			 */

			//简单求和实例处理
			getSummaries01(param) {
				const {
					columns,
					data
				} = param;
				const sums = [];
				columns.forEach((column, index) => {
					if (index === 0) {
						sums[index] = '总价';
						return;
					}
					const values = data.map(item => Number(item[column.property]));
					if (!values.every(value => isNaN(value))) {
						sums[index] = values.reduce((prev, curr) => {
							const value = Number(curr);
							if (!isNaN(value)) {
								return prev + curr;
							} else {
								return prev;
							}
						}, 0);
						sums[index] += ' 元';
					} else {
						sums[index] = 'N/A';
					}
				});
				return sums;
			},



			//指定列求和实例处理
			getSummaries02(param) {
				const {
					columns,
					data
				} = param;
				const sums = [];
				columns.forEach((column, index) => {
					if (index === 0) {
						sums[index] = '总价';
					} else if (index === 3) { //对价格做处理:保留两位小数
						const values = data.map(item => Number(item[column.property]));
						if (!values.every(value => isNaN(value))) {
							sums[index] = values.reduce((prev, curr) => {
								const value = Number(curr);
								if (!isNaN(value)) {
									return prev + curr;
								} else {
									return prev;
								}
							}, 0);
							sums[index] = this.$utils.formSum(sums[index], 2, '.', ',');
						} else {
							sums[index] = '/';
						}
					} else if (index === 4) { //对利润率做处理:保留两位小数
						const values = data.map(item => Number(item[column.property]));
						if (!values.every(value => isNaN(value))) {
							sums[index] = values.reduce((prev, curr) => {
								const value = Number(curr);
								if (!isNaN(value)) {
									return prev + curr;
								} else {
									return prev;
								}
							}, 0);
							sums[index] = sums[index].toFixed(2);
							sums[index] += ' %';
						} else {
							sums[index] = '/';
						}
					} else {
						const values = data.map(item => Number(item[column.property]));
						if (!values.every(value => isNaN(value))) {
							sums[index] = values.reduce((prev, curr) => {
								const value = Number(curr);
								if (!isNaN(value)) {
									return prev + curr;
								} else {
									return prev;
								}
							}, 0);
						} else {
							sums[index] = '/';
						}
					}
				});
				return sums;
			}
		}
	};
</script>

Logo

前往低代码交流专区

更多推荐