【问题标题】:Pagination in angular jsAngular js中的分页
【发布时间】:2015-09-16 06:52:28
【问题描述】:

我通过 http post 调用从数据库中获取数据并呈现表格。好吧,实现代码看起来不错,但是分页似乎不起作用。

我根据我的要求实现了它,我得到了我可以在 chrome 控制台中看到的响应。分页可能有什么问题,因为我看不到任何分页按钮。

代码如下:

 <!doctype html>
    <html lang="en" ng-app="myApp">
        <head>
            <meta charset="utf-8">
            <base href="/">
            <title>The Single Page Blogger</title>
            <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.min.js"></script>
            <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
            <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
            <script data-require="ui-bootstrap@*" data-semver="0.12.1" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.min.js"></script>
            <script src="<%=request.getContextPath()%>/js/module.js"></script>
            <link rel="stylesheet" href="<%=request.getContextPath()%>/style2.css" />
            <script>

                //Get table from Server and do pagination
                app.controller("tableController", function ($scope, $http) {
                    $scope.filteredTodos = []
                            , $scope.currentPage = 1
                            , $scope.numPerPage = 10
                            , $scope.maxSize = 5;

                    $scope.getTable = function () {
                        $scope.customerTable = [];

                        $http.get('<%=request.getContextPath()%>/GetTable.do').success(function (data)
                        {
                            $scope.customerTable = data;
                        });
                    };
                    $scope.getTable();

                    $scope.$watch("currentPage + numPerPage", function () {
                        var begin = (($scope.currentPage - 1) * $scope.numPerPage)
                                , end = begin + $scope.numPerPage;

                        $scope.filteredTodos = $scope.customerTable.slice(begin, end);
                    });
                });

            </script>
        </head>
        <body>
            <div class="container" id="main"><br/><br/>
                Search: <input type="text" ng-model="search" placeholder="Search">
                <div ng-controller="tableController">
                    <table class="table table-striped table-hover table-bordered">
                        <tr>
                            <th style="font-size: 13.3px">Card number</th>
                            <th style="font-size: 13.3px">First name</th>
                            <th style="font-size: 13.3px">Opening balance</th>
                            <th style="font-size: 13.3px">Withdrawal</th>
                            <th style="font-size: 13.3px">Deposit</th>
                            <th style="font-size: 13.3px">Closing balance</th>
                            <th style="font-size: 13.3px">Tx date</th>
                            <th style="font-size: 13.3px">Usage type</th>
                        </tr>
                        <tr ng-repeat="data in customerTable| filter: search">
                            <td>{{data.CARD_NUMBER}}</td>
                            <td>{{data.FIRST_NAME}}</td>
                            <td>{{data.OPENING_BALANCE}}</td>
                            <td>{{data.WITHDRAWAL}}</td>
                            <td>{{data.DEPOSIT}}</td>
                            <td>{{data.CLOSING_BAL}}</td>
                            <td>{{data.TXDATE}}</td>
                            <td>{{data.USAGE_TYPE}}</td>
                        </tr>
                    </table>
                    <pagination 
                        ng-model="currentPage"
                        total-items="customerTable.length"
                        max-size="maxSize"  
                        boundary-links="true">
                    </pagination>
                    <br/><br/><br>
                    </form>
                </div>
            </div>
        </body>
    </html>

这里是 plunker:http://plnkr.co/edit/eNgT4bVroGIla4EOdkNZ?p=preview

模块:

var app = angular.module('myApp', ['ui.bootstrap']);

【问题讨论】:

  • 我可以看到分页,一切似乎都正常
  • 是的,它不适用于我的代码。 plunker 只是一个演示
  • 您是否将['ui.bootstrap'] 定义为依赖注入?就像var app = angular.module('app', ['ui.bootstrap']);??
  • 我在你的代码中看不到ui-bootstrap-tpls-0.12.1.min.jsscript,你检查过你有像plunker这样的一切吗?
  • 是的,刚才我加了,试过了,还是不行

标签: angularjs


【解决方案1】:

试试这个,

                <tr ng-repeat="data in filteredTodos| filter: search">
                            <td>{{data.CARD_NUMBER}}</td>
                            <td>{{data.FIRST_NAME}}</td>
                            <td>{{data.OPENING_BALANCE}}</td>
                            <td>{{data.WITHDRAWAL}}</td>
                            <td>{{data.DEPOSIT}}</td>
                            <td>{{data.CLOSING_BAL}}</td>
                            <td>{{data.TXDATE}}</td>
                            <td>{{data.USAGE_TYPE}}</td>
                        </tr>

您应该迭代 filteredTodos 而不是 customerTable

【讨论】:

  • 使用filteredTodos时无法获取视图上的响应数据
  • 我可以在 chrome 控制台中看到响应,但使用 filtersTodos 表是空的
  • 情况并非总是如此,是的,它应该,但如果你做到了!例如,$http.get() 向服务器发送一个异步调用,当调用获取数据时,其余代码将编译,因此有时您在页面加载时手头没有准备好数据。这是人们使用 promises、$q 等的众多原因之一。
  • 在您的情况下,您需要进行大量动态编译,不仅要编译 AngularJS 部分,而且您在顶部导入的 CSS 和 js 似乎需要一些时间提供的路径正在使用&lt;%=request.getContextPath()%&gt; 相对于上下文进行编译,我建议您使用相对路径加载这些路径。
  • @kittu 为你找到了一些东西 :) stackoverflow.com/questions/20369377/…
猜你喜欢
  • 2018-12-02
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
相关资源
最近更新 更多