javascript - 带有 Angular UI Bootstrap 分页指令的 AngularJS 不会隐藏结果

标签 javascript angularjs pagination angular-ui

我第一次尝试使用 Angular-ui 分页指令,我很困惑为什么它不起作用。我可以看到分页按钮,它正确地显示了两个要分页的页面,因为有 8 个结果和 items-per-page="5" 但是我所有的数据项都显示而不是隐藏到五个每页。

控制者

dataService.get(uri).then(function (data) {

    $scope.testimonials = data;

    $scope.totalItems = $scope.testimonials.length;
    $scope.currentPage = 1;

    $scope.setPage = function(pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.pageChanged = function() {
        console.log('Page changed to: ' + $scope.currentPage);
    }
});

查看

<table class="table table-striped" ng-show="testimonials.length">
    <thead>
      <th>Name</th>
      <th>Message</th>
    </thead>
    <tbody>
    <tr ng-repeat="testimonial in testimonials">
      <td>{{testimonial.name}}</td>
      <td>{{testimonial.message}}</td>
      <td><a href="testimonials/{{testimonial.id}}" class="btn btn-primary">Edit</a></td>
      <td><button class="btn btn-danger" ng-click="delete(testimonial)">Delete</button></td>
    </tr>
    </tbody>

    <pagination total-items="totalItems" ng-model="currentPage" items-per-page="5" ng-change="pageChanged()"></pagination>
</table>

我很感激任何建议,谢谢!

最佳答案

你需要在下面的 ng-reapeter 代码中过滤数据才能工作

<table class="table table-striped" ng-show="testimonials.length">
    <thead>
      <th>Name</th>
      <th>Message</th>
    </thead>
    <tbody>
    <tr ng-repeat="testimonial in testimonials | startFrom: (currentPage-1)*5| limitTo: 5">
      <td>{{testimonial.name}}</td>
      <td>{{testimonial.message}}</td>
      <td><a href="testimonials/{{testimonial.id}}" class="btn btn-primary">Edit</a></td>
      <td><button class="btn btn-danger" ng-click="delete(testimonial)">Delete</button></td>
    </tr>
    </tbody>

    <pagination total-items="totalItems" ng-model="currentPage" items-per-page="5" ng-change="pageChanged()"></pagination>
</table>

过滤器开始于:

app.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);
        }

    }
});

关于javascript - 带有 Angular UI Bootstrap 分页指令的 AngularJS 不会隐藏结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24460926/

相关文章:

jquery - 分页 JS 停止工作,我不知道为什么

grails - 在 Grails 中,当我们将 max 和 offset 值传递给 list 方法时,如何获得总结果数?

javascript - React 应用程序是否应该在文件刷新时进行完全刷新?

Javascript::为什么 Object.hasOwnProperty ('caller' ) 返回 true?

javascript - 在简单的 Angular 指令中使用隔离范围 - @、& 和 =

mysql - 高效的 DB2 查询分页和显示总页数?

javascript - 需要在 JS 中的每次鼠标悬停时逐渐提高 div 不透明度属性

javascript - 如何在js中用两个不同的间隔为一个组件设置动画

javascript - 根据 Angular JS 中的应用程序逻辑在 Iframe 上下载或渲染

javascript - 具有两个几乎相同指令的 Angular 应用程序 - 一个指令有效,另一个无效