angular表格分页

<!doctype html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="bootstrap.min.css">
    <style>
        #divMain {
            padding:30px 100px;
        }
        nav {
            position: relative;
            width:100%;
            height: 50px;
        }
        .pagination {
            right: 0;
            position: absolute;
            top: -30px;
        }
        nav li {
            cursor: pointer;
        }
        input{
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
<div id="divMain" ng-app="myApp" ng-controller="myCtrl">
    <!--<input ng-model="name">-->
    <input ng-model="search"/> 搜索
    <table class="table table-bordered table-responsive table-striped table-hover">
        <thead class="">
            <tr>
                <th>id</th>
                <th>名称</th>
                <th>内容</th>
                <th style="width:80px;">方法</th>
                <th>方式</th>
            </tr>
        </thead>
        <tr ng-repeat="e in items | filter:search " >
            <td>{{e.id}}</td>
            <td>{{e.item_name}}</td>
            <td>{{e.content}}</td>
            <td>{{e.way}}</td>
            <td>{{e.method}}</td>
        </tr>
    </table>
    <nav>
        <ul class="pagination">
            <li>
                <a ng-click="Previous()">
                    <span>上一页</span>
                </a>
            </li>
            <li ng-repeat="page in pageList" ng-class="{active: isActivePage(page)}" >
                <a ng-click="selectPage(page)" >{{ page }}</a>
            </li>
            <li>
                <a ng-click="Next()">
                    <span>下一页</span>
                </a>
            </li>
        </ul>
    </nav>
</div>
<script src="angular.min.js"></script>
<script>
    var app = angular.module("myApp", []);
    app.controller("myCtrl", function ($scope, $http) {
        $http.get('http://10.6.148.22/catalogue/get_service_item').then(function (response) {
            //数据源
            $scope.data = response.data.data;
            //分页总数
            $scope.pageSize = 15;
            $scope.pages = Math.ceil($scope.data.length / $scope.pageSize); //分页数
            $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
            $scope.pageList = [];
            $scope.selPage = 1;
            //设置表格数据源(分页)
            $scope.setData = function () {
                $scope.items = $scope.data.slice(($scope.pageSize * ($scope.selPage - 1)), ($scope.selPage * $scope.pageSize));
                //通过当前页数筛选出表格当前显示数据
            };
            $scope.items = $scope.data.slice(0, $scope.pageSize);
            //分页要repeat的数组
            for (var i = 0; i < $scope.newPages; i++) {
                $scope.pageList.push(i + 1);
            }
            //打印当前选中页索引
            $scope.selectPage = function (page) {
                //不能小于1大于最大
                if (page < 1 || page > $scope.pages) return;
                //最多显示分页数5
                if (page > 2) {
                    //因为只显示5个页数,大于2页开始分页转换
                    var newpageList = [];
                    for (var i = (page - 3) ; i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)) ; i++) {
                        newpageList.push(i + 1);
                    }
                    $scope.pageList = newpageList;
                    console.log(newpageList)
                }
                $scope.selPage = page;
                $scope.setData();
                $scope.isActivePage(page);
                console.log("选择的页:" + page);
            };
            //设置当前选中页样式
            $scope.isActivePage = function (page) {
                return $scope.selPage == page;
            };
            //上一页
            $scope.Previous = function () {
                $scope.selectPage($scope.selPage - 1);
            };
            //下一页
            $scope.Next = function () {
                $scope.selectPage($scope.selPage + 1);
            };
        });
    })
</script>
</body>
</html>

 

posted @ 2017-01-20 15:20  blog-ze  阅读(761)  评论(0编辑  收藏  举报