From 0f4b6dd80c7e2d4468025486e7b0dc58505125d4 Mon Sep 17 00:00:00 2001 From: thuva4 Date: Sat, 10 Mar 2018 00:10:46 +0530 Subject: Add sorting service to module Show the results in sorted order Provide options to sort the results by fields in pods and project page Change-Id: I9b73fbe98146d4bdf9edbb4a3ef3b3e0717500a7 qSigned-off-by: thuva4 --- testapi/opnfv_testapi/ui/app.js | 30 ++++++++++++++++++++++ .../ui/components/pods/podsController.js | 29 +++------------------ .../ui/components/projects/projects.html | 9 ++++++- .../ui/components/projects/projectsController.js | 11 ++++++-- 4 files changed, 51 insertions(+), 28 deletions(-) (limited to 'testapi') diff --git a/testapi/opnfv_testapi/ui/app.js b/testapi/opnfv_testapi/ui/app.js index a64bfcc..ada7577 100644 --- a/testapi/opnfv_testapi/ui/app.js +++ b/testapi/opnfv_testapi/ui/app.js @@ -32,6 +32,36 @@ this.filter = {}; }); + angular + .module('testapiApp') + .service('sortService', function(){ + + this.sortFunction = function(data, field, ascending){ + if(ascending){ + data.sort(function(a,b) { + if (a[field].toLowerCase() > b[field].toLowerCase()) { + return -1; + } + if (a[field].toLowerCase() < b[field].toLowerCase()) { + return 1; + } + return 0; + }); + }else{ + data.sort(function(a,b) { + if (a[field].toLowerCase() < b[field].toLowerCase()) { + return -1; + } + if (a[field].toLowerCase() > b[field].toLowerCase()) { + return 1; + } + return 0; + }); + } + return data + } + }); + angular .module('testapiApp') .directive('dynamicModel', ['$compile', '$parse', function ($compile, $parse) { diff --git a/testapi/opnfv_testapi/ui/components/pods/podsController.js b/testapi/opnfv_testapi/ui/components/pods/podsController.js index 95e970d..f405ecb 100644 --- a/testapi/opnfv_testapi/ui/components/pods/podsController.js +++ b/testapi/opnfv_testapi/ui/components/pods/podsController.js @@ -21,7 +21,7 @@ PodsController.$inject = [ '$scope', '$http', '$filter', '$state', '$window', '$uibModal', 'testapiApiUrl','raiseAlert', - 'confirmModal', 'keepState' + 'confirmModal', 'keepState', 'sortService' ]; /** @@ -30,7 +30,7 @@ * through pods declared in TestAPI. */ function PodsController($scope, $http, $filter, $state, $window, $uibModal, testapiApiUrl, - raiseAlert, confirmModal, keepState) { + raiseAlert, confirmModal, keepState, sortService) { var ctrl = this; ctrl.url = testapiApiUrl + '/pods'; ctrl.checkBox = [] @@ -50,29 +50,8 @@ ctrl.sortBy = sortBy function sortBy(field){ - if(ctrl.sorting[field]){ - ctrl.data.pods.sort(function(a,b) { - if (a[field].toLowerCase() > b[field].toLowerCase()) { - return -1; - } - if (a[field].toLowerCase() < b[field].toLowerCase()) { - return 1; - } - return 0; - }); - ctrl.sorting[field] = false - }else{ - ctrl.data.pods.sort(function(a,b) { - if (a[field].toLowerCase() < b[field].toLowerCase()) { - return -1; - } - if (a[field].toLowerCase() > b[field].toLowerCase()) { - return 1; - } - return 0; - }); - ctrl.sorting[field] = true - } + ctrl.data.pods = sortService.sortFunction(ctrl.data.pods, field , ctrl.sorting[field] ) + ctrl.sorting[field]=!ctrl.sorting[field] } /** diff --git a/testapi/opnfv_testapi/ui/components/projects/projects.html b/testapi/opnfv_testapi/ui/components/projects/projects.html index 84902f8..5d514d1 100644 --- a/testapi/opnfv_testapi/ui/components/projects/projects.html +++ b/testapi/opnfv_testapi/ui/components/projects/projects.html @@ -38,7 +38,14 @@ Bulk Select - Name + Name + + + + + + + Description Operations diff --git a/testapi/opnfv_testapi/ui/components/projects/projectsController.js b/testapi/opnfv_testapi/ui/components/projects/projectsController.js index d4fa962..07a58fe 100644 --- a/testapi/opnfv_testapi/ui/components/projects/projectsController.js +++ b/testapi/opnfv_testapi/ui/components/projects/projectsController.js @@ -21,7 +21,7 @@ ProjectsController.$inject = [ '$scope', '$http', '$filter', '$state', '$window', '$uibModal', 'testapiApiUrl', - 'raiseAlert', 'confirmModal', 'authenticate', 'keepState' + 'raiseAlert', 'confirmModal', 'authenticate', 'keepState', 'sortService' ]; /** @@ -30,7 +30,7 @@ * through projects declared in TestAPI. */ function ProjectsController($scope, $http, $filter, $state, $window, $uibModal, testapiApiUrl, - raiseAlert, confirmModal, authenticate, keepState) { + raiseAlert, confirmModal, authenticate, keepState, sortService) { var ctrl = this; ctrl.url = testapiApiUrl + '/projects'; @@ -44,11 +44,13 @@ ctrl.openBatchDeleteModal = openBatchDeleteModal; ctrl.projectDelete = projectDelete; ctrl.batchDelete = batchDelete; + ctrl.sortByName = sortByName ctrl.checkBox = []; ctrl.checkBoxList = []; ctrl.name = ''; ctrl.details = ''; + ctrl.ascending = false; /** * This will contact the TestAPI to create a new project. @@ -72,6 +74,11 @@ }); } + function sortByName(){ + ctrl.data.projects = sortService.sortFunction(ctrl.data.projects, 'name', ctrl.ascending) + ctrl.ascending = !ctrl.ascending + } + /** * This will open the modal that will show the create * project view -- cgit 1.2.3-korg