纯JS实现表格分页

思路:

1.讲表格内容存入一个变量

2.通过点击按钮从表格中调取数据

3.讲调取的数据填入表格中


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>表格分页</title>

<style>

html,body {

margin:0;

height:100%;

}

.barcon {

width:auto;

margin:0 auto;

text-align:center;

}

.barcon1 {

font-size:17px;

float:left;

margin-top:20px;

}

.barcon2 {

float:right;

}

.barcon2 ul {

margin:20px 0;

padding-left:0;

list-style:none;

text-align:center;

}

.barcon2 li {

display:inline;

}

.barcon2 a {

font-size:16px;

font-weight:normal;

display:inline-block;

padding:5px;

padding-top:0;

color:black;

border:1px solid #ddd;

background-color:#fff;

}

.barcon2 a:hover {

background-color:#eee;

}

.ban {

opacity:.4;

}

#shade {

background:#000;

filter:alpha(opacity=50);/* IE的透明度*/

            opacity:0.5;/* 透明度*/

            display:none;

position:absolute;

top:0px;

left:0px;

width:100%;

height:100%;

z-index:9999;/* 此处的图层要大于页面*/

            display:none;

}

</style>

</head>

<body>

<table >

<thead>

<tr>

<th>ID</th>

<th>菜单</th>

<th>链接地址</th>

<th>父菜单</th>

</tr>

</thead>

<tbody id="list"></tbody>

</table>

<div id="barcon" class="barcon">

<div id="barcon1" class="barcon1"></div>

<div id="barcon2" class="barcon2">

<ul>

<li><a href="###" id="firstPage">首页</a></li>

<li><a href="###" id="prePage">上一页</a></li>

<li><a href="###" id="nextPage">下一页</a></li>

<li><a href="###" id="lastPage">尾页</a></li>

<li><select id="jumpWhere">

</select></li>

<li><a href="###" id="jumpPage" onclick="jumpPage()">跳转</a></li>

</ul>

</div>

</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"

            integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>

<script>

var table;

table = [

{

"id":"21",

"name":"演唱会",

"parent_id":"15",

"url":"http://www.acfun.cn/v/list99/index.htm/v/list140/index.htm"

        },

{

"id":"22",

"name":"舞蹈·彼女",

"parent_id":"0",

"url":"http://www.acfun.cn/v/list123/index.htm"

        },

{

"id":"23",

"name":"宅舞",

"parent_id":"22",

"url":"http://www.acfun.cn/v/list134/index.htm"

        },

{

"id":"24",

"name":"综合舞蹈",

"parent_id":"22",

"url":"http://www.acfun.cn/v/list135/index.htm"

        },

{

"id":"25",

"name":"爱豆",

"parent_id":"22",

"url":"http://www.acfun.cn/v/list129/index.htm"

        },

{

"id":"26",

"name":"手作",

"parent_id":"22",

"url":"http://www.acfun.cn/v/list130/index.htm"

        },

{

"id":"27",

"name":"造型",

"parent_id":"22",

"url":"http://www.acfun.cn/v/list127/index.htm"

        },

{

"id":"28",

"name":"游戏",

"parent_id":"0",

"url":"http://www.acfun.cn/v/list59/index.htm"

        },

{

"id":"29",

"name":"主机单机",

"parent_id":"28",

"url":"http://www.acfun.cn/v/list84/index.htm"

        },

{

"id":"30",

"name":"游戏集锦",

"parent_id":"28",

"url":"http://www.acfun.cn/v/list83/index.htm"

        },

{

"id":"31",

"name":"电子竞技",

"parent_id":"28",

"url":"http://www.acfun.cn/v/list145/index.htm"

        },

{

"id":"32",

"name":"英雄联盟",

"parent_id":"28",

"url":"http://www.acfun.cn/v/list85/index.htm"

        },

{

"id":"33",

"name":"守望先锋",

"parent_id":"28",

"url":"http://www.acfun.cn/v/list170/index.htm"

        },

{

"id":"34",

"name":"桌游卡牌",

"parent_id":"28",

"url":"http://www.acfun.cn/v/list165/index.htm/"

        },

{

"id":"35",

"name":"Mugen",

"parent_id":"28",

"url":"http://www.acfun.cn/v/list72/index.htm/"

        },

{

"id":"36",

"name":"游戏中心",

"parent_id":"28",

"url":"http://www.acfun.cn/gamecenter"

        },

{

"id":"37",

"name":"娱乐",

"parent_id":"0",

"url":"http://www.acfun.cn/v/list59/index.htm"

        },

{

"id":"38",

"name":"生活娱乐",

"parent_id":"37",

"url":"http://www.acfun.cn/v/list84/index.htm"

        },

{

"id":"39",

"name":"鬼畜调教",

"parent_id":"37",

"url":"http://www.acfun.cn/v/list83/index.htm"

        },

{

"id":"40",

"name":"萌宠",

"parent_id":"37",

"url":"http://www.acfun.cn/v/list145/index.htm"

        },

{

"id":"41",

"name":"美食",

"parent_id":"37",

"url":"http://www.acfun.cn/v/list85/index.htm"

        },

{

"id":"42",

"name":"科技",

"parent_id":"0",

"url":"http://www.acfun.cn/v/list59/index.htm"

        },

{

"id":"43",

"name":"科学技术",

"parent_id":"42",

"url":"http://www.acfun.cn/v/list84/index.htm"

        },

{

"id":"44",

"name":"教程",

"parent_id":"42",

"url":"http://www.acfun.cn/v/list83/index.htm"

        },

{

"id":"45",

"name":"数码",

"parent_id":"42",

"url":"http://www.acfun.cn/v/list145/index.htm"

        },

{

"id":"46",

"name":"广告",

"parent_id":"42",

"url":"http://www.acfun.cn/v/list85/index.htm"

        },

{

"id":"47",

"name":"影视",

"parent_id":"0",

"url":"http://www.acfun.cn/v/list59/index.htm"

        },

{

"id":"48",

"name":"国产剧",

"parent_id":"47",

"url":"http://www.acfun.cn/v/list84/index.htm"

        },

{

"id":"49",

"name":"网络剧",

"parent_id":"47",

"url":"http://www.acfun.cn/v/list83/index.htm"

        },

{

"id":"50",

"name":"纪录片",

"parent_id":"47",

"url":"http://www.acfun.cn/v/list145/index.htm"

        },

{

"id":"51",

"name":"综艺",

"parent_id":"47",

"url":"http://www.acfun.cn/v/list85/index.htm"

        },

{

"id":"52",

"name":"文章",

"parent_id":"0",

"url":"http://www.acfun.cn/v/list59/index.htm"

        },

{

"id":"53",

"name":"游记·涂鸦",

"parent_id":"52",

"url":"http://www.acfun.cn/v/list84/index.htm"

        },

{

"id":"54",

"name":"综合",

"parent_id":"52",

"url":"http://www.acfun.cn/v/list83/index.htm"

        },

{

"id":"55",

"name":"工作·情感",

"parent_id":"52",

"url":"http://www.acfun.cn/v/list145/index.htm"

        },

{

"id":"56",

"name":"动漫文化",

"parent_id":"52",

"url":"http://www.acfun.cn/v/list85/index.htm"

        },

{

"id":"57",

"name":"漫画·文学",

"parent_id":"52",

"url":"http://www.acfun.cn/v/list170/index.htm"

        }

]

starpag();

function starpag() {

goPage(1,20);

var tempOption ="";

for (var i =1;i <=totalPage;i++) {

tempOption +='<option value=' +i +'>' +i +''

        }

$("#jumpWhere").html(tempOption);

};

// 分页函数

    // pno--页数

    // psize--每页显示记录数

    // 分页部分是从真实数据行开始,因而存在加减某个常数,以确定真正的记录数

    // 纯js分页实质是数据行全部加载,通过是否显示属性完成分页功能

    var pageSize =0;//每页显示行数

    var currentPage_ =1;//当前页全局变量,用于跳转时判断是否在相同页,在就不跳,否则跳转。

    var totalPage;//总页数

    function goPage(pno, psize) {

var num =table.length;//表格所有行数(所有记录数)

        pageSize = psize;//每页显示行数

        //总共分几页

        if (num /pageSize >parseInt(num /pageSize)) {

totalPage =parseInt(num /pageSize) +1;

}else {

totalPage =parseInt(num /pageSize);

}

var currentPage = pno;//当前页数

        currentPage_ =currentPage;

var startRow = (currentPage -1) *pageSize +1;

var endRow =currentPage *pageSize;

endRow = (endRow >num) ?num :endRow;

// console.log(endRow);

        var tablepag =new Array();

for (let index =startRow;index <=endRow;index++) {

tablepag.push(table[index -1])

}

tabletr(tablepag);

var tempStr ="共" +num +"条记录 共分" +totalPage +"页 当前第" +currentPage +"页";

document.getElementById("barcon1").innerHTML =tempStr;

if (currentPage >1) {

$("#firstPage").on("click",function () {

goPage(1, psize);

}).removeClass("ban");

$("#prePage").on("click",function () {

goPage(currentPage -1, psize);

}).removeClass("ban");

}else {

$("#firstPage").off("click").addClass("ban");

$("#prePage").off("click").addClass("ban");

}

if (currentPage

$("#nextPage").on("click",function () {

goPage(currentPage +1, psize);

}).removeClass("ban");

$("#lastPage").on("click",function () {

goPage(totalPage, psize);

}).removeClass("ban");

}else {

$("#nextPage").off("click").addClass("ban");

$("#lastPage").off("click").addClass("ban");

}

$("#jumpWhere").val(currentPage);

}

function jumpPage() {

var num =parseInt($("#jumpWhere").val());

if (num !=currentPage_) {

goPage(num,pageSize);

}

}

function tabletr(arr) {

var htm ='';

for (let index =0;index < arr.length;index++) {

htm +='<tr>' +

'<td>' + arr[index].id +'</td>' +

'<td>' + arr[index].name +'</td>' +

'<td>' + arr[index].url +'</td>' +

'<td>' + arr[index].parent_id +'</td>' +

'<td>' +

'</tr>';

}

$("#list").html(htm);

}

</script>

</body>

</html>

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

推荐阅读更多精彩内容

  • 老王最近几天春风得意,每天上班都是哼着小曲儿,办公事的同事老是调侃他是不是彩票中了头奖。 老王每次都说:人生苦短,...
    柚西的小世界阅读 759评论 7 7
  • (稻盛哲学学习会)打卡第139天 姓名:洪波 公司:宁波万尚进出口 部门:分水碶 组别:幸福精进学习营第40期-感...
    洪波66阅读 223评论 0 0
  • 在这样一个物欲横流的时代,一份好的工作就是以工资高低来评价的。大家可能干着自己不喜欢的一份工作,然后拿着高薪,天天...
    暖阳茶阅读 381评论 0 1