【问题标题】:Angular UI bootstrap pagination not working inside ui.bootstrap.tabsAngular UI 引导分页在 ui.bootstrap.tabs 中不起作用
【发布时间】:2015-06-30 20:34:59
【问题描述】:

Angular UI 引导程序提供了很棒的解决方案。非常不幸的是 Pagination (ui.bootstrap.pagination) 在 Tabs (ui.bootstrap.tabs) 中不起作用

演示 http://plnkr.co/edit/mLBkDwEVY6SIh5oCyy0K?p=preview

angular.module('mytodos', ['ui.bootstrap']).controller('TodoController', function($scope) {
  $scope.filteredTodos = [];
  $scope.currentPage = 1;
  $scope.numPerPage = 10;
  $scope.maxSize = 5;
  $scope.todos = [{
    "id": 1,
    "date": "12-Jan-2015",
    "cheque_no": 11111,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 2,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 3,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 4,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 5,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 6,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 7,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 8,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 9,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 10,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 11,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 12,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 13,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 14,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 15,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 16,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 17,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 18,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 19,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 20,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 21,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 22,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 23,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 24,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 25,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 26,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 27,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 28,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 29,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 30,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 31,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 32,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 33,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 34,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 35,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 36,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 37,
    "date": "22-February-2015",
    "cheque_no": 222222,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 38,
    "date": "14-March-2015",
    "cheque_no": 33333,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 39,
    "date": "16-April-2015",
    "cheque_no": 44444,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }, {
    "id": 40,
    "date": "30-May-2015",
    "cheque_no": 55555,
    "withdraw": 126.01,
    "deposit": 236.00,
    "balance": 953.00
  }];

  $scope.$watch('currentPage + numPerPage', function() {
    var begin = (($scope.currentPage - 1) * $scope.numPerPage),
      end = begin + $scope.numPerPage;
    $scope.filteredTodos = $scope.todos.slice(begin, end);
  });
});
.box {
  border: 1px solid #DDD;
  padding: 10px;
  position: relative;
  background: #FFF;
  margin: 0 0 10px;
}
.tab-content > .tab-pane > .box {
  border-top: none;
}
.tab-content > .box {
  border-top: none;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.min.js"></script>
<div ng-app="mytodos" ng-controller="TodoController">
  <div class="container">

    <tabset>
      <tab>
        <tab-heading>Statement</tab-heading>
        <div class="box">
          <h1>Todos</h1>
          <h4>{{todos.length}} total</h4>
          <ul>
            <li ng-repeat="todo in filteredTodos">{{todo.id}} Index: {{$index}}</li>
          </ul>
          <pagination ng-model="currentPage" total-items="todos.length" max-size="maxSize" boundary-links="true">
          </pagination>
        </div>
      </tab>
    </tabset>
  </div>
</div>

【问题讨论】:

    标签: angularjs pagination angularjs-scope angular-ui-bootstrap angular-ui-bootstrap-tab


    【解决方案1】:

    您的$watch不起作用!如果您将console.log($scope.currentPage) 放入您的$watch 函数中,您会看到页面更改时不会有输出。

    我建议在分页元素上使用ng-change 并在作用域上定义一个函数(而不是$watch)!

    @见http://angular-ui.github.io/bootstrap/#/pagination

    为确保 currentPage 模型已更新,您可以设置一个对象而不仅仅是一个整数(范围继承的关键!)

    // JS
    $scope.pagination = { currentPage: 1 };
    // HTML
    <pagination ng-model="pagination.currentPage">
    

    我推荐阅读这篇文章:https://github.com/angular/angular.js/wiki/Understanding-Scopes

    【讨论】:

    • 我尝试使用 ng-change,$scope.currentPage 也没有更新。
    • @Betty St 感谢您的努力。从这里我得到了不同的解决方案github.com/angular-ui/bootstrap/issues/3881。作为初学者,我有点难以理解:(
    • 对不起,我更新了我的答案,我认为这是一个简单的范围继承问题!
    • 我遇到了两个分页不共享相同内容的相同状态的问题(一个在顶部,一个在底部)。将所有与状态相关的值保存在一个对象中,就像 $scope.pagination = { currentPage: 1 }; 一样,谢谢。
    【解决方案2】:

    终于找到了filter()的解决方案,而不是担心$scope

    angular.module('mytodos', ['ui.bootstrap']).controller('TodoController', function($scope) {
      $scope.filteredTodos = [];
      $scope.currentPage = 1;
      $scope.numPerPage = 10;
      $scope.maxSize = 5;
      $scope.todos = [{
        "id": 1,
        "date": "12-Jan-2015",
        "cheque_no": 11111,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 2,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 3,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 4,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 5,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 6,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 7,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 8,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 9,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 10,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 11,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 12,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 13,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 14,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 15,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 16,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 17,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 18,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 19,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 20,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 21,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 22,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 23,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 24,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 25,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 26,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 27,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 28,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 29,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 30,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 31,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 32,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 33,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 34,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 35,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 36,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 37,
        "date": "22-February-2015",
        "cheque_no": 222222,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 38,
        "date": "14-March-2015",
        "cheque_no": 33333,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 39,
        "date": "16-April-2015",
        "cheque_no": 44444,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }, {
        "id": 40,
        "date": "30-May-2015",
        "cheque_no": 55555,
        "withdraw": 126.01,
        "deposit": 236.00,
        "balance": 953.00
      }];
    }).filter('pagination', function() {
    	  return function(input, currentPage, pageSize) {
    	    if(angular.isArray(input)) {
    	      var start = (currentPage-1)*pageSize;
    	      var end = currentPage*pageSize;
    	      return input.slice(start, end);
    	    }
    	  };
    	});
    .box {
      border: 1px solid #DDD;
      padding: 10px;
      position: relative;
      background: #FFF;
      margin: 0 0 10px;
    }
    .tab-content > .tab-pane > .box {
      border-top: none;
    }
    .tab-content > .box {
      border-top: none;
    }
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.min.js"></script>
    <div ng-app="mytodos" ng-controller="TodoController">
      <div class="container">
    
        <tabset>
          <tab>
            <tab-heading>Statement</tab-heading>
            <div class="box">
              <h1>Todos</h1>
              <h4>{{todos.length}} total</h4>
              <ul>
                <li ng-repeat="todo in todos| pagination: currentPage : numPerPage">{{todo.id}} Index: {{$index}}</li>
              </ul>
              <pagination ng-model="currentPage" total-items="todos.length" max-size="maxSize" boundary-links="true">
              </pagination>
            </div>
          </tab>
        </tabset>
      </div>
    </div>

    【讨论】:

      【解决方案3】:

      试试这个

      <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>
      

      【讨论】:

        猜你喜欢
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-27
        • 1970-01-01
        • 2017-03-05
        • 2016-12-21
        • 1970-01-01
        相关资源
        最近更新 更多