我在内存中有大约1000项的数据集,并试图创建一个寻呼机 这个数据集,但我不确定怎么做。

我正在使用一个自定义过滤器函数来过滤结果,这工作得很好,但我需要以某种方式获得页数。

有线索吗?


当前回答

这是我的例子。选中按钮在列表中间 控制器。 配置> > >

 $scope.pagination = {total: null, pages: [], config: {count: 10, page: 1, size: 7}};

分页逻辑:

/*
     Pagination
     */
    $scope.$watch('pagination.total', function (total) {
        if(!total || total <= $scope.pagination.config.count) return;
        _setPaginationPages(total);
    });

    function _setPaginationPages(total) {
        var totalPages = Math.ceil(total / $scope.pagination.config.count);
        var pages = [];
        var start = $scope.pagination.config.page - Math.floor($scope.pagination.config.size/2);
        var finish = null;

        if((start + $scope.pagination.config.size - 1) > totalPages){
            start = totalPages - $scope.pagination.config.size;
        }
        if(start <= 0) {
            start = 1;
        }

       finish = start +  $scope.pagination.config.size - 1;
       if(finish > totalPages){
           finish = totalPages;
       }


        for (var i = start; i <= finish; i++) {
            pages.push(i);
        }

        $scope.pagination.pages = pages;
    }

    $scope.$watch("pagination.config.page", function(page){
        _setPaginationPages($scope.pagination.total);
        _getRespondents($scope.pagination.config);
    });

以及我对引导的看法

<ul ng-class="{hidden: pagination.total == 0}" class="pagination">
        <li ng-click="pagination.config.page = pagination.config.page - 1"
            ng-class="{disabled: pagination.config.page == 1}" ><a href="#">&laquo;</a></li>
        <li ng-repeat="p in pagination.pages"
            ng-click="pagination.config.page = p"
            ng-class="{active: p == pagination.config.page}"><a href="#">{{p}}</a></li>
        <li ng-click="pagination.config.page = pagination.config.page + 1"
            ng-class="{disabled: pagination.config.page == pagination.pages.length}"><a href="#">&raquo;</a></li>
    </ul >

它很有用

其他回答

概述:分页使用

 - ng-repeat
 - uib-pagination

观点:

<div class="row">
    <div class="col-lg-12">
        <table class="table">
            <thead style="background-color: #eee">
                <tr>
                    <td>Dispature</td>
                    <td>Service</td>
                    <td>Host</td>
                    <td>Value</td>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="x in app.metricsList">
                    <td>{{x.dispature}}</td>
                    <td>{{x.service}}</td>
                    <td>{{x.host}}</td>
                    <td>{{x.value}}</td>
                </tr>
            </tbody>
        </table>

        <div align="center">
            <uib-pagination items-per-page="app.itemPerPage" num-pages="numPages"
                total-items="app.totalItems" boundary-link-numbers="true"
                ng-model="app.currentPage" rotate="false" max-size="app.maxSize"
                class="pagination-sm" boundary-links="true"
                ng-click="app.getPagableRecords()"></uib-pagination>        

            <div style="float: right; margin: 15px">
                <pre>Page: {{app.currentPage}} / {{numPages}}</pre>
            </div>          
        </div>
    </div>
</div>

JS控制器:

app.controller('AllEntryCtrl',['$scope','$http','$timeout','$rootScope', function($scope,$http,$timeout,$rootScope){

    var app = this;
    app.currentPage = 1;
    app.maxSize = 5;
    app.itemPerPage = 5;
    app.totalItems = 0;

    app.countRecords = function() {
        $http.get("countRecord")
        .success(function(data,status,headers,config){
            app.totalItems = data;
        })
        .error(function(data,status,header,config){
            console.log(data);
        });
    };

    app.getPagableRecords = function() {
        var param = {
                page : app.currentPage,
                size : app.itemPerPage  
        };
        $http.get("allRecordPagination",{params : param})
        .success(function(data,status,headers,config){
            app.metricsList = data.content;
        })
        .error(function(data,status,header,config){
            console.log(data);
        });
    };

    app.countRecords();
    app.getPagableRecords();

}]);

前面的消息基本上推荐了如何自己构建分页。如果你和我一样,更喜欢一个完整的指令,我刚刚发现了一个很棒的指令,叫做ngTable。它支持排序、过滤和分页。

这是一个非常干净的解决方案,你所需要的是:

   <table ng-table="tableParams" class="table">
        <tr ng-repeat="user in $data">
            <td data-title="'Name'" sortable="'name'">
                {{user.name}}
            </td>
            <td data-title="'Age'" sortable="'age'">
                {{user.age}}
            </td>
        </tr>
    </table>

在控制器中:

$scope.tableParams = new ngTableParams({
    page: 1,            // show first page
    count: 10,          // count per page
    sorting: {
        name: 'asc'     // initial sorting
    }
}, {
    total: data.length, // length of data
    getData: function($defer, params) {
        // use build-in angular filter
        var orderedData = params.sorting() ?
                            $filter('orderBy')(data, params.orderBy()) :
                            data;

        var start = (params.page() - 1) * params.count();
        var end = params.page() * params.count();

        $defer.resolve(orderedData.slice( start, end));
    }
});

GitHub链接:https://github.com/esvit/ng-table/

我想添加我的解决方案,与ngRepeat和过滤器,你使用它不使用$watch或切片数组。

您的筛选结果将分页!

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

app.controller('myController', ['$scope', function($scope){
    $scope.list= ['a', 'b', 'c', 'd', 'e'];

    $scope.pagination = {
        currentPage: 1,
        numPerPage: 5,
        totalItems: 0
    };

    $scope.searchFilter = function(item) {
        //Your filter results will be paginated!
        //The pagination will work even with other filters involved
        //The total number of items in the result of your filter is accounted for
    };

    $scope.paginationFilter = function(item, index) {
        //Every time the filter is used it restarts the totalItems
        if(index === 0) 
            $scope.pagination.totalItems = 0;

        //This holds the totalItems after the filters are applied
        $scope.pagination.totalItems++;

        if(
            index >= (($scope.pagination.currentPage - 1) * $scope.pagination.numPerPage)
            && index < ((($scope.pagination.currentPage - 1) * $scope.pagination.numPerPage) + $scope.pagination.numPerPage)
        )
            return true; //return true if item index is on the currentPage

        return false;
    };
}]);

在HTML中,确保在分页过滤器之前对ngRepeat应用过滤器。

<table data-ng-controller="myController">
    <tr data-ng-repeat="item in list | filter: searchFilter | filter: paginationFilter track by $index">
        <td>
            {{item}}
        </td>
    <tr>
</table>
<ul class="pagination-sm"
    uib-pagination
    data-boundary-links="true"
    data-total-items="pagination.totalItems"
    data-items-per-page="pagination.numPerPage"
    data-ng-model="pagination.currentPage"
    data-previous-text="&lsaquo;"
    data-next-text="&rsaquo;"
    data-first-text="&laquo;"
    data-last-text="&raquo;">
 </ul>

jQuery Mobile angular适配器有一个分页过滤器,你可以基于它。

下面是一个使用它的演示小提琴(添加超过5个项目,它就会变成页面):http://jsfiddle.net/tigbro/Du2DY/

来源如下:https://github.com/tigbro/jquery-mobile-angular-adapter/blob/master/src/main/webapp/utils/paging.js

下面的代码将有助于在后端提供自定义分页角repeat。

你的数据会在

$scope.myticketIssuesData = [];
$scope.allticketIssuesData = [];

var jiraapp = angular.module('jiraapp', ['ui.bootstrap']); jiraapp.controller('JiraController', ['$scope', '$http', '$window','$location', function JiraController($scope, $http, $window,$location) { $scope.myticketIssuesData = []; $scope.allticketIssuesData = []; $scope.jiraIssue = {}; $scope.RequesterType = []; $scope.loading = false; $scope.showerror = false; $scope.alert = {}; $scope.maxSize = 10; $scope.totalCount = 0; $scope.pageIndex = 0; $scope.startIndex = 0; $scope.pageSizeSelected = 10; $scope.maxallSize = 10; $scope.totalallCount = 0; $scope.pageallIndex = 0; $scope.startallIndex = 0; $scope.pageallSizeSelected = 10; $scope.getUserTickets = function() { $scope.loading = true; $http({ method: 'GET', url: 'http://localhost:53583/api/Jira/getUserTickets?assignee='+$scope.loc+'&startAt='+ $scope.startIndex +'&maxResults='+$scope.pageSizeSelected, headers: { "Accept": "application/json", "Access-Control-Allow-Origin": "http://localhost:8080", "crossDomain": "true", } }).then(function successCallback(response) { $scope.myticketIssuesData = response.data.issues; $scope.totalCount = response.data.total; $scope.loading = false; }, function errorCallback(response) { $scope.loading = false; }); } $scope.getrequestType = function(){ $http({ method: 'GET', url: 'http://localhost:53583/api/Jira/getrequestType', headers: { "Accept": "application/json", "Access-Control-Allow-Origin": "http://localhost:8080", "crossDomain": "true", } }).then(function successCallback(response) { $scope.RequesterType = response.data.values; }, function errorCallback(response) { }); } $scope.getDropDown = function(){ $scope.getrequestType(); } $scope.initialize = function (item) { $scope.getUserTickets(); $scope.getDropDown(); } $scope.initialize(); $scope.pageChanged = function () { if($scope.pageIndex == 0) $scope.startIndex = 0; else if($scope.pageIndex == 1) $scope.startIndex = 0; else $scope.startIndex = (($scope.pageIndex-1) * $scope.pageSizeSelected); $scope.getUserTickets(); }; $scope.pageallChanged = function () { if($scope.pageallIndex == 0) $scope.startallIndex = 0; else if($scope.pageallIndex == 1) $scope.startallIndex = 0; else $scope.startallIndex = (($scope.pageallIndex-1) * $scope.pageallSizeSelected); $scope.getAllTickets(); }; $scope.changeallPageSize = function () { $scope.pageallIndex = 0; $scope.getAllTickets(); }; $scope.getAllTickets = function() { $scope.loading = true; $http({ method: 'GET', url: 'http://localhost:53583/api/Jira/getAllTickets?startAt='+ $scope.startallIndex +'&maxResults='+$scope.pageallSizeSelected, headers: { "Accept": "application/json", "Access-Control-Allow-Origin": "http://localhost:8080", "crossDomain": "true", } }).then(function successCallback(response) { $scope.allticketIssuesData = response.data.issues; $scope.totalallCount = response.data.total; $scope.loading = false; }, function errorCallback(response) { $scope.loading = false; }); } }]); <html ng-app="jiraapp"> <head> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" crossorigin="anonymous"> <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" crossorigin="anonymous"></script> <script src="/angular.min.js"></script> <script src="/jira.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular-route.min.js"></script> <script src="/ui-bootstrap-tpls-0.13.4.min.js"></script> <!-- this is important --> <style type="text/css"> #loading { position: fixed; top: 50%; left: 50%; margin-top: -5em; margin-left: -10em; } .pagination { display: inline-block; padding-left: 0; margin: 20px 0; border-radius: 4px } .pagination>li { display: inline } .pagination>li>a, .pagination>li>span { position: relative; float: left; padding: 6px 12px; margin-left: -1px; line-height: 1.42857143; color: #337ab7; text-decoration: none; background-color: #fff; border: 1px solid #ddd } .pagination>li:first-child>a, .pagination>li:first-child>span { margin-left: 0; border-top-left-radius: 4px; border-bottom-left-radius: 4px } .pagination>li:last-child>a, .pagination>li:last-child>span { border-top-right-radius: 4px; border-bottom-right-radius: 4px } .pagination>li>a:focus, .pagination>li>a:hover, .pagination>li>span:focus, .pagination>li>span:hover { z-index: 3; color: #23527c; background-color: #eee; border-color: #ddd } .pagination>.active>a, .pagination>.active>a:focus, .pagination>.active>a:hover, .pagination>.active>span, .pagination>.active>span:focus, .pagination>.active>span:hover { z-index: 2; color: #fff; cursor: default; background-color: #337ab7; border-color: #337ab7 } .pagination>.disabled>a, .pagination>.disabled>a:focus, .pagination>.disabled>a:hover, .pagination>.disabled>span, .pagination>.disabled>span:focus, .pagination>.disabled>span:hover { color: #777; cursor: not-allowed; background-color: #fff; border-color: #ddd } .pagination-lg>li>a, .pagination-lg>li>span { padding: 10px 16px; font-size: 18px; line-height: 1.3333333 } .pagination-lg>li:first-child>a, .pagination-lg>li:first-child>span { border-top-left-radius: 6px; border-bottom-left-radius: 6px } .pagination-lg>li:last-child>a, .pagination-lg>li:last-child>span { border-top-right-radius: 6px; border-bottom-right-radius: 6px } .pagination-sm>li>a, .pagination-sm>li>span { padding: 5px 10px; font-size: 12px; line-height: 1.5 } .pagination-sm>li:first-child>a, .pagination-sm>li:first-child>span { border-top-left-radius: 3px; border-bottom-left-radius: 3px } .pagination-sm>li:last-child>a, .pagination-sm>li:last-child>span { border-top-right-radius: 3px; border-bottom-right-radius: 3px } .pager { padding-left: 0; margin: 20px 0; text-align: center; list-style: none } .pager li { display: inline } .pager li>a, .pager li>span { display: inline-block; padding: 5px 14px; background-color: #fff; border: 1px solid #ddd; border-radius: 15px } .pager li>a:focus, .pager li>a:hover { text-decoration: none; background-color: #eee } .pager .next>a, .pager .next>span { float: right } .pager .previous>a, .pager .previous>span { float: left } .pager .disabled>a, .pager .disabled>a:focus, .pager .disabled>a:hover, .pager .disabled>span { color: #777; cursor: not-allowed; background-color: #fff } </style> </head> <body ng-controller="JiraController"> <div class="col-sm-12"> <div class="row" style="background: #09c;"> <div style="margin-left: auto; margin-right: auto;"> <img src="/logo.png" height="80"> <span class="d-none d-sm-inline" style="color: white; font-size: 4rem; vertical-align: middle; font-family:'Source Code Pro'">Jira</span> </div> </div> <div class="row"> <div class="col-sm-12"> <nav> <div class="nav nav-tabs" id="nav-tab" role="tablist"> <a class="nav-item nav-link active" id="nav-myticket-tab" data-toggle="tab" href="#nav-myticket" role="tab" aria-controls="nav-myticket" aria-selected="true" ng-click="getUserTickets()">My Ticket</a> </div> </nav> <div class="tab-content" id="nav-tabContent"> <div class="tab-pane fade show active" id="nav-myticket" role="tabpanel" aria-labelledby="nav-myticket-tab"> <div class="col-sm-12" style="margin:10px"> <div id="loading" ng-show="loading"> <img src="spinner.gif"> </div> <table ng-show="!loading" class="table table-striped table-bordered table-hover tabel-condensed"> <thead> <tr> <td>Key</td> <td>Priority</td> <td>Summary</td> <td>Assignee</td> <td>Status</td> <td>Due Date</td> </tr> </thead> <tbody> <tr ng-repeat="data in myticketIssuesData"> <td> <a href={{data.fields.customfield_10023._links.web}} target="_blank"> {{data.key}} </a> </td> <td>{{data.fields.priority.name}}</td> <td>{{data.fields.summary}}</td> <td>{{data.fields.assignee.displayName}}</td> <td>{{data.fields.status.name}}</td> <td>{{data.fields.duedate}}</td> </tr> </tbody> <tfoot> <tr> <td align="center" colspan="6"> <!-- <span class="form-group pull-left page-size form-inline"> <select id="ddlPageSize" class="form-control control-color" ng-model="pageSizeSelected" ng-change="changePageSize()"> <option value="5">5</option> <option value="10">10</option> <option value="25">25</option> <option value="50">50</option> </select> </span> --> <div class="pull-right"> <pagination total-items="totalCount" ng-change="pageChanged()" items-per-page="pageSizeSelected" direction-links="true" ng-model="pageIndex" max-size="maxSize" class="pagination" boundary-links="true" rotate="false" num-pages="numPages"> </pagination> <a style="margin-left: 640px;" class="btn btn-primary">Page: {{pageIndex}} / {{numPages}}</a> </div> </td> </tr> </tfoot> </table> </div> </div> </div> </div> </div> </body> </html>