Skip to content

Commit

Permalink
Option to Keep Rows Expanded in Grouped Tables (xaksis#704)
Browse files Browse the repository at this point in the history
* add row style class to header row
* add logic to maintain expanded rows
* add documentation
  • Loading branch information
robgaston1 authored and p0psicles committed Jun 24, 2020
1 parent 43f22f0 commit 3c9c8fe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
:collapsable="groupOptions.collapsable"
:collect-formatted="collectFormatted"
:formatted-row="formattedRow"
:class="getRowStyleClass(headerRow)"
:get-classes="getClasses"
:full-colspan="fullColspan"
:groupIndex="index"
Expand Down Expand Up @@ -353,6 +354,9 @@ export default {
default() {
return {
enabled: false,
collapsable: false,
maintainExpanded: false,
rowKey: null
};
},
},
Expand Down Expand Up @@ -428,6 +432,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 @@ -903,17 +910,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 @@ -1339,6 +1355,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

0 comments on commit 3c9c8fe

Please sign in to comment.