【问题标题】:AngularJS with AngularUI Bootsrap pagination directive doesn't hide results带有 Angular UI Bootstrap 分页指令的 AngularJS 不会隐藏结果
【发布时间】:2014-08-19 02:25:30
【问题描述】:

我第一次尝试使用 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>

感谢任何建议,谢谢!

【问题讨论】:

    标签: javascript angularjs pagination angular-ui


    【解决方案1】:

    您需要在下面的 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);
            }
    
        }
    });
    

    【讨论】:

    • 这不起作用,我收到错误 Error: [$injector:unpr] Unknown provider: startFromFilterProvider 因为我没有名为 startFrom 的过滤器
    • 对不起,我已经添加了过滤器开始回答
    【解决方案2】:

    我找不到我使用的原始示例,但这是我的应用程序中的示例。

    过滤器部分并不重要,但 filterProducts 对象会被切片并显示在您的视图中。查看$watch 了解其工作原理。

    app.controller('ProductController', function($scope, $filter, $routeParams, $rootScope, $location, Products){
    
        $scope.Products = Products;
        Products.brandLimit = $routeParams.brandLimit;
        Products.brand = $routeParams.brand;
    
        // try to avoid reloading the prod data
        if (!Products.products){
            Products.getProducts().then(function(data){
                Products.products = data.products;
                Products.pagination();
            });
        }else{
            Products.pagination();
        }
    
        // watch for when the pagination changes
        $scope.$watch('Products.currentPage + Products.numPerPage', function() {
            var begin = ((Products.currentPage - 1) * Products.numPerPage);
            var end = begin + Products.numPerPage;
    
            Products.pagedProducts = Products.filteredProducts.slice(begin, end);
        });
    });
    

    在服务中:

    app.factory('Products', function($http, $filter, $location, $routeParams){
        var Products = {
            search: '',
            searching: false,
            filteredProducts: '',
            pagedProducts: '',
            getProduct: function(id){
                delete Products.detail;
                $http.get('/product/' + id).then(function(response){
                    Products.detail = response.data.product;
                });
            },
            getProducts: function(){
                return $http.get('/product').then(function(response){
                    return response.data;
                });
            },
            pagination: function(){
    
                // relies on fulltext filter
                this.filteredProducts = $filter('fulltext')(this.products, this.brandLimit);
    
                // set up default values to feed to ui pagination
                this.currentPage = 1;
                this.numPerPage = 10;
                this.maxSize = 10;
    
                // check the length of filtered items based on search or brand clicked (in the URL)
                this.totalItems = this.filteredProducts.length;
                this.numPages =  Math.ceil(this.totalItems / this.numPerPage);
            },
            brandTitle: function() {
    
                if (this.searching === false || this.brand) {
                    this.search = '';
                    return $routeParams.brand + " Products";
                } else {
                    return 'Searching "' + $routeParams.brandLimit + '"';
                }
            },
            searchTerm: function(){
                if(this.search){
                    $location.path("search/" + this.search);
                    this.searching = true;
                }else{
                    $location.path("/");
                    this.searching = false;
                }
            }
        };
        return Products;
    });
    

    还有 HTML:

    <pagination ng-show="Products.numPages" total-items="Products.totalItems" ng-model="Products.currentPage" max-size="Products.maxSize" class="pagination-small" boundary-links="true" rotate="false" num-pages="Products.numPages"></pagination>
    
    <table class="table table-striped">
        <tr>
            <th>Maker</th>
            <th>Title</th>
            <th ng-bind="product.priceDealer">Dealer Price</th>
            <th>MSRP</th>
        </tr>
        <tr ng-repeat="product in Products.pagedProducts">
            <td>{{product.brand}}</td>
            <td><a href="#/product-detail/{{product.id}}/{{product.title | slug}}">{{product.title}}</a></td>
            <td ng-bind="product.priceDealer | currency"></td>
            <td>{{product.msrp | currency:"$"}}<td>
            </tr>
        </table>
    

    【讨论】:

    • 我真的不喜欢在有办法不用 $watch 时使用它。
    【解决方案3】:

    不需要,使用 Angular UI Bootstrap 的属性:

    HTML

    <pager total-items="totalItems" ng-model="currentPage" items-per-page="itemsPerPage"></pager>
    

    ==== 并在您的控制器中添加以下代码

    ============

    $scope.totalItems = $scope.testimonials.length;
    $scope.itemsPerPage = 5;
    $scope.currentPage = 1;
    
    $scope.$watch('currentPage + itemsPerPage', function () {
            var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
                end = begin + $scope.itemsPerPage;
    
            $scope.filteredtestimonials= $scope.alerts.slice(begin, end);
        });
    

    ============

    请注意,您需要提及 ng-repeat="testimonial in filteredtestimonials"

    和属性应该在你使用 ng-repeat 的相同范围内

    如果您仍然遇到任何问题,请告诉我,您可以在以下位置查看更多示例:http://angular-ui.github.io/bootstrap/#/pagination

    包括:

    在您的页面或布局页面中,每页的项目不会直接产生价值,似乎

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      相关资源
      最近更新 更多