2016-07-02 40 views
0

嘗試使用AngularJS UI bootstrap pagination directive在小型搜索應用上完成分頁。幾乎有它的工作除了我不能得到前10個搜索結果後顯示。AngularJS分頁幾乎工作

會發生什麼情況是加載了最初的100個結果,並且它們被拆分爲每頁顯示10個結果。但是,在分頁之後,前10個字段不會出現,然後單擊分頁控件中的頁面「1」鏈接。

這是HTML

<uib-pagination total-items="results.totalItems" max-size="10" items-per-page="itemsPerPage" ng-model="currentPage" ng-change="changePage()" direction-links="true" previous-text="&lsaquo;" next-text="&rsaquo;" class="pagination-sm"></uib-pagination> 

這是加載初始100個結果

$scope.search = function() { 
    $scope.isSearching = true; 
    return searchService.search($scope.searchTerms, $scope.currentPage).then(function(es_return) {  
     var totalItems = es_return.hits.total; 
     var totalTime = es_return.took; 
     var numPages = Math.ceil(es_return.hits.total/$scope.itemsPerPage); 
     $scope.results.pagination = []; 
     for (var i = 1; i <= 100; i++) { 
     if(totalItems > 0) 
     $scope.results.totalItems = totalItems; 
     $scope.results.queryTime = totalTime; 
     $scope.results.pagination = searchService.formatResults(es_return.hits.hits); 
     } 
     } 
    ) 
    }; 

然後我的搜索功能,這些都是我使用了分頁功能

$scope.paginate = function() { 
    var begin = (($scope.currentPage - 1) * $scope.itemsPerPage); 
    var end = begin + $scope.itemsPerPage; 
    $scope.results.documents = $scope.results.pagination.slice(begin, end); 
    }; 


$scope.changePage = function() { 
    $scope.paginate(); 
}; 

我很確定這個問題的罪魁禍首是$ scope.search()

$scope.results.pagination "=" searchService.formatResults(es_return.hits.hits); 

特別是等號......?

我還在學習js和angularjs,所以我真的不知道用什麼來替代的「=」 ...所以,最初的10個結果顯示

回答

0

$scope.results.documents似乎是持有物業的目前顯示的文件。您還需要在searchService返回結果後進行設置,而不僅僅是在單擊分頁時。

$scope.results.pagination是全部結果;所以你想切片,就像改變頁面時一樣。

+0

感謝您的簡單回答,我正在反思這一點 - 一行簡單的代碼,它的工作原理! – user3125823

0

變化

) };

).then((){$scope.paginate();}); }

所以你一定,你準備後,該分頁發生。

+0

感謝您的努力,但其他答案在第一次嘗試。我遇到了一些錯誤,試圖讓你的工作。 – user3125823