angularJS:动态标题排序不起作用

angularJS: Dynamic header sorting not working

本文关键字:排序 不起作用 标题 动态 angularJS      更新时间:2023-09-26

我正在尝试实现一种解决方案,通过使用AngularJS点击表的标题来对表进行排序。

我在谷歌搜索后发现了一个很好的例子:https://scotch.io/tutorials/sort-and-filter-a-table-using-angular

我可以看到向上和向下箭头,但当我单击它们时,表不会排序。

我认为问题在于在我的情况下如何格式化JSON对象。我一直没能弄清楚,我希望通过我在这篇帖子中提供的信息,我能得到一些帮助,了解我做错了什么。

这是JavaScript的副本:

    (function (define, angular) {
    'use strict';
    define(function () {
        var opportunityController = function ($scope, Metadata, Factory) {
            var vm = this;
            //set the default sort type
            vm.sortType = 'Products';            
            //set the default sort order
            vm.sortReverse = false;             
            Factory.Data(caller.sp, caller.filter).then(function (payload) {
                var data = angular.fromJson(payload.data).Table;
                ProcessData(data);
            });
            function ProcessData(data) {
                if (angular.isDefined(data)) {
                    var counter = 0;
                    vm.products = [];
                    vm.productsSet = FindByAsObjectArray(function (x) {
                        return (x.TypeName == "Product");
                    }, data);
                    for (var index = 0, length = vm.productsSet.length; index < length; index++) {
                        vm.products[index] = {
                            data: vm.productsSet[index]
                        };                                                
                    }
                }                    
            }
        };
        return ['$scope','Metadata','Factory',opportunityController];
    });
})(define, angular);

我开始工作了,最终版本:https://jsfiddle.net/itortu/nhhppf53/

非常感谢。

您正在使用控制器的

ng-controller="Company:OpportunityController as opportunity"

因此,你必须在你的点击中引用sortType和sortReverse,就像这样:

ng-click="opportunity.sortType = 'Products'; opportunity.sortReverse = !opportunity.sortReverse"