Skip to content
This repository was archived by the owner on Oct 24, 2018. It is now read-only.

Add slot support for footer #73

Merged
merged 2 commits into from
Nov 2, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Pass row data information as tfoot slot scope
Update documentation and demo examples with `tfoot` slot usage.

Note: with later versions of `vue` we'll need to change the attribute
name from `scope` to `slot-scope`
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

Sam Tsai committed Oct 26, 2017
commit ce13dc9a310540ec77bfedc642ff79ca3d55af42
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

All notable changes to `vue-table-component` will be documented in this file

## 1.7.0 - 2017-10-26
- Added named slot `tfoot` to display table footer information, receives row data as scoped properties

## 1.6.1 - 2017-09-25
- Fixed a bug that didn't rerender the table when a column was changed

59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -226,7 +226,7 @@ Alternatively you can pass a function to the `formatter` prop. Here's an example
export default {
methods: {
formatter(value, rowProperties) {
return `Hi, I am ${value}`;
return `Hi, I am ${value}`;
},
},
}
@@ -235,6 +235,63 @@ export default {

This will display values `Hi, I am John` and `Hi, I am Paul`.

## Adding table footer `<tfoot>` information

Sometimes it can be useful to add information to the bottom of the table like summary data.
A slot named `tfoot` is available and it receives all of the `rows` data to do calculations on the fly or you can show data directly from whatever is available in the parent scope.

```html
<table-component
:data="[{ firstName: 'John', songs: 72 },{ firstName: 'Paul', songs: 70 }]">
<table-column show="firstName" label="First name"></table-column>
<table-column show="songs" label="Songs" data-type="numeric"></table-column>
<template slot="tfoot" scope="{ rows }">
<tr>
<th>Total Songs:</th>
<th>{{ rows.reduce((sum, value) => { return sum + value.data.songs; }, 0) }}</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>
</template>
</table-component>
```

OR

```vue
<template>
<table-component
:data="tableData">
<table-column show="firstName" label="First name"></table-column>
<table-column show="songs" label="Songs" data-type="numeric"></table-column>
<template slot="tfoot">
<tr>
<th>Total Songs:</th>
<th>{{ totalSongs }}</th>
</tr>
</template>
</table-component>
</template>
<script>
export default {
computed: {
totalSongs () {
return this.tableData.reduce(sum, value => {
return sum + value.songs;
}, 0);
}
},
data () {
return {
tableData: [{ firstName: 'John', songs: 72 },{ firstName: 'Paul', songs: 70 }]
}
}
}
</script>
```

Note: `rows` slot scope data includes more information gathered by the Table Component (e.g. `columns`) and `rows.data` is where the original `data` information is located.

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
4 changes: 2 additions & 2 deletions __tests__/components/table-component/TableComponent.test.js
Original file line number Diff line number Diff line change
@@ -90,10 +90,10 @@ describe('TableComponent', () => {
<table-component
:data="[{ firstName: 'John' },{ firstName: 'Paul' }]">
<table-column show="firstName" label="First name"></table-column>
<template slot="tfoot">
<template slot="tfoot" scope="{ rows }">
<tr>
<td>Name count:</td>
<td>2</td>
<td>{{ rows.length }}</td>
</tr>
</template>
</table-component>
2,673 changes: 1,233 additions & 1,440 deletions docs/build/app.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -153,13 +153,13 @@ <h1 class="page-title">
</a>
</template>
</table-column>
<template slot="tfoot">
<template slot="tfoot" scope="{ rows }">
<tr>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>Total Songs:</th>
<th>{{ rows.reduce((sum, value) => { return sum + value.data.songs; }, 0) }}</th>
<th>&nbsp;</th>
<th>Total:</th>
<th>166</th>
<th>&nbsp;</th>
</tr>
</template>
2 changes: 1 addition & 1 deletion src/components/TableComponent.vue
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@
></table-row>
</tbody>
<tfoot>
<slot name="tfoot"></slot>
<slot name="tfoot" :rows="rows"></slot>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm passing rows as it seems to be the most useful information, but please suggest changes if there's a better property.

</tfoot>
</table>
</div>