简体   繁体   English

如何使用angular和bootstrap-ui初始化下拉菜单

[英]How to initialize dropdown menu with angular and bootstrap-ui

Following some tutorials, I have realized a basic pagination with Angular and Bootstrab. 通过一些教程,我已经实现了对Angular和Bootstrab的基本分页。

What I can't figure out is how to initialize the dropdown menu to choose how many records per page using my angular controller (so that the option $scope.viewby ( = 3) is selected when launching the app. 我不知道如何使用我的角度控制器初始化下拉菜单以选择每页多少条记录(以便在启动应用程序时选择$scope.viewby (= 3)选项。

Using <option selected> is not what I want (and also it doesn't work). 使用<option selected>不是我想要的(而且它也不起作用)。

 var mockData = [{ "id": 1, "name": "Spizaetus coronatus" }, { "id": 2, "name": "Priodontes maximus" }, { "id": 3, "name": "Gekko gecko" }, { "id": 4, "name": "Aonyx capensis" }, { "id": 5, "name": "Spermophilus lateralis" }, { "id": 6, "name": "Aegypius occipitalis" }, { "id": 7, "name": "Geochelone elephantopus" }] var app = angular.module('myApp', ['ui.bootstrap']) app.controller('paginate', function($scope, $filter) { $scope.data = mockData $scope.viewby = 3 $scope.totalItems = $scope.data.length $scope.currentPage = 2 $scope.itemsPerPage = $scope.viewby $scope.maxSize = 5; //Number of pager buttons to show $scope.limits = [3, 5, 10, 20] $scope.setPage = function(pageNo) { $scope.currentPage = pageNo }; $scope.pageChanged = function() { console.log('Page changed to: ' + $scope.currentPage) }; $scope.setItemsPerPage = function(num) { console.log("current viewby: ", $scope.viewby) $scope.itemsPerPage = num $scope.currentPage = 1 //reset to first page } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script> <div ng-app='myApp'> <div ng-controller='paginate'> <table class="table"> <tr ng-repeat="row in data.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage))"> <td>{{row.name}}</td> <td>{{row.id}}</td> </tr> </table> View <select ng-model="viewby" ng-change="setItemsPerPage(viewby)"> <option>3</option> <option>5</option> <option>10</option> <option>20</option> </select> records at a time. <div uib-pagination num-pages="numPages" total-items="totalItems" items-per-page="itemsPerPage" ng-model="currentPage" class="pagination-sm" max-size="maxSize" rotate="false" boundary-links="true" ng-change="pageChanged()"> </div> </div> </div> 

You are missing the value attribute of your option s. 您缺少选项value属性。

If you put values into option you'll see the selected value. 如果将值放入选项,您将看到所选值。

Note that the value's and variable type must be same type (int,string etc...) 请注意,值和变量类型必须为同一类型(int,string等...)

<select ng-model="viewby" ng-change="setItemsPerPage(viewby)">
          <option value="3">3</option>
          <option value="5">5</option>
          <option value="10">10</option>
          <option value="20">20</option>
</select>

 var mockData = [{ "id": 1, "name": "Spizaetus coronatus" }, { "id": 2, "name": "Priodontes maximus" }, { "id": 3, "name": "Gekko gecko" }, { "id": 4, "name": "Aonyx capensis" }, { "id": 5, "name": "Spermophilus lateralis" }, { "id": 6, "name": "Aegypius occipitalis" }, { "id": 7, "name": "Geochelone elephantopus" }] var app = angular.module('myApp', ['ui.bootstrap']) app.controller('paginate', function($scope, $filter) { $scope.data = mockData $scope.viewby = '3' $scope.totalItems = $scope.data.length $scope.currentPage = 2 $scope.itemsPerPage = $scope.viewby $scope.maxSize = 5; //Number of pager buttons to show $scope.limits = [3, 5, 10, 20] $scope.setPage = function(pageNo) { $scope.currentPage = pageNo }; $scope.pageChanged = function() { console.log('Page changed to: ' + $scope.currentPage) }; $scope.setItemsPerPage = function(num) { console.log("current viewby: ", $scope.viewby) $scope.itemsPerPage = num $scope.currentPage = 1 //reset to first page } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script> <div ng-app='myApp'> <div ng-controller='paginate'> <table class="table"> <tr ng-repeat="row in data.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage))"> <td>{{row.name}}</td> <td>{{row.id}}</td> </tr> </table> View <select ng-model="viewby" ng-change="setItemsPerPage(viewby)"> <option value="3">3</option> <option value="5">5</option> <option value="10">10</option> <option value="20">20</option> </select> records at a time. <div uib-pagination num-pages="numPages" total-items="totalItems" items-per-page="itemsPerPage" ng-model="currentPage" class="pagination-sm" max-size="maxSize" rotate="false" boundary-links="true" ng-change="pageChanged()"> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM