gpt4 book ai didi

angularjs - 如何对使用 ng-repeat 生成的表使用 UI Bootstrap 分页

转载 作者:行者123 更新时间:2023-12-02 11:24:34 25 4
gpt4 key购买 nike

我正在尝试使用 uib-pagination 在我的 Angular 应用程序中保持分页.我无法找到正确的方法来做到这一点。

HTML

<table id="mytable" class="table table-striped">
<thead>
<tr class="table-head">
<th>Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in aCandidates">
<th>
<div>{{person}}</div>
</th>
</tr>
</tbody>
</table>

Controller
$scope.totalItems = $scope.aCandidates.length;
$scope.currentPage = 1;
$scope.itemsPerPage = 10;

我能够看到分页栏,但没有执行任何操作,即使将总项目数设为 10,也会呈现整个表格数据。

uib-pagination 的工作原理以及数据(在本例中为 aCandidates )如何链接到分页。

最佳答案

您缺少的部分是您必须有一个函数来从整个数组中获取当前页面的数据。我添加了一个名为 setPagingData() 的函数来处理这个。

您可以在 fork plunker 中看到这一点

var app = angular.module("plunker", ["ui.bootstrap"]);

app.controller("MainCtrl", function($scope) {
var allCandidates =
["name1", "name2", "name3", "name4", "name5",
"name6", "name7", "name8", "name9", "name10",
"name11", "name12", "name13", "name14", "name15",
"name16", "name17", "name18", "name19", "name20"
];

$scope.totalItems = allCandidates.length;
$scope.currentPage = 1;
$scope.itemsPerPage = 5;

$scope.$watch("currentPage", function() {
setPagingData($scope.currentPage);
});

function setPagingData(page) {
var pagedData = allCandidates.slice(
(page - 1) * $scope.itemsPerPage,
page * $scope.itemsPerPage
);
$scope.aCandidates = pagedData;
}
});
<link data-require="bootstrap-css@*" data-semver="4.0.0-alpha.2" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script data-require="ui-bootstrap@*" data-semver="1.3.2" src="https://cdn.rawgit.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-tpls-1.3.2.js"></script>

<div ng-app="plunker">
<div ng-controller="MainCtrl">
<table id="mytable" class="table table-striped">
<thead>
<tr class="table-head">
<th>Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in aCandidates">
<th>
<div>{{person}}</div>
</th>
</tr>
</tbody>
</table>
<uib-pagination total-items="totalItems" ng-model="currentPage" items-per-page="itemsPerPage"></uib-pagination>
</div>
</div>

关于angularjs - 如何对使用 ng-repeat 生成的表使用 UI Bootstrap 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37702042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com