Paging table data written in pure js is a json string
This article mainly introduces pages written in pure js. The table data is a json string. For more information, see
If you don't want to talk about anything, go directly to the Code:
The Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Base href = "<% = basePath %>">
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<Title> paging </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script type = "text/javascript">
Var tableData = [{"C0": "Linxia _ kangle county", "C1": 190893.39, "C2": 24544.65, "AREA_ID": "930013005 "}, {"C0": "Linxia _ yongjing county", "C1": 368900.35, "C2": 40592.19, "AREA_ID": "930013006" },{ "C0 ": "Lanzhou _ Donggang branch", "C1": 88.48, "C2": 126.4, "AREA_ID": "930013106" },{ "C0": "Linxia _ Linxia county ", "C1": 107337.9, "C2": 20612.1, "AREA_ID": "930013008" },{ "C0": "Linxia _ Guanghe County", "C1": 69738.07, "C2": 34894.44, "AREA_ID": "930013003" },{ "C0": "Linxia _ hezheng county", "C1": 46622.96, "C2": 20954.97, "AREA_ID": "930013002" },{ "C0": "Linxia _ Dongxiang county", "C1": 96021.84, "C2": 16725.63, "AREA_ID ": "930013004" },{ "C0": "Linxia _ Linxia city center", "C1": 1845311.12, "C2": 129478.93, "AREA_ID": "930013001 "}, {"C0": "Tianshui _ Qinzhou district", "C1": 0, "C2": 0, "AREA_ID": "930013801" },{ "C0 ": "linxiazhou_jishishan", "C1": 256181.79, "C2": 15185.98, "AREA_ID": "930013007" },{ "C0": "Jiuquan _ Suzhou ", "C1": 264312, "C2": 402.6, "AREA_ID": "930013701"}];
Var columns = [{"cid": "C0", "ctext": "district/county" },{ "cid": "C1", "ctext ": "Total Customer revenue" },{ "cid": "C2", "ctext": "current month billing fee"}];
/**
Page: page number
PageSize: number of records per page
In addition to page and pageSize, This method also has three parameters:
1. All table data, in json string format, can be obtained through the action query database.
II. key and name of the column corresponding to the header, also in json string format
3. Table id
Note: This is only suitable for scenarios where only one row is in the header and written in advance. You can modify the logic as needed. You are welcome to criticize and correct the logic.
*/
Function splitPage (page, pageSize ){
Var ptable = document. getElementById ("page_table ");
Var num = ptable. rows. length; // table. rows returns all rows contained in the table. In this example, the table consists of one row in the table header and N rows in the table body.
// Alert (num );
// Clear the tbody
For (var I = num-1; I> 0; I --){
Ptable. deleteRow (I );
}
Var totalNums = tableData. length; // The total number of rows.
Var totalPage = Math. ceil (totalNums/pageSize); // total number of pages
Var begin = (page-1) * pageSize; // start position of the page (including)
Var end = page * pageSize; // The end position of the page (not included)
End = end> totalNums? TotalNums: end;
// Write data to the tbody
Var n = 1; // the starting line of the tbody
For (var I = begin; I <end; I ++ ){
Var row = ptable. insertRow (n ++ );
Var rowData = tableData [I];
For (var j = 0; j <columns. length; j ++ ){
Var col = columns [j]. cid;
Var cell = row. insertCell (j );
Var cellData = rowData [col];
Cell. innerHTML = cellData;
}
}
// Generate a paging Toolbar
Var pageBar = "page +" page/total "+ totalPage +" page "+ "";
If (page> 1 ){
PageBar + = "<a href =" javascript: splitPage ("+ 1 +", "+ pageSize +"); "> homepage </a> ";
} Else {
PageBar + = "Homepage ";
}
If (page> 1 ){
PageBar + = "<a href =" javascript: splitPage ("+ (page-1) +", "+ pageSize +"); "> previous page </a> ";
} Else {
PageBar + = "Previous Page ";
}
If (page <totalPage ){
PageBar + = "<a href =" javascript: splitPage ("+ (page + 1) +", "+ pageSize +"); "> next page </a> ";
} Else {
PageBar + = "next page ";
}
If (page <totalPage ){
PageBar + = "<a href =" javascript: splitPage ("+ (totalPage) +", "+ pageSize +"); "> last page </a> ";
} Else {
PageBar + = "last page ";
}
Document. getElementById ("page_bar"). innerHTML = pageBar;
}
</Script>
</Head>
<Body onload = "splitPage (1, 3);">
<Table id = "page_table">
<Thead>
<Tr>
<Th> h1 </th>
<Th> h2 </th>
<Th> h3 </th>
</Tr>
</Thead>
<Tbody>
<Tr>
<Td> 111 </td>
<Td> 222 </td>
<Td> 333 </td>
</Tr>
</Tbody>
</Table>
<Div id = "page_bar"> </div>
</Body>
</Html>