美文网首页
Angulars分页加载功能详解

Angulars分页加载功能详解

作者: 哈酒拎壶冲 | 来源:发表于2017-06-19 16:56 被阅读62次

    因公司项目要求 最近在写H5项目 遇到了关于分页加载list列表的问题 网上差了一些资料 经过自己整理 完美的实现了分页加载功能 接下来我将记录下详细的实现流程 自己做个记录 也希望可以帮助到其他需要的人

    接下来我将详细的写出html文件、JS文件、CSS样式文件来详细完整的呈现出分页的实现流程(JS是重点部分)。


    首先设置html文件

    <!doctype html>
    //augular设置的app名称
    <html ng-app="myapp">
    <head>
        <meta charset="UTF-8">
    //引入bootstrap的样式文件
        <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.min.css">
    //引入CSS样式文件(自定义的样式文件)
        <link rel="stylesheet" type="text/css" href="../css/FrontnewsAll.css" />
            
    </head>
    //angular定义的controller
    <body ng-controller="map_ctrl">
    //自定义界面
    /*
    <div id="" style="margin-left: 40px;margin-top: 20px;" ng-controller="map_ctrl">
                <p>
    
                    <a href="Dashboard.html" style="color: #000000;font-size: 12px;text-decoration:none;">舆情中心</a> >
                    <a style="color: #000000;font-size: 12px;text-decoration:none;">负面舆情</a>
                </p>
    </div>
    */
    <div class="row" style="margin-top: 20px;margin-left: 10px;" id="book">
                <div id="" style="margin-top: 10px;" >
    //这是augular创建列表 ng-repeat 属性 list是所有数据 n相当于model href使用{{n.url}}跳转链接 其他属性直接n.
                    <ul class="list-group" style="margin-top: -3px;" ng-repeat="n in list">
                        <li style="">
                            <a href="{{n.url}}" ng-click="" class="" style=" color: #333333;font-size: 14px;width: 100%;word-break:break-all;display:-webkit-box;-webkit-line-clamp:1;-webkit-box-orient:vertical;overflow:hidden;" ng-bind="n.title" target=black>
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
    <!-- paging -->  
    //下面是分页空间界面
    
    <ul class="pagination" style="margin: 0px;" >  
            <li ng-class="{true:'disabled'}[p_current==1]"><a href="javascript:void(0);" ng-click="p_index()">首页</a></li>  
            <li ng-repeat="page in pages" ng-class="{true:'active'}[p_current==page]"><a href="javascript:void(0);" ng-click="load_page(page)">{{ page }}</a></li>  
            <li ng-class="{true:'disabled'}[p_current==p_all_page]"><a href="javascript:void(0);" ng-click="p_last()">尾页</a></li>  
    </ul>  
    <span style="vertical-align: 12px;">  共:{{count}} 条</span> 
    //angular.min.js必须倒入
    <script src="../vendor/jquery/jquery.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../vendor/angular.min.js"></script>
    <script src="../js/Dashboard/Allsnews_fengye_Negative.js" type="text/javascript" charset="utf-8"></script>
    </body>
    </html>
    

    设置JS文件

    //设置app(必须写)
    var app = angular.module("myapp",[]);     
    //设置ng-controller;
    app.controller("map_ctrl",function($scope,$http,$location){ 
        //配置  
        $scope.count = 0;  
       //每页加载数据数
        $scope.p_pernum = 10;  
        //index  
        $scope.p_current = 1; 
       //初始化第几页 
        $scope.p_all_page =0;  
      //总页数
        $scope.pages = [];  
        //初始化第一页  
    
        //获取数据  注意:angular 所有算法必须 按 $scope. 这种方式写    
        //获取数据算法  具体详情 自己去了解angular post请求
        $scope.get = function(page,size,callback){
        var req3 = {
            method: 'post',
            url: "../ashx/news.ashx",
            params: {
                type: "list",
                PageSize: size,
                PageIndex:  page,
                newsType:"1",
                key:"zs999999",
            }
        }
        $http(req3).then(
            function success(res) {  
                    $scope.count=res.data.Count; 
                    // angular绑定list列表数据
                    $scope.list=res.data.R;  
                    $scope.p_current = page;  
                    $scope.p_all_page = Math.ceil($scope.count/size);  
                    reloadPno();  
                    callback();  
    
            },
            function err(e) {
                console.log("err");
            });
    
          }
    //初始化加载
         $scope.get($scope.p_current,$scope.p_pernum,function(){  
    
        });
    //  }  
        //单选按钮选中  
        $scope.select= function(id){  
    //      alert(id);  
        }  
        //首页  
        $scope.p_index = function(){  
            $scope.load_page(1);  
        }  
        //尾页  
        $scope.p_last = function(){  
            $scope.load_page($scope.p_all_page);  
        }  
        //加载某一页  
        $scope.load_page = function(page){  
            $scope.get(page,$scope.p_pernum,function(){ });  
        };  
        //初始化页码  
        var reloadPno = function(){  
              $scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,10);  
            };  
    //分页算法  (转载之[齐玉林](http://my.csdn.net/qilin001cs)博客算法)
    var calculateIndexes = function (current, length, displayLength) {  
       var indexes = [];  
       var start = Math.round(current - displayLength / 2);  
       var end = Math.round(current + displayLength / 2);  
        if (start <= 1) {  
            start = 1;  
            end = start + displayLength - 1;  
           if (end >= length - 1) {  
               end = length - 1;  
            }  
        }  
        if (end >= length - 1) {  
           end = length;  
            start = end - displayLength + 1;  
           if (start <= 1) {  
               start = 1;  
            }  
        }  
        for (var i = start; i <= end; i++) {  
            indexes.push(i);  
        }  
        return indexes;  
       };     
    }); 
    

    CSS样式文件

    #book ul {
                    list-style-type: none;
                }
                
                #book ul li {
                    border-bottom: 1px solid #999999;
                    ;
                    height: 30px;
                    line-height: 30px;
                    margin-left: 20px;
                }
                
                #book ul li a {
                    border-left: 3px solid #6A8AFC;
                    padding-left: 8px;
                    text-decoration: none;
                    color: #666;
                }
                
                #book ul li a:hover {
                    color: #58DB5A;
                    cursor: pointer;
                }
                
                #book .info {
                    border-bottom: 1px solid #999999;
                    line-height: 30px;
                    margin-left: 10px;
                }
                
                #book .jianjie {
                    margin-left: 5px;
                    color: #666!important;
                }
                
                .editor-container {
                    background-color: #fff!important;
                    width: 100%;
                    height: 100%;
                    font-size: 15px!important;
                    font-family: Verdana, Geneva, Tahoma, sans-serif!important;
                }
                
                .htitle {
                    left: 50%;
                    top: 20%;
                    display: inline-block;
                    position: absolute;
                    color: white;
                    margin-left: -100px;
                }
                .page-list .pagination {
                    float: left;
                }
                
                .page-list .pagination span {
                    cursor: pointer;
                }
                
                .page-list .pagination .separate span {
                    cursor: default;
                    border-top: none;
                    border-bottom: none;
                }
                
                .page-list .pagination .separate span:hover {
                    background: none;
                }
                
                .page-list .page-total {
                    float: left;
                    margin: 25px 20px;
                }
                
                .page-list .page-total input,
                .page-list .page-total select {
                    height: 26px;
                    border: 1px solid #ddd;
                }
                
                .page-list .page-total input {
                    width: 40px;
                    padding-left: 3px;
                }
                
                .page-list .page-total select {
                    width: 50px;
                }
                
    

    OK 到这里问题就完美的实现了分页功能 我们的接口因为封装 如果其他人使用接口请求数据需要自己实现 其他功能可以直接搬用 希望会对大家有帮助

    相关文章

      网友评论

          本文标题:Angulars分页加载功能详解

          本文链接:https://www.haomeiwen.com/subject/qjsgqxtx.html