首页 > 解决方案 > Angular.forEach 转换值以在排序表中使用

问题描述

我正在尝试在 Angular 中整理我的列表,但它不起作用。

默认排序顺序应按财政年度降序排列,但一些 2019 年的值在 2020 年的值之后排序:

https://i.imgur.com/1F9JM2V.png

然后,当您单击该列排序时,数字被错误地排序:

https://i.imgur.com/67fMJ8V.png

并且结束日期没有排序结构。我在视图中将其设置为 MM/dd/yyyy 格式,不确定是否有任何影响:

https://i.imgur.com/dOcnFBt.png

只尝试了反向和降序。不确定是否有任何语法或内置的排序方式。

控制器:

function init() {
        $scope.loading = true;
        $scope.rfrorder = {
            Orderby: 'rfrFY',
            descending: false
        };
        ContractsService.getRefRFRInformation()
            .then(function (results) {
                $scope.refRFRInfo = results.data;
                angular.forEach($scope.refRFRInfo, function (value) {
                    value.edit = true;
                    value.editMode = false;
                    if (value.endDate == null) {
                        value.edit = false;
                    }
                }); 
                $scope.loading = false;
            });

    }
 $scope.rfrSorting = function (column) {
        var sort = $scope.rfrorder;
        if (sort.Orderby == column) {
            sort.descending = !$scope.rfrorder.descending;
        } else {
            sort.Orderby = column;
            sort.descending = false;
        }
    };
    $scope.rfrselected = function (column) {
        if (column == $scope.rfrorder.Orderby) {
            return ('tablesort-icon glyphicon glyphicon-arrow-' + (($scope.rfrorder.descending) ? 'down' : 'up'));
        }
        else {
            return 'tablesort-icon glyphicon glyphicon-sort';
        }
    };    

看法:

<thead class="headercolor">
   <tr class="thead">
       <th ng-click="rfrSorting('rfrFY')"><div class="tablesort-header">RFR FY <i ng-class="rfrselected('rfrFY')"></i></div></th>
       <th ng-click="rfrSorting('rfrNumber')"><div class="tablesort-header">RFR Number <i ng-class="rfrselected('rfrNumber')"></i></div></th>
       <th ng-click="rfrSorting('rfrEffectiveDate')"><div class="tablesort-header">Effective Date <i ng-class="rfrselected('rfrEffectiveDate')"></i></div></th>
       <th ng-click="rfrSorting('rfrEndDate')"><div class="tablesort-header">End Date <i ng-class="rfrselected('rfrEndDate')"></i></div></th>
       <th ng-click="rfrSorting('rfrModifiedDate')"><div class="tablesort-header">Modified Date <i ng-class="rfrselected('rfrModifiedDate')"></i></div></th>
       <th ng-click="rfrSorting('rfrModifiedBy')"><div class="tablesort-header">Modified By <i ng-class="rfrselected('rfrModifiedBy')"></i></div></th>
       <th></th>
    </tr>
</thead>

<tbody class="form-group form-group-sm">
<tr ng-repeat-start="rfrDetail in refRFRInfo | orderBy:rfrorder.Orderby:rfrorder.descending">

编辑

我相信这与以字符串形式返回的数字有关。我正在尝试找到一种方法来通过并转换数字,然后将它们放回我可以在视图上显示的对象中。

ContractsService.getRefRFRInformation()
            .then(function (results) {
                $scope.refRFRInfo = results.data;

                angular.forEach($scope.refRFRInfo, function (value) {
                    //$scope.model.refRFRInfo.rfrNumber = parseInt(value.rfrNumber);
                    //$scope.model.refRFRInfo.rfrFY = parseInt(value.rfrFY);
                    //$scope.model.refRFRInfo.endDate = Date.parse(value.endDate);
                    $scope.number.push(value.rfrNumber);
                    value.edit = true;
                    value.editMode = false;
                    //new Date(value.startDate).withoutTime() <= new Date().withoutTime() &&
                    if (value.endDate == null) {
                        // value.editMode = true;
                        value.edit = false;
                    }
                });
                $scope.loading = false;
            });

我了解基本原理,因为我有 $scope.number 进行验证,但我不知道如何遍历整个对象,然后创建一个具有正确值的新对象。

标签: angularjssorting

解决方案


问题在于您的排序代码。

试试看这篇文章: AngularJS 按表头排序行


推荐阅读