Add sorting service to module 65/52865/2
authorthuva4 <tharma.thuva@gmail.com>
Fri, 9 Mar 2018 18:40:46 +0000 (00:10 +0530)
committerthuva4 <tharma.thuva@gmail.com>
Fri, 9 Mar 2018 18:43:38 +0000 (00:13 +0530)
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 <tharma.thuva@gmail.com>

testapi/opnfv_testapi/ui/app.js
testapi/opnfv_testapi/ui/components/pods/podsController.js
testapi/opnfv_testapi/ui/components/projects/projects.html
testapi/opnfv_testapi/ui/components/projects/projectsController.js

index a64bfcc..ada7577 100644 (file)
             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) {
index 95e970d..f405ecb 100644 (file)
@@ -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 = []
         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]
         }
 
         /**
index 84902f8..5d514d1 100644 (file)
         <tr style="
         text-align: center;">
             <th style="width: 1%;">Bulk Select</th>
-            <th style="width: 19%;">Name</th>
+            <th style="width: 19%;">Name
+                <a class="text-danger"  ng-click="ctrl.sortByName()" ng-class="{ 'hidden': !ctrl.sortName }" >
+                    <span class="glyphicon glyphicon-sort-by-alphabet pull-right" aria-hidden="true"></span>
+                </a>
+                <a class="text-danger"  ng-click="ctrl.sortByName()" ng-class="{ 'hidden': ctrl.sortName }" >
+                    <span class="glyphicon glyphicon-sort-by-alphabet-alt pull-right" aria-hidden="true"></span>
+                </a>
+            </th>
             <th style="width: 70%;">Description</th>
             <th style="width: 10%;" ng-class="{'hidden': ! ((auth.projectNames.length>0) && auth.isAuthenticated) && authenticate}">Operations</th>
         </tr>
index d4fa962..07a58fe 100644 (file)
@@ -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';
 
         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.
                 });
         }
 
+        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