简体   繁体   English

AngularJS分页几乎可以正常工作

[英]AngularJS pagination almost working

Trying to finish pagination on a small search app using AngularJS UI bootstrap pagination directive . 尝试使用AngularJS UI引导程序分页指令在小型搜索应用程序上完成分页。 Nearly have it working EXCEPT that I can not get the first 10 results to display after the initial search. 除了在最初搜索后无法显示前10个结果之外,它几乎可以正常工作。

What happens is the initial 100 results are loaded and they are split to display 10 per page. 发生的情况是先加载了100个结果,然后将它们拆分为每页显示10个结果。 However, the first 10 do not appear UNTIL after you paginate some and click on the page "1" link in the pagination control. 但是,对前10个页面进行分页并单击分页控件中的页面“ 1”链接后,前10个不会出现。

This is the HTML 这是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>

This is my search function that loads the initial 100 results 这是我的搜索功能,可加载最初的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);
        }
        }
    )
  };

And then these are the functions I'm using for pagination 这些是我用于分页的功能

    $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();
};

I'm pretty sure the culprit to this problem is this line in $scope.search() 我很确定这个问题的罪魁祸首是$ scope.search()中的这一行

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

specifically the equal sign ...? 特别是等号...?

I'm still learning js and angularjs so I'm not really sure what should replace the "=" ... so that the initial 10 results appear 我仍在学习js和angularjs,所以我不确定是否应该替换“ =” ...,以便显示最初的10个结果

$scope.results.documents seems to be the property holding your currently displayed documents. $scope.results.documents似乎是保存您当前显示的文档的属性。 You also need to set it once searchService returned its results, not just when clicking on the pagination. 您还需要在searchService返回其结果后进行设置,而不仅仅是单击分页时。

$scope.results.pagination is the full array of results; $scope.results.pagination是完整的结果数组; so you want to slice that just like when changing page. 因此,您希望像在更改页面时那样对其进行切片。

Change 更改

) };

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

So you are sure, that pagination happens after you prepared it. 因此,您可以肯定,分页是在您准备好它之后发生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM