【问题标题】:how to correct paging in angularjs如何纠正angularjs中的分页
【发布时间】:2017-12-23 22:44:36
【问题描述】:

我有以下代码,我的页面位置 1 没有显示数据。它从第 2 页开始......我不知道......我正在尝试最后 3 个小时但没有成功我是 angularjs 的新手,这就是为什么我在这里请帮助我......代码在下面......

/HTML

 <table class="table table-hover" aria-describedby="dataTables-example_info" role="grid" class="table table-striped table-bordered table-hover dataTable no-footer" id="dataTables-example">
       <thead>
       <tr role="row">

   <th style="width: 360px;" colspan="1" rowspan="1">Class</th>
                                                    <th style="width: 360px;" colspan="1" rowspan="1">Section</th>
                                                    <th style="width: 260px;" colspan="1" rowspan="1">Edit Subjects</th>

                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr ng-repeat="info in data.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage))">
                                                   <td>{{info.class_name}}</td>
                                                    <td>{{info.section_name}}</td>                                                   

                                                   <td> <button ng-click="moreinformation(info)" type="button" class="btn btn-info btn-flat"><i class="fa fa-pencil"></i></td>


                                                </tr>
                                            </tbody>
                                            </table>
                                            <ul class="pagination"> 
             <li class="paginate_button next" aria-controls="dataTables-example" tabindex="0" id="dataTables-example_next">
                            <a  ng-click="prevPage()">« Prev</a>
                        </li>
            <li  ng-repeat="n in range(totalItems)"
                            ng-class="{active: n == currentPage}"
                            ng-click="setPage()">
                         <a href ng-bind="n + 1"></a>
                        </li>

             <li class="paginate_button next" aria-controls="dataTables-example" tabindex="0" id="dataTables-example_next">
                            <a  ng-click="nextPage()">Next »</a>
                        </li>
       </ul>

//JS代码...

 $scope.itemsPerPage =10;      
     $scope.currentPage = 0;
      $scope.totalItems = $scope.data.length / 10 ;
           $scope.data = [{ "class_name": "CLASS1", "section_name":"A" }, { "class_name": "CLASS2", "section_name": "A" }];
       $scope.prevPage = function () {
                            if ($scope.currentPage > 0) {
                                $scope.currentPage--;
                            }
                        };
                        $scope.nextPage = function () {
                            console.log($scope.totalItems);
                            if ($scope.currentPage < $scope.totalItems - 1) {
                                $scope.currentPage++;
                            }
                        };        
           $scope.setPage = function () {
                                console.log(this.n);
                                if (this.n == 0)
                                {
                                  //  this.n = 1;
                                    $scope.currentPage++;
                                }
                                else
                                {
                                    $scope.currentPage = this.n;
                                }

                            };

                            $scope.range = function (start, end) {
                                var ret = [];
                                if (!end) {
                                    end = start;
                                    start = 0;
                                }
                                for (var i = start; i < end; i++) {
                                    ret.push(i);
                                }

                                return ret;
                            };

【问题讨论】:

  • 你的 ng-bind 是 n + 1

标签: javascript angularjs angularjs-ng-repeat angular-ui-bootstrap


【解决方案1】:

this.n 在你的setPage 函数中总是绑定到默认值(如n),它并不关心你将n+1 设置为绑定到&lt;a&gt; 元素,这个绑定只会影响&lt;a&gt; 元素的内部文本。

因此,要完成这项工作,您可以例如将所需的值发送到您的 setPage 处理程序,如下所示:

ng-click="setPage(n + 1)"

在 JS 中是这样的:

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

【讨论】:

  • 请再提供一个答案...页面运行良好但... ng-class="{active: n == currentPage}" 显示您的第 2 页突出显示 .. 但我是第一页时我点击了第 2 页其突出显示的第 3 页 ...
  • 是的,当然你也应该更新那个表达式,因为你的currentPage实际上是n+1,所以例如它可以是{active: (n+1) === currentPage}
猜你喜欢
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 2019-10-09
  • 2023-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多