|
@@ -154,12 +154,68 @@
|
|
|
address: 'No. 189, Grove St, Los Angeles',
|
|
|
zip: 'CA 90036'
|
|
|
}],
|
|
|
+ tableData6: [{
|
|
|
+ id: '12987122',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '234',
|
|
|
+ amount2: '3.2',
|
|
|
+ amount3: 10
|
|
|
+ }, {
|
|
|
+ id: '12987123',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '165',
|
|
|
+ amount2: '4.43',
|
|
|
+ amount3: 12
|
|
|
+ }, {
|
|
|
+ id: '12987124',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '324',
|
|
|
+ amount2: '1.9',
|
|
|
+ amount3: 9
|
|
|
+ }, {
|
|
|
+ id: '12987125',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '621',
|
|
|
+ amount2: '2.2',
|
|
|
+ amount3: 17
|
|
|
+ }, {
|
|
|
+ id: '12987126',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '539',
|
|
|
+ amount2: '4.1',
|
|
|
+ amount3: 15
|
|
|
+ }],
|
|
|
currentRow: null,
|
|
|
multipleSelection: []
|
|
|
};
|
|
|
},
|
|
|
|
|
|
methods: {
|
|
|
+ getSummaries(param) {
|
|
|
+ const { columns, data } = param;
|
|
|
+ const sums = [];
|
|
|
+ columns.forEach((column, index) => {
|
|
|
+ if (index === 0) {
|
|
|
+ sums[index] = 'Total Cost';
|
|
|
+ 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);
|
|
|
+ } else {
|
|
|
+ sums[index] = 'N/A';
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return sums;
|
|
|
+ },
|
|
|
setCurrent(row) {
|
|
|
this.$refs.singleTable.setCurrentRow(row);
|
|
|
},
|
|
@@ -1470,6 +1526,143 @@ When the row content is too long and you do not want to display the horizontal s
|
|
|
```
|
|
|
:::
|
|
|
|
|
|
+### Summary row
|
|
|
+
|
|
|
+For table of numbers, you can add an extra row at the table footer displaying each column's sum.
|
|
|
+:::demo You can add the summary row by setting `show-summary` to `true`. By default, for the summary row, the first column does not sum anything up but always displays 'Sum' (you can configure the displayed text using `sum-text`), while other columns sum every number in that column up and display them. You can of course define your own sum behaviour. To do so, pass a method to `summary-method`, which returns an array, and each element of the returned array will be displayed in the columns of the summary row. The second table of this example is a detailed demo.
|
|
|
+```html
|
|
|
+<template>
|
|
|
+ <el-table
|
|
|
+ :data="tableData6"
|
|
|
+ border
|
|
|
+ show-summary
|
|
|
+ style="width: 100%">
|
|
|
+ <el-table-column
|
|
|
+ prop="id"
|
|
|
+ label="ID"
|
|
|
+ width="180">
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ prop="name"
|
|
|
+ label="Name">
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ prop="amount1"
|
|
|
+ sortable
|
|
|
+ label="Amount 1">
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ prop="amount2"
|
|
|
+ sortable
|
|
|
+ label="Amount 2">
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ prop="amount3"
|
|
|
+ sortable
|
|
|
+ label="Amount 3">
|
|
|
+ </el-table-column>
|
|
|
+ </el-table>
|
|
|
+
|
|
|
+ <el-table
|
|
|
+ :data="tableData6"
|
|
|
+ border
|
|
|
+ height="200"
|
|
|
+ :summary-method="getSummaries"
|
|
|
+ show-summary
|
|
|
+ style="width: 100%; margin-top: 20px">
|
|
|
+ <el-table-column
|
|
|
+ prop="id"
|
|
|
+ label="ID"
|
|
|
+ width="180">
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ prop="name"
|
|
|
+ label="Name">
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ prop="amount1"
|
|
|
+ label="Cost 1 ($)">
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ prop="amount2"
|
|
|
+ label="Cost 2 ($)">
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ prop="amount3"
|
|
|
+ label="Cost 3 ($)">
|
|
|
+ </el-table-column>
|
|
|
+ </el-table>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+ export default {
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ tableData6: [{
|
|
|
+ id: '12987122',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '234',
|
|
|
+ amount2: '3.2',
|
|
|
+ amount3: 10
|
|
|
+ }, {
|
|
|
+ id: '12987123',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '165',
|
|
|
+ amount2: '4.43',
|
|
|
+ amount3: 12
|
|
|
+ }, {
|
|
|
+ id: '12987124',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '324',
|
|
|
+ amount2: '1.9',
|
|
|
+ amount3: 9
|
|
|
+ }, {
|
|
|
+ id: '12987125',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '621',
|
|
|
+ amount2: '2.2',
|
|
|
+ amount3: 17
|
|
|
+ }, {
|
|
|
+ id: '12987126',
|
|
|
+ name: 'Tom',
|
|
|
+ amount1: '539',
|
|
|
+ amount2: '4.1',
|
|
|
+ amount3: 15
|
|
|
+ }]
|
|
|
+ };
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ getSummaries(param) {
|
|
|
+ const { columns, data } = param;
|
|
|
+ const sums = [];
|
|
|
+ columns.forEach((column, index) => {
|
|
|
+ if (index === 0) {
|
|
|
+ sums[index] = 'Total Cost';
|
|
|
+ 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);
|
|
|
+ } else {
|
|
|
+ sums[index] = 'N/A';
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return sums;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+</script>
|
|
|
+```
|
|
|
+:::
|
|
|
+
|
|
|
### Table Attributes
|
|
|
| Attribute | Description | Type | Accepted Values | Default |
|
|
|
|---------- |-------------- |---------- |-------------------------------- |-------- |
|
|
@@ -1490,6 +1683,9 @@ When the row content is too long and you do not want to display the horizontal s
|
|
|
| expand-row-keys | set expanded rows by this prop, prop's value is the keys of expand rows, you should set row-key before using this prop | Array | — | |
|
|
|
| default-sort | set the default sort column and order. property `prop` is used to set default sort column, property `order` is used to set default sort order | Object | `order`: ascending, descending | if `prop` is set, and `order` is not set, then `order` is default to ascending |
|
|
|
| tooltip-effect | tooltip `effect` property | String | dark/light | | dark |
|
|
|
+| show-summary | whether to display a summary row | Boolean | — | false |
|
|
|
+| sum-text | displayed text for the first column of summary row | String | — | Sum |
|
|
|
+| summary-method | custom summary method | Function({ columns, data }) | — | — |
|
|
|
|
|
|
### Table Events
|
|
|
| Event Name | Description | Parameters |
|
|
@@ -1518,6 +1714,11 @@ When the row content is too long and you do not want to display the horizontal s
|
|
|
| toggleRowSelection | used in multiple selection Table, toggle if a certain row is selected. With the second parameter, you can directly set if this row is selected | row, selected |
|
|
|
| setCurrentRow | used in single selection Table, set a certain row selected. If called without any parameter, it will clear selection. | row |
|
|
|
|
|
|
+### Table Slot
|
|
|
+| Name | Description |
|
|
|
+|------|--------|
|
|
|
+| append | Contents to be inserted after the last row. It is still nested inside the `<tbody>` tag. You may need this slot if you want to implement infinite scroll for the table. This slot will be displayed above the summary row if there is one. |
|
|
|
+
|
|
|
### Table-column Attributes
|
|
|
| Attribute | Description | Type | Accepted Values | Default |
|
|
|
|---------- |-------------- |---------- |-------------------------------- |-------- |
|