Implementation of json page paging instance code using javascript

Source: Internet
Author: User

In the afternoon, a friend asked me how to paging the json data.

The following code is used:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script>
Var a = {"code": 1, "list": [{"category1_id": "1", "category1_name": "\ u9152 \ u6c34 \ u996e \ u6599 ", "category1_intro": "\ u6e05 \ u51c9 \ u53ef \ u53e3 \ u7684 \ u9152 \ u6c34 \ u996e \ u6599", "category1_images": "http: \ // www.songcuomen.com \/attachments \/productType \/2014-01 \/10 \/21_1389343628.jpg "}, {" category1_id ":" 2 "," category1_name ": "\ u7c73 \ u9762 \ u7cae \ u6cb9", "category1_intro": "\ u5bb6 \ u5ead \ u5fc5 \ u5907", "category1_images": "http: \ // www.songcuomen.com \/attachments \/productType \/2014-01 \/10 \/21_13893420.5.jpg "}, {" category1_id ":" 3 "," category1_name ": "\ u7f8e \ u5bb9 \ u6d17 \ u62a4", "category1_intro": "\ u62a4 \ u80a4 \ u5fc5 \ u5907", "category1_images": "http: \ // www.songcuomen.com \/attachments \/productType \/2014-01 \/10 \/21_1389343935.jpg "}, {" category1_id ":" 4 "," category1_name ": "\ u6e05 \ u6d01 \ u6d17 \ u6da4", "average": "\ Users \ u60a8 \ Users \ u51c0 \ u7684 \ u751f \ u6d3b \ u73af \ u5883 ", "categorycategory images": "http: \ // www.songcuomen.com \/attachments \/productType \/2014-01 \/10 \/21_1389343986.jpg"}, {"category1_id": "5 ", "category1_name": "\ u751f \ u6d3b \ u7528 \ u54c1", "category1_intro": "\ u5bb6 \ u5ead \ u5fc5 \ u5907", "category1_images": "http: \ // www.songcuomen.com \/attachments \/productType \/2014-01 \/10 \/21_1389344445.jpg "}, {" category1_id ":" 6 "," category1_name ": "\ u4f11 \ u95f2 \ u98df \ u54c1", "category1_intro": "\ u597d \ u5403 \ u7f8e \ u5473", "category1_images": "http: \ // www.songcuomen.com \/attachments \/productType \/2014-01 \/10 \/21_1389344494.jpg "}]};
Var y = 1; // indicates the number of pages.
Var t = 2; // indicates the actual number of data entries on each page.
Var z = a ["list"]. length;
Var zy = Math. ceil (z/t );
Window. onload = function (){
Fanye (0 );
}
Function fanye (f)
{
If (y = 1 & f =-1)
{
Alert ('already the first page ');
F = 0;
}
If (y = zy & f = 1)
{
Alert ('the last page ');
F = 0;
}

Var otbod = document. getElementById ('table'). tBodies [0];
Var s = otbod. rows. length;
For (I = 0; I <s; I ++)
{

Otbod. removeChild (otbod. rows [0]);
}
Y + = f;
Var q = (Y-1) * 2; // page number-1 multiplied by the actual number of results per page for this page starting from the number
For (var I = 1; I <3; I ++)
{

Var otr = document. createElement ('tr ');
Var otd1 = document. createElement ('td ');
Var otd2 = document. createElement ('td ');
Var otd3 = document. createElement ('td ');
Var otd4 = document. createElement ('td ');
Otd1.innerHTML = a ["list"] [q] ['category1 _ id'];
Otd2.innerHTML = a ["list"] [q] ['category1 _ name'];
Otd3.innerHTML = a ["list"] [q] ['category1 _ intro'];
Otd4.innerHTML = a ["list"] [q] ['category1 _ images'];
Otr. appendChild (otd1 );
Otr. appendChild (otd2 );
Otr. appendChild (otd3 );
Otr. appendChild (otd4 );
Otbod. appendChild (otr );
If (q = zy)
{
Break;
}
Q ++;
}
}
</Script>

</Head>

<Body>

<Table border = "1" id = "table">
<Thead>
<Tr> <td> category1_id </td> <td> category1_name </td> <td> category1_intro </td> <td> category1_images </td> </tr>
</Thead>
<Tbody>

</Tbody>
</Table>
<A href = "javascript: fanye (1);"> next page </a>
<A href = "javascript: fanye (-1);"> previous page </a>
</Body>
</Html>

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.