Using JS to make HTML table pagination example (JS implementation paging) _javascript skills

Source: Internet
Author: User

Sometimes the table's column number is too long, not conducive to user inquiries, so use JS to do a table page, the following is the relevant code

One, JS code

Copy Code code as follows:

<script type= "Text/javascript" >
var pageSize = 15; The number of record bars displayed per page
var curpage=0; Current page
var lastpage; Last page
var direct=0; Direction
var Len; Total number of rows
var page; Total pages
var begin;
var end;


$ (document). Ready (function display () {
Len =$ ("#mytable tr"). Length-1; To find the total number of rows in this table, remove the first row
Page=len% pagesize==0? Len/pagesize:math.floor (len/pagesize) +1;//count the number of pages according to the record bar
Alert ("page===" +page);
curpage=1; Set Current as first page
Displaypage (1);//Show First page

document.getElementById ("Btn0"). Innerhtml= "Current" + Curpage + "/" + page + "pages per page"; Show how many pages are currently
document.getElementById ("Sjzl"). Innerhtml= "Total data" + len + ""; Display the amount of data
document.getElementById ("PageSize"). Value = PageSize;



$ ("#btn1"). Click (function FirstPage () {//home
curpage=1;
Direct = 0;
Displaypage ();
});
$ ("#btn2"). Click (function FrontPage () {//prev
Direct=-1;
Displaypage ();
});
$ ("#btn3"). Click (function NextPage () {//Next page
Direct=1;
Displaypage ();
});
$ ("#btn4"). Click (function LastPage () {//Last page
Curpage=page;
Direct = 0;
Displaypage ();
});
$ ("#btn5"). Click (function Changepage () {//goto page
Curpage=document.getelementbyid ("Changepage"). Value * 1;
if (!/^[1-9]\d*$/.test (curpage)) {
Alert ("Please enter a positive integer");
return;
}
if (Curpage > page) {
Alert ("Beyond data page");
return;
}
Direct = 0;
Displaypage ();
});


$ ("#pageSizeSet"). Click (function SetPageSize () {//Set how many records to display per page
PageSize = document.getElementById ("PageSize"). Value; The number of record bars displayed per page
if (!/^[1-9]\d*$/.test (pageSize)) {
Alert ("Please enter a positive integer");
return;
}
Len =$ ("#mytable tr"). Length-1;
Page=len% pagesize==0? Len/pagesize:math.floor (len/pagesize) +1;//count the number of pages according to the record bar
curpage=1; Current page
direct=0; Direction
FirstPage ();
});
});

function Displaypage () {
if (curpage <=1 && direct==-1) {
direct=0;
Alert ("Already the first page");
Return
else if (curpage >= page && direct==1) {
direct=0;
Alert ("Already the last page");
return;
}

LastPage = Curpage;

Fix a bug that Curpage calculates 0 when Len=1
if (Len > PageSize) {
Curpage = ((curpage + direct + len)% len);
} else {
Curpage = 1;
}


document.getElementById ("Btn0"). Innerhtml= "Current" + Curpage + "/" + page + "pages per page"; Show how many pages are currently

Begin= (curPage-1) *pagesize + 1;//start record number
End = begin + 1*pagesize-1; Record number at the end


if (End > Len) end=len;
$ ("#mytable tr"). Hide (); First, set this behavior to hide
$ ("#mytable tr"). each (function (i) {//Then, determine whether the bank resumes display by conditional judgment
if ((I>=begin && i<=end) | | | i==0)//Show Begin<=x<=end Records
$ (this). Show ();
});

}
</script>

Second, the HTML code

Copy Code code as follows:

<a id= "Btn0" ></a>
<input id= "PageSize" type= "text" size= "1" maxlength= "2" value= "Getdefaultvalue ()"/><a> strip </a> <a href= "#" id= "Pagesizeset" > Settings </a>
<a id= "Sjzl" ></a>
<a href= "#" id= "btn1" > Home </a>
<a href= "#" id= "btn2" > Prev </a>
<a href= "#" id= "Btn3" > next page </a>
<a href= "#" id= "Btn4" > Last </a>
<a> go to </a>
<input id= "Changepage" type= "text" size= "1" maxlength= "4"/>
<a> page </a>
<a href= "#" id= "Btn5" > Jump </a>

<table id= "mytable" align= "Center" >
... The remaining table code

The final example results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.