Skip to content

Commit

Permalink
ability to disable multiple column sort. fixes #791
Browse files Browse the repository at this point in the history
  • Loading branch information
xaksis committed Mar 2, 2021
1 parent 20068fc commit 908add4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions dev/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
styleClass="vgt-table bordered"
:sort-options="{
enabled: true,
multipleColumns: false,
}"
:search-options="{
enabled: true,
Expand Down
10 changes: 9 additions & 1 deletion src/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
:all-selected-indeterminate="allSelectedIndeterminate"
:mode="mode"
:sortable="sortable"
:multiple-column-sort="multipleColumnSort"
:typed-columns="typedColumns"
:getClasses="getClasses"
:searchEnabled="searchEnabled"
Expand Down Expand Up @@ -145,6 +146,7 @@
:all-selected-indeterminate="allSelectedIndeterminate"
:mode="mode"
:sortable="sortable"
:multiple-column-sort="multipleColumnSort"
:typed-columns="typedColumns"
:getClasses="getClasses"
:searchEnabled="searchEnabled"
Expand Down Expand Up @@ -407,6 +409,7 @@ export default {
default() {
return {
enabled: true,
multipleColumns: true,
initialSortBy: {},
};
},
Expand Down Expand Up @@ -470,6 +473,7 @@ export default {
// internal sort options
sortable: true,
defaultSortBy: null,
multipleColumnSort: true,
// internal search options
searchEnabled: false,
Expand Down Expand Up @@ -1577,12 +1581,16 @@ export default {
},
initializeSort() {
const { enabled, initialSortBy } = this.sortOptions;
const { enabled, initialSortBy, multipleColumns } = this.sortOptions;
if (typeof enabled === 'boolean') {
this.sortable = enabled;
}
if (typeof multipleColumns === 'boolean') {
this.multipleColumnSort = multipleColumns;
}
//* initialSortBy can be an array or an object
if (typeof initialSortBy === 'object') {
const ref = this.fixedHeader
Expand Down
16 changes: 5 additions & 11 deletions src/components/VgtTableHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,11 @@ export default {
sortable: {
type: Boolean,
},
// sortColumn: {
// type: Number,
// },
// sortType: {
// type: String,
// },
multipleColumnSort: {
type: Boolean,
default: true,
},
// utility functions
// isSortableColumn: {
// type: Function,
// },
getClasses: {
type: Function,
},
Expand Down Expand Up @@ -163,7 +157,7 @@ export default {
//* if column is not sortable, return right here
if (!this.isSortableColumn(column)) return;
if (e.shiftKey) {
if (e.shiftKey && this.multipleColumnSort) {
this.sorts = secondarySort(this.sorts, column);
} else {
this.sorts = primarySort(this.sorts, column);
Expand Down
24 changes: 19 additions & 5 deletions src/components/utils/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,27 @@ function getCurrentPrimarySort(sortArray, column) {
: undefined;
}

function getNextSort(currentSort) {
if (currentSort === SORT_TYPES.Ascending) {
function getNextSort(currentSort, column) {
if (SORT_TYPES.Descending === getColumnFirstSortType(column)
&& currentSort === SORT_TYPES.Ascending) {
return SORT_TYPES.None
} else if (currentSort === SORT_TYPES.Ascending) {
return SORT_TYPES.Descending;
}
if (SORT_TYPES.Descending === getColumnFirstSortType(column)
&& currentSort === SORT_TYPES.Descending) {
return SORT_TYPES.Ascending;
} else if (currentSort === SORT_TYPES.Descending) {
return SORT_TYPES.None;
}
return SORT_TYPES.Ascending;

if (SORT_TYPES.Descending === getColumnFirstSortType(column)
&& currentSort === SORT_TYPES.None) {
return SORT_TYPES.Descending;
} else {
return SORT_TYPES.Ascending;
}

}

function getIndex(sortArray, column) {
Expand All @@ -31,7 +45,7 @@ function getIndex(sortArray, column) {

const primarySort = (sortArray, column) => {
const currentPrimarySort = getCurrentPrimarySort(sortArray, column);
const nextPrimarySort = getNextSort(currentPrimarySort);
const nextPrimarySort = getNextSort(currentPrimarySort, column);
return [{
field: column.field,
type: currentPrimarySort ? nextPrimarySort : getColumnFirstSortType(column),
Expand All @@ -46,7 +60,7 @@ const secondarySort = (sortArray, column) => {
type: getColumnFirstSortType(column),
});
} else {
sortArray[index].type = getNextSort(sortArray[index].type);
sortArray[index].type = getNextSort(sortArray[index].type, column);
}
return sortArray;
};
Expand Down
12 changes: 8 additions & 4 deletions vp-docs/guide/configuration/sort-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ Allows specifying a default sort for the table on wakeup. Both **field** and **t
</vue-good-table>
```

## Multiple Column Sort
## multipleColumns

type: `Boolean (default: true)`

Enable/disable multiple column sort. Users can shift-click on multiple columns to sort by multiple columns. The first column in the array gets primary sort.

```html
<vue-good-table
:columns="columns"
:rows="rows"
:sort-options="{
enabled: true,
multipleColumns: true,
initialSortBy: [
{field: 'name', type: 'asc'},
{field: 'age', type: 'desc'}
],
}">
</vue-good-table>
```

Users can shift-click on multiple columns to sort by multiple columns. The first column in the array gets primary sort.
```

0 comments on commit 908add4

Please sign in to comment.