JS paging, table, and data generation

Source: Internet
Author: User

Use js to control Paging

Refer to the blog garden style for the following results:

Create a table using JS

 //  JS Table Generation tableCode  
// Arrth header information
// Arrtr data
VaR Gettable = Function (Arrth, arrtr ){
VaR S = '<Table class = "tbdata"> ';
S + = '<tr> ';
For (VaR I = 0; I <Arrth. length; I ++ ){
S + = '<TH>' + Arrth [I] + '</Th> ';
}
S + = '</tr> ';
For ( VaR I = 0; I <arrtr. length; I ++ ){
S + = '<tr> ';
For ( VaR J = 0; j <arrtr [I]. length; j ++ ){
S + = '<TD>' + arrtr [I] [J] + '</TD> ';
}
S + = '</tr> ';
}
S + = '</table> ';
Return S;
}

JS Paging

 //  JS Paging  
// El: Paging container count: Total number of records pagestep: Number of pagenum displayed on each page: page number fngo: Paging jump function
VaR Jspage = Function (EL, Count, pagestep, pagenum, fngo ){
This . Getlink = Function (Fngo, index, pagenum, text ){
VaR S = '<a href = "# P' + index +'" onclick = "'+ fngo +' ('+ index + ');"';
If (Index = pagenum ){
S + = 'class = "acur "';
}
TEXT = text | index;
S + = '>' + TEXT + '</a> ';
Return S;
}

// Total number of pages
VaR Pagenumall = math. Ceil (count/pagestep );
If (Pagenumall = 1 ){
Divpage. innerhtml = '';
Return ;
}
VaR Itemnum = 5; // Number of both sides of the current page
Pagenum = math. Max (pagenum, 1 );
Pagenum = math. Min (pagenum, pagenumall );
VaR S = '';
If (Pagenum> 1 ){
S + = This . Getlink (fngo, pageNum-1, pagenum, 'preview ');
} Else {
S + = '<span> previous page </span> ';
}
VaR Begin = 1;
If (Pagenum-itemnum> 1 ){
S + = This . Getlink (fngo, 1, pagenum) + '...';
Begin = pagenum-itemnum;
}
VaR End = math. Min (pagenumall, begin + itemnum * 2 );
If (END = pagenumall-1 ){
End = pagenumall;
}
For ( VaR I = begin; I <= end; I ++ ){
S + = This . Getlink (fngo, I, pagenum );
}
If (End <pagenumall ){
S + = '...' + This . Getlink (fngo, pagenumall, pagenum );
}
If (Pagenum <pagenumall ){
S + = This . Getlink (fngo, pagenum + 1, pagenum, 'next page ');
} Else {
S + = '<span> next page </span> ';
}
VaR Divpage = Document. getelementbyid (EL );
Divpage. innerhtml = s;
}

JS returns random data

 //  Random JS content  
VaR Jsrand = {};
// Random Number
Jsrand.Int = Function (Min, max ){
Return This . Show ('I', Min, max );
}
// Random character
Jsrand. Str = jsrand. String = Function (Min, max ){
Return This . Show ('', Min, max );
}
// Random date
Jsrand. Date = Function (){
Return This . Show ('D ');
}
// Random amount
Jsrand. Money = Function (Min, max ){
Return This . Show ('M', Min, max );
}
// Random Chinese Characters
Jsrand. CH = Function (Min, max ){
Return This . Show ('ch', Min, max );
}
Jsrand. Show = Function (Type, Min, max ){
VaR STR;
If (Type = 'I '){
STR = This . Rand (Min, max );
} Else If (Type = 'M '){
STR = '$' + This . Rand (Min, max) + '. 00 ';
} Else If (Type = 'D '){
STR = This . Rand (1990,2020) + '-';
STR + = This . Rand (1, 12, 2) + '-';
STR + = This . Rand (1, 31, 2) + '';
STR + = This . Rand (0, 23, 2) + ':';
STR + =This . Rand (1, 59, 2 );
} Else {
Min = min | 0;
Max = max | 10;
STR = '';
VaR Len = This . Rand (Min, max );
For ( VaR I = 0; I <Len; I ++ ){
VaR Ichar = This . Rand (48,122 );
If (Type = 'ch '){
Ichar = This . Rand (19968,408 69 );
}
VaR CHR = string. fromcharcode (ichar );
CHR = CHR. Replace (/&/g, "& amp ;")
. Replace (/</g, "& lt ;")
. Replace (/>/g, "& gt ;")
. Replace (// G, "& nbsp ;")
. Replace (/'/g, "& apos ;")
. Replace (/"/g," & quot ;");
STR + = CHR;
}
}

Return STR;
}

Jsrand. Rand = Function (Min, Max, Len ){
Min = min | 0;
Max = max | 1000;
VaR Val = min + math. Round (math. Random () * (max-min ));
If (LEN ){
While (''+ Val). Length <Len ){
Val = '0' + val;
}
}
Return Val;
}

Usage

 
FunctionGopage (pageindex ){
VaRArrth = ['id', 'name', 'amount ', 'note', 'add time'];
VaRArrtr = [];
For(VaRI = 0; I <20; I ++)
{
Arrtr. Push ([
Jsrand.Int(1, 1000 ),
Jsrand. STR (),
Jsrand. Money (),
Jsrand. Show ('ch', 0, 20 ),
Jsrand. Date ()
]);
}
Document. getelementbyid ('divdata'). innerhtml = gettable (Arrth, arrtr );
Jspage ('divpage', 800, 10, pageindex, 'gopage ');
}
Gopage (1 );

 

Instance download: http://files.cnblogs.com/zjfree/jsPage.rar

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.