Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to Keep Rows Expanded in Grouped Tables #704

Merged
merged 9 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions src/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
:collapsable="groupOptions.collapsable"
:collect-formatted="collectFormatted"
:formatted-row="formattedRow"
:class="getRowStyleClass(headerRow)"
:get-classes="getClasses"
:full-colspan="fullColspan"
>
Expand Down Expand Up @@ -345,6 +346,9 @@ export default {
default() {
return {
enabled: false,
collapsable: false,
maintainExpanded: false,
rowKey: null
};
},
},
Expand Down Expand Up @@ -419,6 +423,9 @@ export default {
selectionText: 'rows selected',
clearSelectionText: 'clear',

// keys for rows that are currently expanded
expandedRowKeys: new Set(),

// internal sort options
sortable: true,
defaultSortBy: null,
Expand Down Expand Up @@ -894,17 +901,26 @@ export default {
if (headerRow) {
this.$set(headerRow, 'vgtIsExpanded', !headerRow.vgtIsExpanded);
}
if (this.groupOptions.maintainExpanded && headerRow.vgtIsExpanded) {
this.expandedRowKeys.add(headerRow[this.groupOptions.rowKey]);
} else {
this.expandedRowKeys.delete(headerRow[this.groupOptions.rowKey]);
}
},

expandAll() {
this.filteredRows.forEach((row) => {
this.$set(row, 'vgtIsExpanded', true);
if (this.groupOptions.maintainExpanded) {
this.expandedRowKeys.add(row[this.groupOptions.rowKey]);
}
});
},

collapseAll() {
this.filteredRows.forEach((row) => {
this.$set(row, 'vgtIsExpanded', false);
this.expandedRowKeys.clear();
});
},

Expand Down Expand Up @@ -1324,6 +1340,12 @@ export default {
handleGrouped(originalRows) {
each(originalRows, (headerRow, i) => {
headerRow.vgt_header_id = i;
if (
this.groupOptions.maintainExpanded &&
this.expandedRowKeys.has(headerRow[this.groupOptions.rowKey])
) {
this.$set(headerRow, 'vgtIsExpanded', true);
}
each(headerRow.children, (childRow) => {
childRow.vgt_id = i;
});
Expand Down
19 changes: 19 additions & 0 deletions vp-docs/guide/advanced/grouped-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,24 @@ this.$refs.myCustomTable.expandAll();
this.$refs.myCustomTable.collapseAll();
```

### Maintaining Expanded Rows

If you make alterations to the data being passed into the rows on the table, such as adding or removing a row, all of your groupings will be collapsed by default.
Inside of `groupOptions`, add an attribute of `maintainExpanded: true` so that the expanded rows will stay expanded after
changes to the row data. Additionally you must provide the `rowKey` attribute as a string. The `rowKey` shoud be a field on the row
that can be used for a unique identifier, such as 'id'.

```vue
<vue-good-table
:columns="columns"
:rows="rows"
:groupOptions="{
enabled: true,
collapsable: true,
maintainExpanded: true,
rowKey: 'id'
}"
/>
```
* **Live Demo:** https://jsfiddle.net/nb6fcqs7