Created
January 9, 2015 00:22
-
-
Save JonathanGawrych/c858430f19d67eae079b to your computer and use it in GitHub Desktop.
Simple sort toggle to be used with angular's sortBy. Allows multilevel sorting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var defaultSortBy = 'lastName'; | |
| var sortBy = defaultSortBy.slice(); | |
| function toggleSortBy(sort) { | |
| // if we are using default, switch to array. | |
| if (typeof sortBy === 'string') | |
| sortBy = []; | |
| // if the value is in decending order | |
| if (sortBy.indexOf('-' + sort) !== -1) { | |
| // remove it | |
| sortBy.splice(sortBy.indexOf('-' + sort), 1); | |
| // if our array is empty, switch to default | |
| if (sortBy.length === 0) | |
| sortBy = defaultSortBy.slice(); | |
| // if the value is in accending order | |
| } else if (sortBy.indexOf(sort) !== -1) { | |
| // change it to decending order | |
| sortBy[sortBy.indexOf(sort)] = '-' + sort; | |
| // if the value is not present | |
| } else { | |
| // if we aren't multilevel sorting | |
| if (!sortMultiLevel) | |
| sortBy.length = 0; // clear the array | |
| // add it | |
| sortBy.push(sort); | |
| } | |
| } | |
| function isSortedBy(sort, reverse) { | |
| // if we are default, don't show any arrows | |
| if (typeof sortBy === 'string') | |
| return false; | |
| // return if sort (or if reversed, -sort) is in the array | |
| return sortBy.indexOf((reverse ? '-' : '') + sort) !== -1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment