Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My Table 

[Store Number]	[opening closing time]	[opening closing time]	[Profit (%)]
----------------------------------------------------------------------------- 
1					8--10						100					  23	
2					9--10						70					  15
3					10--10						60					  8
4					8--8						95					  20
5					8--9						70					  17
6					9--9						85					  19		

I am able to sort the data asc and desc order where the data is in int value but not where data in 
string. Here in example i could not sort the column [opening closing time] as it has to be sort
by the time interval difference. How do i achieve this ? I need to implement sort functionality in all
column. Rest 3 columns works as desired. But i do not know how do i implement sorting in 
[opening closing time] column? 


My HTML
<table>
<thead>
	<tr>
		<th ng-click="sortRdata('StoreNum')">Store Number</th>
		<th ng-click="sortRdata('OperatingHours')">opening closing time</th>
		<th ng-click="sortRdata('AvgCustNum')">opening closing time</th>
		<th ng-click="sortRdata('Profit')">Profit (%)</th>	
	</tr>
</thead>
<tbody>
		<tr ng-repeat="r in StoreReports|orderBy:sortRcolumn:reverseRsort ">
		 <td>{{::r.StoreNum}}</td>
         <td>{{::r.OperatingHours}}</td>
         <td>{{::r.AvgCustNum}}</td>
         <td>{{::r.Profit}}</td>
		</tr>
</tbody>
</table>


Javascipt Code:
    $scope.sortRcolumn = "StoreNum";
    $scope.reverseRsort = false;

    $scope.sortRdata = function (column) {
        $scope.reverseRsort = ($scope.sortRcolumn == column) ? !$scope.reverseRsort : false;
        $scope.sortRcolumn = column;
    }


What I have tried:

$scope.sdata = function (OperatingHours) {
       var operatingHrArray = operatingHours.slpit('');
       var openingHour = parseInt(operatingHrArray[0]);
       var closingHour = parseInt(operatingHrArray[3]) + 12;
       return closingHour - openingHour;
   }
Posted
Updated 21-Aug-17 17:54pm

1 solution

try this way

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

    <div ng-app="myApp" ng-controller="myCtrl">
 


        <table>
            <thead>
                <tr>
                    <th ng-click="sortRdata('StoreNum')">Store Number</th>
                    <th ng-click="sortRdata('OperatingHours')">opening closing time</th>
                    <th ng-click="sortRdata('AvgCustNum')">opening closing time</th>
                    <th ng-click="sortRdata('Profit')">Profit (%)</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="r in StoreReports|orderBy:sortRcolumn:reverseRsort ">
                    <td>{{::r.StoreNum}}</td>
                    <td>{{::r.OperatingHours}}</td>
                    <td>{{::r.AvgCustNum}}</td>
                    <td>{{::r.Profit}}</td>
                </tr>
            </tbody>
        </table>

    </div>

    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope) {
            $scope.sortRcolumn = "StoreNum";
            $scope.reverseRsort = false;

            var data = [
                { StoreNum: 1, OperatingHours: '8--10', AvgCustNum: 100, Profit: 23 },
                { StoreNum: 2, OperatingHours: '9--10', AvgCustNum: 70, Profit: 15 },
                { StoreNum: 3, OperatingHours: '10--10', AvgCustNum: 60, Profit: 8 },
                { StoreNum: 4, OperatingHours: '8--8', AvgCustNum: 95, Profit: 20 },
                { StoreNum: 5, OperatingHours: '8--9', AvgCustNum: 70, Profit: 17 },
                { StoreNum: 6, OperatingHours: '9--9', AvgCustNum: 85, Profit: 19 },
            ];

            var newData = [];
            for (var i = 0; i < data.length; i++) {
                var obj = data[i];
                obj.OperatingHoursNumber = parseInt(obj.OperatingHours.split('--')[0]);
                newData.push(obj);
            }
             

            $scope.StoreReports = newData; 
           
            $scope.sortRdata = function (column) {
               
                if (column == 'OperatingHours')
                    column = 'OperatingHoursNumber'
                $scope.reverseRsort = ($scope.sortRcolumn == column) ? !$scope.reverseRsort : false;
                $scope.sortRcolumn = column;
            }

        });
    </script>



</body>
</html>
 
Share this answer
 
Comments
[no name] 22-Aug-17 9:35am    
Sir, right now its sorting according to start hours , but it should be shorting how many hours each store opens. for example store 1 opens for 14 hours where as store 3 opens for 12 hours.
Karthik_Mahalingam 22-Aug-17 9:56am    
how will you calculate the total hours
[no name] 22-Aug-17 10:18am    
operating hours 8--10 means it opens at 8 am and close at 10 pm and like so , sir
Karthik_Mahalingam 22-Aug-17 10:24am    
how will you know it is 10am or 10 pm
[no name] 22-Aug-17 10:53am    
I could not tell looking at the raw data how it has been added in table. but looking at the model of store it runs 8 hours a day and it opens in morning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900