Skip to content

Commit

Permalink
Introduce new column property firstSortType (#648)
Browse files Browse the repository at this point in the history
* Refactor sort module for readability

* Add firstSortType property for columns
 - with firstSortType we introduce the possibility to controll the sort order on the first sort(e.g. first click on the <th>) for this column
  • Loading branch information
s-slavchev authored Jun 23, 2020
1 parent c874ee0 commit 9152556
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
1 change: 1 addition & 0 deletions dev/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default {
label: 'Age',
field: 'age',
type: 'number',
firstSortType: 'desc',
filterOptions: {
enabled: true,
filterDropdownItems: [
Expand Down
49 changes: 21 additions & 28 deletions src/components/utils/sort.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
const DEFAULT_SORT_TYPE = 'asc';

function getColumnFirstSortType(column) {
return column.firstSortType || DEFAULT_SORT_TYPE;
}

function getCurrentPrimarySort(sortArray, column) {
return ( sortArray.length === 1 && sortArray[0].field === column.field )
? sortArray[0].type
: undefined;
}

function getNextSort(currentSort) {
if (currentSort === 'asc') return 'desc';
// if (currentSort === 'desc') return null;
return 'asc';
return (currentSort === 'asc')
? 'desc'
: DEFAULT_SORT_TYPE;
}

function getIndex(sortArray, column) {
Expand All @@ -13,40 +24,22 @@ function getIndex(sortArray, column) {
}

exports.primarySort = (sortArray, column) => {
if (sortArray.length
&& sortArray.length === 1
&& sortArray[0].field === column.field) {
const type = getNextSort(sortArray[0].type);
if (type) {
sortArray[0].type = getNextSort(sortArray[0].type);
} else {
sortArray = [];
}
} else {
sortArray = [{
field: column.field,
type: 'asc',
}];
}
return sortArray;
const currentPrimarySort = getCurrentPrimarySort(sortArray, column);
return [{
field: column.field,
type: currentPrimarySort ? getNextSort(currentPrimarySort) : getColumnFirstSortType(column),
}];
};

exports.secondarySort = (sortArray, column) => {
//* this means that primary sort exists, we're
//* just adding a secondary sort
const index = getIndex(sortArray, column);
if (index === -1) {
sortArray.push({
field: column.field,
type: 'asc',
type: getColumnFirstSortType(column),
});
} else {
const type = getNextSort(sortArray[index].type);
if (type) {
sortArray[index].type = type;
} else {
sortArray.splice(index, 1);
}
sortArray[index].type = getNextSort(sortArray[index].type);
}
return sortArray;
};
21 changes: 21 additions & 0 deletions vp-docs/guide/configuration/column-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ columns: [
]
```

## firstSortType

type `String (default: 'asc')`

controls the first sort type when sorting by the column. If you want the first sort type for this column to be descending, set this property to 'desc'. Possible values:
* _asc_ - the initial sort will be ascending
* _desc_ - the initial sort will be descending


```javascript
columns: [
{
label: 'name',
field: 'user_name',
sortable: true,
firstSortType: 'desc'
},
// ...
]
```

## sortFn

type `Function`
Expand Down

0 comments on commit 9152556

Please sign in to comment.