Is there a way to prioritize items in a task list based on both their date of addition and their listed priority? I've managed to sort the tasks by date, but I'm looking for a solution that will organize items with the 'HIGH' priority at the top of the list.
UPDATE: Ideally, I want the sorting to occur based on the 'HIGH' property when it is selected from the 'Sort By' dropdown menu located above the task list.
Codepen: http://codepen.io/anon/pen/ryyQzy.
HTML:
<md-list class="md-padding">
<div layout="row">
<md-input-container class="md-block" flex="70">
<input type="text" ng-model="search" name="search" placeholder="Search by Task Name"></input>
</md-input-container>
<md-input-container flex="30">
<md-select name="priority" ng-model="task.priority" placeholder="Sort By">
<md-option value="Low">Newest First</md-option>
<md-option value="HIGH">High Priority First</md-option>
</md-select>
</md-input-container>
</div>
<md-subheader class="md-no-sticky">Current Tasks ({{taskList.length}})</md-subheader>
<md-list-item class="md-3-line" ng-repeat="task in taskList | orderBy:sortByNewest | filter:search">
<div class="md-list-item-text" layout="column">
<p>{{task.name}}</p>
<p>Priority: {{$location.path('/')}}</p>
<span class="weak">Added {{task.addedOn | date: 'medium'}}</span>
</div>
<md-checkbox class="md-secondary" ng-model="task.completed"></md-checkbox>
<md-divider ng-if="!$last"></md-divider>
</md-list-item>
</md-list>
JS:
var app = angular.module('todoApp', ['ngMaterial']);
function menuController ($scope, $mdDialog) {
var originatorEv;
this.openMenu = function($mdOpenMenu, ev) {
originatorEv = ev;
$mdOpenMenu(ev);
};
};
app.controller('todoController', function($scope, $mdDialog, $mdToast, $filter) {
$scope.sortByNewest = '-addedOn';
//$scope.sortByPriority = ' ';
$scope.taskList = [
{ name: 'Task 1', priority: 'Low', completed: false, addedOn: 1488722128000 },
{ name: 'Task 2', priority: 'HIGH', completed: false, addedOn: 1488722128000 },
];
$scope.addTask = function() {
if (angular.isUndefined($scope.taskName) || $scope.taskName.length === 0) {
var alert = $mdDialog.alert()
.parent(angular.element(document.querySelector('#popupContainer')))
.clickOutsideToClose(true)
.title('Error')
.textContent('You must enter a task name and select a priority level')
.ok('Close');
$mdDialog.show( alert )
.finally(function() {
alert = undefined;
});
}
else {
$scope.taskList.push({name: $scope.taskName, priority: $scope.task.priority, addedOn: Date.now()});
$scope.taskName = "";
$scope.task.priority = "";
var pinTo = $scope.getToastPosition();
$mdToast.show (
$mdToast.simple()
.textContent('Task Added')
.position(pinTo)
.hideDelay(3000)
)
}
};
$scope.selectAll = function() {
angular.forEach($scope.taskList, function(task) {
task.completed = true;
});
};
$scope.selectNone = function() {
angular.forEach($scope.taskList, function(task) {
task.completed = false;
});
};
$scope.delete = function(ev) {
var completedTasks = $filter('filter')($scope.taskList, { completed: true}, true);
if (completedTasks.length > 0) {
console.log('show dialog box to confirm');
var confirm = $mdDialog.confirm()
.title ('Are you sure you want to delete the selected tasks?')
.textContent ('Deleted tasks can\'t be recovered.')
.targetEvent (ev)
.ok ('Confirm')
.cancel ('Cancel')
clickOutsideToClose: false;
$mdDialog.show(confirm).then(function() {
if (completedTasks.length > 1) {
var pinTo = $scope.getToastPosition();
$mdToast.show (
$mdToast.simple()
.textContent('Tasks Deleted')
.position(pinTo)
.hideDelay(3000)
)
}
else {
var pinTo = $scope.getToastPosition();
$mdToast.show (
$mdToast.simple()
.textContent('Task Deleted')
.position(pinTo)
.hideDelay(3000)
)
}
$scope.status = 'Tasks Deleted';
var i = $scope.taskList.length;
while (i--) {
var task = $scope.taskList[i];
if(task.completed) {
$scope.taskList.splice(i, 1);
}
}
},
function() {
$scope.status = 'Deletion Cancelled';
});
}
else {
$mdDialog.show(
$mdDialog.alert()
.parent(angular.element(document.querySelector('#popupContainer')))
.clickOutsideToClose(true)
.title('Error')
.textContent('You must select at least one task to delete.')
.ok('Close')
.targetEvent(ev)
);
}
};
function DialogController($scope, $mdDialog) {
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.answer = function(answer) {
$mdDialog.hide(answer);
};
};
var last = {
bottom: false,
top: true,
left: false,
right: true
};
$scope.toastPosition = angular.extend({},last);
$scope.getToastPosition = function() {
sanitizePosition();
return Object.keys($scope.toastPosition)
.filter(function(pos) { return $scope.toastPosition[pos]; })
.join(' ');
};
function sanitizePosition() {
var current = $scope.toastPosition;
if ( current.bottom && last.top ) current.top = false;
if ( current.top && last.bottom ) current.bottom = false;
if ( current.right && last.left ) current.left = false;
if ( current.left && last.right ) current.right = false;
last = angular.extend({},current);
};
});
app.controller('toastController', function($scope, $mdToast) {
$scope.closeToast = function() {
$mdToast.hide();
}
});