{{numPages}}不是由分页指令计算的

时间:2014-10-01 12:51:20

标签: angularjs pagination angular-ui

我对分页指令的印象是{{numPages}}值将由指令计算。它没有为我返回任何东西。

为了让这项工作正常,我有什么遗漏吗?我不想计算它,如果该指令应该为我做这个。其他分页效果很好。

<pagination 
            total-items="totalItems" 
            ng-model="currentPage" 
            max-size="maxSize" 
            items-per-page="itemsPerPage" 
            class="pagination-sm" 
            boundary-links="true" rotate="false">
     </pagination>
    <table class="table table-striped">         
        <tr>
            <td     style="width:150px;">GPT ID</td>
            <td     style="width:250px;">Therapy Area</td>
            <td     style="width:450px;">GPT Description</td>           
            <td     style="width:150px;">Actions</td>           
        </tr>
        <tr ng-repeat="prGpt in prGpts | orderBy:['therapyArea.therapyArea','gptDesc'] | startFrom:(currentPage -1) * itemsPerPage | limitTo: itemsPerPage">        
            <td>{{prGpt.id}}</td>           
            <td>
                <span ng-if="!prGpt.editMode">{{prGpt.therapyArea.therapyArea}}</span>                  
                <span ng-if="prGpt.editMode && !createMode">
                    <select class="form-control" style="width:150px;" ng-model="selectedGpt.therapyArea" ng-options="item as item.therapyArea for item in therapyAreas"/>
                </span>
            </td>
            <td>
                <span ng-if="!prGpt.editMode">{{prGpt.gptDesc}}</span>
                <span ng-if="prGpt.editMode && !createMode"><input class="form-control" type="text" style="width:400px;" ng-model="selectedGpt.gptDesc" /></span>
            </td>
            <td>
                <span ng-if="!prGpt.editMode" class="glyphicon glyphicon-pencil" ng-click="copySelectedGpt(prGpt);beginEditGpt()"/>
                <span ng-if="prGpt.editMode  && !createMode" class="glyphicon glyphicon-floppy-disk" ng-click="saveEditGpt()"/>
                <span ng-if="prGpt.editMode  && !createMode" class="glyphicon glyphicon-thumbs-down" ng-click="cancelEditGpt()"/>
                <span ng-if="!prGpt.editMode  && !createMode" class="glyphicon glyphicon-remove-circle" ng-click="copySelectedGpt(prGpt);openDeleteDialog()"/>
                <span><a ng-href="#!pr/gptProjects/{{prGpt.id}}">Edit Projects</a>                  
            </span>                         
        </tr>
    </table> 
    <br/>      
       <pre>Page: {{currentPage}} / {{numPages}}</pre>
    </div> 

控制器:

 // GPT List Controller  
     .controller('prGPTCtrl',['$scope', '$modal', '$dialog', 'Restangular', 'prTAService', 'prGPTService', function($scope, $modal, $dialog, Restangular, prTAService, prGPTService) {

          // window.alert('prGPTCtrl');
            $scope.prGpts = {};
            $scope.therapyAreas = {};
            $scope.createMode = false;
            $scope.selectedGpt = {};
            $scope.newGpt = {};

            // pagination
            $scope.currentPage = 1;
            $scope.itemsPerPage = 10;
            $scope.maxSize = 20;
            $scope.totalItems = $scope.prGpts.length;           

            Restangular.setBaseUrl('resources/pr');

            //call the TA service to get the TA list for the drop down lists
            //and then get the gpt list once successful
            prTAService.getTAs().then(function(tas) {
                $scope.therapyAreas = tas;
                prGPTService.getGPTs().then(function(gpts) {
                    //window.alert('prGPTCtrl:getGPTs');
                    $scope.prGpts = gpts;
                });
            });

            $scope.$watch('prGpts.length', function(){              
                $scope.totalItems = $scope.prGpts.length;
             });              


            /*
             * Take a copy of the selected GPT to copy in
             */         
            $scope.copySelectedGpt = function(prGpt) {
                $scope.selectedGpt = Restangular.copy(prGpt);
            };

            $scope.beginEditGpt = function() {
                var gpt = {};
                var ta = {};

                var gpt;
                for(var i = 0; i < $scope.prGpts.length;i++) {
                    gpt = $scope.prGpts[i];
                    gpt.editMode = false;
                }

                var index = _.findIndex($scope.prGpts, function(b) {
                    return b.id === $scope.selectedGpt.id;
                });

                gpt = $scope.prGpts[index];
                gpt.editMode = true;

                var taIndex = _.findIndex($scope.therapyAreas, function(b) {
                    return b.id === $scope.selectedGpt.therapyArea.id;
                });

                ta = $scope.therapyAreas[taIndex];

                $scope.selectedGpt.therapyArea = ta;

                $scope.createMode = false;

            };


            $scope.cancelEditGpt = function() {
                var gpt;
                for(var i = 0; i < $scope.prGpts.length;i++) {
                    gpt = $scope.prGpts[i];
                    gpt.editMode = false;
                }

                var index = _.findIndex($scope.prGpts, function(b) {
                    return b.id === $scope.selectedGpt.id;
                });

                $scope.selectedGpt = null;
                $scope.prGpts[index].editMode = false;  
            };




            $scope.saveEditGpt = function() {
                $scope.selectedGpt.save().then(function (gpt) {
                    // find the index in the array which corresponds to the current copy being edited
                    var index = _.findIndex($scope.prGpts, function(b) {
                        return b.id === $scope.selectedGpt.id;
                    });

                    $scope.prGpts[index] = $scope.selectedGpt;
                    $scope.prGpts[index].editMode = false;  
                    $scope.selectedGpt = null;

                },
                function(err) {
                    window.alert('Error occured: ' + err);
                }
                );
            };

            // create a new GPT
            $scope.createGpt = function() {
                $scope.createMode = true;
                var gpt;
                for(var i = 0; i < $scope.prGpts.length;i++) {
                    gpt = $scope.prGpts[i];
                    gpt.editMode = false;
                }
            };

            $scope.saveNewGpt  = function() {
                Restangular.all('/gpt/gpts').post($scope.newGpt).then(function(gpt) {
                     $scope.newGpt = {};
                     $scope.prGpts.push(gpt);
                     $scope.createMode = false;
                    // window.alert('created new GPT ' + gpt.gptDesc + ' with id ' + gpt.id);
                });
            };


            $scope.openDeleteDialog = function() {
                var title = "Please confirm deletion of GPT " + $scope.selectedGpt.gptDesc;
                var msg = "<b>Delete GPT? Please confirm...</b>";
                var btns = [{result:'CANCEL', label: 'Cancel'},
                            {result:'OK', label: 'OK', cssClass: 'btn-primary'}];

                $dialog.messageBox(title, msg, btns, function(result) {
                      if (result === 'OK') {
                        $scope.deleteGpt();
                      }
                });
            };

            $scope.deleteGpt = function()  {
                $scope.selectedGpt.remove().then(function() {
                    $scope.prGpts = _.without($scope.prGpts, _.findWhere($scope.prGpts, {id: $scope.selectedGpt.id}));
                    $scope.selectedGpt = null;
                },
                function() {
                    window.alert("There was an issue trying to delete GPT " + $scope.selectedGpt.gptDesc);
                }
                );
            };


     }]);

我有一个startFrom过滤器。

.filter('startFrom', function () {
            return function (input, start) {

                if (input === undefined || input === null || input.length === 0
                 || start === undefined || start === null || start.length === 0 || start === NaN) return [];
                start = +start; //parse to int

                try {
                    var result = input.slice(start);
                    return result;

                } catch (e) {

                //    alert(input);
                }        
            };
        })  

此致

1 个答案:

答案 0 :(得分:4)

您似乎在num-pages="numPages"标记上遗漏了<pagination>。基本上你必须提供一个变量pagination来返回页数。这是通过num-pages

完成的
<pagination 
            num-pages="numPages" <!-- Add this here -->
            total-items="totalItems" 
            ng-model="currentPage" 
            max-size="maxSize" 
            items-per-page="itemsPerPage" 
            class="pagination-sm" 
            boundary-links="true" rotate="false">
</pagination>