1

I am writing a piece of code for private tutions. It contains details of students which are stored in MySQL database.I am dislaying it with ng-repeat. I used PHP for fetching data. There are two things which are not working properly in code

  1. In sorting of column, the 'balance fees','No of days' and 'No of Days remaining column' doesnt get sort.
  2. In calcTotal(names) the total of fees paid shows as 15000800012000 where i provided the data as 15000,8000 and 12000

Here is code :

<script>

                    var app= angular.module("myapp",[]);
                    app.controller("mycontroller",function ($scope, $http)
                        //get data from database
                        {
                        $http.get("getData.php").then(function (response) {
                            $scope.names = response.data.records;});
                            //subtraction between dates
                            $scope.CurrentDate=new Date();
                            $scope.calDate = function(date1, date2){
                               var dateOut1 = new Date(date1);
                               var dateOut2 = new Date(date2);
                               var timeDiff = Math.abs(dateOut2.getTime() - dateOut1.getTime());

                               var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
                               return diffDays;
                            };
                            $scope.getToday = function(){
                                return new Date();
                            }
                            //total fees paid
                                  $scope.calcTotal = function(names){
                                        var sum = 0;
                                        for(var i = 0 ; i<names.length ; i++){
                                        sum = sum + names[i].fpaid;
                                        }
                                    return sum;
                                  };

                            //Sorting Column
                            $scope.sortColumn = "";


                                    $scope.sortColumn = "name";
                                    $scope.reverseSort = false;

                                    $scope.sortData = function (column) {
                                        $scope.reverseSort = ($scope.sortColumn == column) ?
                                            !$scope.reverseSort : false;
                                        $scope.sortColumn = column;
                                    }

                                    $scope.getSortClass = function (column) {

                                        if ($scope.sortColumn == column) {
                                            return $scope.reverseSort
                                              ? 'arrow-down'
                                              : 'arrow-up';
                                        }

                                        return '';
                                    }
                        });    

    </script>
    <body ng-app ="myapp" ng-controller="mycontroller">
        <table border=1>
                    <tr>

                        <th ng-click="sortData('name')">Name of student <div ng-class="getSortClass('name')"></div></th>
                        <th ng-click="sortData('cname')">Course Name <div ng-class="getSortClass('cname')"></div> </th>
                        <th ng-click="sortData('cfees')">Course fees  <div ng-class="getSortClass('cfees')"></th>
                        <th ng-click="sortData('fpaid')">Fees paid  <div ng-class="getSortClass('fpaid')"></th>
                        <th ng-click="sortData('bfees')">Balance Fees<div ng-class="getSortClass('bfees')"></th>   //sorting is not working on this column
                        <th ng-click="sortData('sdate')">Start Date of Course <div ng-class="getSortClass('sdate')" ></th>
                        <th ng-click="sortData('edate')">End date of course <div ng-class="getSortClass('edate')" ></th>
                        <th> No of days </th>
                        <th>No of Days remaining </th>

                    </tr>
                        <tr   ng-repeat="x in names  | filter: searchText | orderBy:sortColumn">

                            <td>{{x.Bank}}</td>
                            <td>{{x.cname}}</td>
                            <td >{{x.cfees}}</td>
                            <td>{{x.fpaid }}</td>
                            <td ng-model="bfess">{{x.cfees-x.fpaid}}</td>
                            <td>{{x.sdate | date:'dd-MM-yyyy'}}</td>
                            <td>{{x.edate | date:'dd-MM-yyyy'}}</td>
                            <td>{{calDate(x.edate,x.sdate)}}</td>
                            <td>{{calDate(x.edate,getToday()| date:'dd-MM-yyyy')}}</td>                     
                    </tr>
                    <tr>
                        <td>Total fess paid by students: {{ calcTotal(names) }}</td>
                    </tr>

                </table>
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44

0 Answers0