填坑之路:前端伪分页

毕设是用ssm+maven来写的,半路上车,遇到了分页的需求。
由于仅仅是毕设,设计的数据量不大,为了多个分页的功能(说不定就因为这个功能加分呢。。。),但又不想去动sql,写limit,于是灵机一动,先把数据取到前端,利用append来动态渲染页面,实现一个伪分页的效果岂不是妙哉。

话不多说,上代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script type="text/javascript" src="js/jquery-2.1.4.min.js" ></script>
        <script type="text/javascript" src="js/Centerm.js" ></script>
        <link rel="stylesheet" href="css/Centerm.css" />
        <title>分页</title>
        <style>
            .table-part{
                overflow: hidden;
            }
        </style>
    </head>
    <body>
        <div class="table-part">
            <div class="table_head">
                <ul class="list-head list">
                    <li class="choose-man choose-all">选择</li>
                    <li class="man-name">姓名</li>
                    <li class="man-email">邮箱</li>
                    <li class="man-position">职位</li>
                    <li class="man-branch">所属部门</li>
                </ul>
            </div>
            <div class="table_body"></div>
            
            <div class="page"></div>
        </div>
        <script>
            $(function(){
                // 模拟了从数据库中返回的Json数据
                var user = [{"id":1,"account":"yangyan@163.com","name":"啦啦啦","password":"6223A9E9F170AD4AC5FCDBD748020B72E9CCBC30877F929C9C6A0D7F","status":2,"deptId":1,"grade":1,"department":{"dId":1,"level":0,"dName":"升腾智能研发中心","pid":null}},{"id":17,"account":"111@test.com","name":"111","password":"F20941E2CEAAFF0AEE7B48500E7B85472155FFDF2E72DB9075CEBB8E","status":2,"deptId":1,"grade":1,"department":{"dId":1,"level":0,"dName":"升腾智能研发中心","pid":null}},{"id":18,"account":"122@test.com","name":"122","password":"5BE1612EAA0D56FDE474AE057E4E8270067DD57CD7ADEFAA92271B70","status":2,"deptId":1,"grade":1,"department":{"dId":1,"level":0,"dName":"升腾智能研发中心","pid":null}},{"id":4,"account":"yjz@test.com","name":"yjz","password":"A59E1F262DED280DC434CE1244B3E8D1D1434A7C751316D087EF5FE6","status":2,"deptId":101,"grade":2,"department":{"dId":101,"level":1,"dName":"产品经理组","pid":1}},{"id":8,"account":"22@test.com","name":"22","password":"318B0D08889C7C3C4382065FF1DCF79DAE78AC466DDC683D92236C6B","status":2,"deptId":10207,"grade":2,"department":{"dId":10207,"level":2,"dName":"深圳研究中心","pid":102}},{"id":7,"account":"11@test.com","name":"11","password":"FECB6EFE632F3451D8E7C462A33EB57386CDDC9D602C6192BE18D941","status":2,"deptId":10301,"grade":2,"department":{"dId":10301,"level":2,"dName":"软件-接口应用部","pid":103}},{"id":2,"account":"yy@test.com","name":"哦哦哦","password":"8C20AEF91678539D184392500AD135839355F4B61C9D10566BE06658","status":2,"deptId":10201,"grade":3,"department":{"dId":10201,"level":2,"dName":"PC组","pid":102}},{"id":6,"account":"y@test.com","name":"olabala","password":"CB6CF3CE0118CE422B712B6FF95B6ADAAA3345844B9D5DD2E240A60C","status":2,"deptId":10201,"grade":3,"department":{"dId":10201,"level":2,"dName":"PC组","pid":102}},{"id":3,"account":"test@163.com","name":"test","password":"B16006741243AB13CE87E50E96E1BAAE291252523E7A7439771AF814","status":2,"deptId":10202,"grade":3,"department":{"dId":10202,"level":2,"dName":"认证研究组","pid":102}},{"id":9,"account":"33@test.com","name":"33","password":"0D636329AA7B169155B58A8018746D477D3A5DD629775D749A275E79","status":2,"deptId":10206,"grade":3,"department":{"dId":10206,"level":2,"dName":"供应商管理组","pid":102}},{"id":5,"account":"aaa@153.com","name":"yyy","password":"A8D912D74516037EAF00C0B36F0F41B3CF654FB663C1B636325725D2","status":2,"deptId":10206,"grade":3,"department":{"dId":10206,"level":2,"dName":"供应商管理组","pid":102}},{"id":14,"account":"88@test.com","name":"88","password":"D10695CCBD2D39D5F5A69475E0DA802009E3C24E638C696CD94DF54C","status":2,"deptId":10301,"grade":3,"department":{"dId":10301,"level":2,"dName":"软件-接口应用部","pid":103}},{"id":13,"account":"77@test.com","name":"77","password":"CD97FC8794E0AAB9ADEB0354DC2929257A56A4DA5CA7E0042506E3C1","status":2,"deptId":10303,"grade":3,"department":{"dId":10303,"level":2,"dName":"技术研究部","pid":103}},{"id":12,"account":"66@test.com","name":"66","password":"64D2D97D56AB295C55C6C1B48B826E8551AB5990C83AD8935AD4416F","status":2,"deptId":10401,"grade":3,"department":{"dId":10401,"level":2,"dName":"软件-业务应用部","pid":104}},{"id":11,"account":"55@test.com","name":"55","password":"6D8149E9B7C3BC3590A1D713270D224697A23BB501D4B5087F83D307","status":2,"deptId":10402,"grade":3,"department":{"dId":10402,"level":2,"dName":"软件-应用系统部","pid":104}},{"id":10,"account":"44@test.com","name":"44","password":"EDF05F312C733EC469878AF8DE4E8326D38A50073F48FFC24A8E8501","status":2,"deptId":10403,"grade":3,"department":{"dId":10403,"level":2,"dName":"质量管理部","pid":104}}];
                var userList = renderUserData(user);
                
                var Count = userList.length; //记录条数  
                var PageSize=10; //设置每页示数目  
                var PageCount=Math.ceil(Count/PageSize); //计算总页数  
                var currentPage =1; //当前页,默认为1
                
                if(PageCount > 1){
                    $('.page').show();
                    var pageHtml = '<a href="javascript:;" class="disable prev"><</a> '
                    
                    for(var j=1;j<=PageCount;j++){
                        if(currentPage == j){
                            pageHtml += '<a href="javascript:;" class="current">'+j+'</a>'
                        } else {
                            pageHtml += '<a href="javascript:;">'+j+'</a>'
                        }
                    }
                    pageHtml += '<a href="javascript:;" class="next">></a>';
                    $('.page').empty().append(pageHtml)
                } else {
                    $('.page').hide();
                }
                
                //默认显示第一页
                renderPage($('.table_body'),userList,currentPage,PageSize);
                
                $('.page').on('click','a:not(.next):not(.prev)',function(){
                    currentPage = $(this).text();
                    $(".current").removeClass("current");
                    $(this).addClass("current");
                
                    disabledPageBtn();
                    
                    renderPage($('.table_body'),userList,currentPage,PageSize);
                })
                
                $('.page').on('click','.next:not(.disable)',function(){
                    currentPage += 1;
                    $(".current").removeClass("current").next('a:not(.next)').addClass('current');
                    
                    disabledPageBtn();
                    
                    renderPage($('.table_body'),userList,currentPage,PageSize);
                })
                
                $('.page').on('click','.prev:not(.disable)',function(){
                    currentPage -= 1;
                    $(".current").removeClass("current").prev('a:not(.prev)').addClass('current');
                    
                    disabledPageBtn();
                    
                    renderPage($('.table_body'),userList,currentPage,PageSize);
                })
            })
            // 将json数据拼接成html数组
            function renderUserData(data) {
                var pageData=[]; 
                if (data.length !== 0) {
                    for (let i = 0; i<data.length; i++) {
                        var jobPos = getPos(data[i].grade);
                        var dataHtml = '<ul class="list-content list">'
                                    +'<li class="choose-man">'
                                    +'<input name="select-items" type="checkbox" />'
                                    +'<input name="userId" class="userId" type="hidden" value="'+data[i].id+'"/>'
                                    +'</li>'
                                    +'<li class="man-name">'+data[i].name+'</li>'
                                    +'<li class="man-email email-type">'+data[i].account+'</li>'
                                    +'<li class="man-position">'+jobPos+'</li>'
                                    +'<li class="man-branch">'+data[i].department.dName+'</li>'
                                +'</ul>';
                        pageData.push(dataHtml);
                    }
                } else {
                    var dataHtml = '<div class="full-page">该部门下暂时还没有员工!!!</div>';
                    pageData.push(dataHtml);
                }
                return pageData;
            }
            
            function getPos(grade){
                if(grade === 1) {
                    return "管理员";
                } else if(grade === 2){
                    return "主管";
                } else {
                    return "普通员工";
                }
            }
            // 渲染每页内容
            function renderPage(obj,data,currentPage,PageSize){
                obj.empty();
                for(var i=(currentPage-1)*PageSize;i<PageSize*currentPage;i++){    
                    obj.append(data[i]);  
                } 
            }
            // 判断上页、下页按钮是否可用
            function disabledPageBtn(){
                // 上页
                if($('.current').prev('a').hasClass('prev')){
                    $(".prev").addClass("disable");
                } else {
                    $(".prev").removeClass("disable");
                }
                
                //下页
                if($('.current').next('a').hasClass('next')){
                    $(".next").addClass("disable");
                } else {
                    $(".next").removeClass("disable");
                }
            }
            
        </script>
    </body>
</html>

没投入太多时间去完善这个代码,不过如果仅仅是基本的需求的话(切记数据量不要太大,如果数据量很大我猜是会相当耗资源),以上代码就足够用了。

效果:

page.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,558评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,002评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,024评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,144评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,255评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,295评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,068评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,478评论 1 305
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,789评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,965评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,649评论 4 336
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,267评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,982评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,800评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,847评论 2 351

推荐阅读更多精彩内容