为了账号安全,请及时绑定邮箱和手机立即绑定

你们怎样实现前端分页的?

你们怎样实现前端分页的?

幕布斯7119047 2018-11-14 14:21:55
后端的nodejs我能够实现分页但是前端的代码如何写呀?
查看完整描述

1 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

估计题主需要的是前端分页脚本吧:

var getPageList = function(options){

    if(typeof options != "object" || !('pageId' in options) || !('pageRecord' in options)){

        throw Error("options Error! eg:{pageId:1, pageRecord:12, pageUrlTemplate : '/page/{PAGE}/'}");

    };

    options.pageId = parseInt(options.pageId);

    options.pageRecord = parseInt(options.pageRecord);

    options.pageSize = options.pageSize || 10;

    

    options.pageUrlTemplate = options.pageUrlTemplate || "?page={PAGE}";

    options.pageCount = parseInt(( options.pageRecord - 1 ) / options.pageSize ) + 1;

    

    var page = [];

    var firstPage = parseInt(( options.pageId - 1 ) / options.pageSize ) * options.pageSize + 1;

    

    options.getLink = options.getLink || function(pageId){

        return options.pageUrlTemplate.replace("{PAGE}", pageId);

    };

    

    page.push({

        name    : '首页',

        style   : options.pageId == 1 ? "disabled" : "",

        link    : options.getLink(1)

    });

    page.push({

        name    : '上一页',

        style   : options.pageId == 1 ? "disabled" : "",

        link    : options.getLink(options.pageId - 1)

    });

    for( var pageId = firstPage; pageId < firstPage + 10; pageId ++){

        if( pageId >= 1 && pageId <= options.pageCount ){

            page.push({

                name    : pageId,

                link    : options.getLink(pageId),

                style   : pageId == options.pageId ? "active" : ""

            });

        }

    }

    page.push({

        name    : '下一页',

        style   : options.pageId == options.pageCount ? "disabled" : "",

        link    : options.getLink(options.pageId + 1)

    });

    page.push({

        name    : '尾页',

        style   : options.pageId == options.pageCount ? "disabled" : "",

        link    : options.getLink(options.pageCount)

    });

    page.toString = function(){

        return page.map(function(item){

            return '<a href="' + item.link + '" class="' + item.style + '">' + item.name + '</a>';

        }).join("");

    };

    return page;

};


getPageList({pageId:1,pageRecord:1200});

/*

    [

        {"name":"首页","style":"disabled","link":"?page=1"},

        {"name":"上一页","style":"disabled","link":"?page=0"},

        {"name":1,"link":"?page=1","style":"active"},

        {"name":2,"link":"?page=2","style":""},

        {"name":3,"link":"?page=3","style":""},

        {"name":4,"link":"?page=4","style":""},

        {"name":5,"link":"?page=5","style":""},

        {"name":6,"link":"?page=6","style":""},

        {"name":7,"link":"?page=7","style":""},

        {"name":8,"link":"?page=8","style":""},

        {"name":9,"link":"?page=9","style":""},

        {"name":10,"link":"?page=10","style":""},

        {"name":"下一页","style":"","link":"?page=2"},

        {"name":"尾页","style":"","link":"?page=120"}

    ]

*/

'' + getPageList({pageId:1,pageRecord:1200,pageUrlTemplate:'/category/{PAGE}/view'});

/*

    <a href="/category/1/view" class="disabled">首页</a>

    <a href="/category/0/view" class="disabled">上一页</a>

    <a href="/category/1/view" class="active">1</a>

    <a href="/category/2/view" class="">2</a>

    <a href="/category/3/view" class="">3</a>

    <a href="/category/4/view" class="">4</a>

    <a href="/category/5/view" class="">5</a>

    <a href="/category/6/view" class="">6</a>

    <a href="/category/7/view" class="">7</a>

    <a href="/category/8/view" class="">8</a>

    <a href="/category/9/view" class="">9</a>

    <a href="/category/10/view" class="">10</a>

    <a href="/category/2/view" class="">下一页</a>

    <a href="/category/120/view" class="">尾页</a>

*/

如果提主恰巧使用的是 AngularJS,可以下载直接使用我的开源小项目:ng-pagination

查看完整回答
反对 回复 2018-12-15
  • 1 回答
  • 0 关注
  • 777 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信