ajax+ Sub-page

Source: Internet
Author: User

<! DOCTYPE html>
<meta charset= "UTF-8";
<meta name= " Viewport "content=" Width=device-width, initial-scale=1, User-scalable=no ";
<meta http-equiv=" X-ua-compatible "content=" Ie=edge, chrome=1 "/>
<meta name=" renderer "content=" WebKit "/>
<title > Test copy </title>
<link href= "Https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel= " Stylesheet,
<link href= "Https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel= " Stylesheet,
<!--pagination-->
<style type= "Text/css";
#pagecount {
Text-align:center;
padding:20px 0;
}
. turn-go{
Text-align:center;
}
</style>
<script src= "Https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js" ></script>
<script src= "Https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" ></SCRIPT>
</ Head>
<body>

<div id= "app",
<table class= "Table Table-border",
<thead>
<tr>
<th> Aid</th>
<th>cid</th>
<th>developer</th>
<th>download</th>
<th>icon</th>
<th>name</th>
<th>orders</th>
<th>price </th>
</tr>
</thead>
<tbody id= "Data" ></TBODY>
</table>
< /div>
<!--page start-->
<div id= "list" >
<ul></ul>
</div>
<div id= " PageCount "></DIV>
<div class=" turn-go ";
<input type=" text "maxlength=" 8 "value=" 1 "id=" Fy_n
<span> page
<input type= "Submit" Name= "Btngo" value= "Go" id= "Go";
</span>
</div
<!--page ends--

<script type= "Text/javascript" >
Load token
Token address is Http://beta.open.api.vrseen.net/token/get
Getting data from an interface
Interface is Http://beta.store.api.vrseen.net/appcategory/list

Get token
var getToken = function () {
Determine if token is present
if (Sessionstorage.getitem ("token") = = null) {
Returns after converting to an object
Return Json.parse (Sessionstorage.getitem ("token"));
}
var token = null;
$.ajax ({
URL: "Http://beta.open.api.vrseen.net/token/get",
Type: "GET",
DataType: "JSON",
Async:false,
Success:function (data) {
Convert to JSON string, write token to local
Sessionstorage.setitem ("token", Json.stringify (Data.data));
token = Data.data;
}
});
return token;
}

Get Data Write Data
$ (Function (page) {
var token = GetToken ();
Get data to write to page
$.ajax ({
URL: "Http://beta.store.api.vrseen.net/app/list",
Type: "POST",
headers:{
"Token-key": Token. Tokenkey,
"Token-value": Token. Tokenvalue
// },
data:{
Page:page
// },
DataType: "JSON",
Success:function (data) {
Console.log (data);
Console.log (Data.data)
if (Data.code = = "SUCCESS") {
var html = "";
Console.log (data.data.lists);
for (var i =0;i < data.data.lists.length;i++) {
var d = data.data.lists[i];
var img = " ';
HTML + = "<tr><td>" + d.aid+ "</td><td>" + d.cid+ "</td><td>" + d.developer+ "</ Td><td> "+ d.download+" </td><td> "+img+" </td><td> "+ d.name+" </td><td> "+ d.orders+" </td><td> "+ d.price+" </td></tr> ";
// }
Console.log (HTML);
$ ("#data"). HTML (HTML);
// }
// }
// })

Page out
var curpage = 1; Current page
GetData ();
Get Data
function GetData (page) {
$.ajax ({
URL: "Http://beta.store.api.vrseen.net/app/list",
Type: "POST",
headers:{
"Token-key": Token. Tokenkey,
"Token-value": Token. Tokenvalue
},
data:{
Page:page
},
DataType: "JSON",
Beforesend:function () {
$ ("#list ul"). Append ("<li id= ' loading ' >loading...</li>");//Display load animation
},
Success:function (data) {
var Zys = Math.ceil (data.data.page.total_pages);//Total Pages 34 pages
var total = Math.ceil (DATA.DATA.PAGE.LAST/7);//34 pages, showing number of bars
var current = number (data.data.page.current); Current page
$ ("#list ul"). empty ();//Empty data area
Curpage = current; Current page
Totalpage = Zys; Show pages
var li = "";
$.each (list,function (data) {//Traverse JSON data column
Li + = "<li><a href= ' # ' >" +total+ "</a></li>";
Console.log (data)
// });
$ ("#list ul"). Append (LI);
Console.log (Data.data.page.total_pages)
Console.log (current)
if (Data.code = = "SUCCESS") {
var html = "";
for (var i =0;i < data.data.lists.length;i++) {
var d = data.data.lists[i];
var img = " ';
HTML + = "<tr><td>" + d.aid+ "</td><td>" + d.cid+ "</td><td>" + d.developer+ "</td ><td> "+ d.download+" </td><td> "+img+" </td><td> "+ d.name+" </td><td> "+ d.orders+ "</td><td>" + d.price+ "</td></tr>";
}
Console.log (HTML);
$ ("#data"). HTML (HTML);
}
},

Complete:function () {//RAW ingredient page strip
Getpagebar ();
NewData ();
Go ();
},
Error:function () {
Alert ("Data load Failed");
}

});
}
Get pagination Bar
function Getpagebar () {
Page number is greater than max pages
if (curpage>totalpage) curpage=totalpage;
Page number less than 1
if (curpage<1) curpage=1;
Show content Current page and total pages
Pagestr = "<span> Current page:</span><span>" +curpage
+ "/" +totalpage+ "</span>";
If this is the first page
if (curpage==1) {
Pagestr + = "<span> home </span><span> prev </span>";
}else{
Pagestr + = "<span><a href= ' javascript:void (0) ' rel= ' 1 ' > Home </a></span><span><a href= ' javascript:void (0) ' rel= ' "+ (curPage-1) +" ' > Prev </a></span> ";
}
If this is the last page
if (curpage>=totalpage) {
Pagestr + = "<span> next </span><span> last </span>";
}else{
Pagestr + = "<span><a href= ' javascript:void (0) ' rel= '" + (parseint (curpage) +1) + "' > Next </a></span ><span><a href= ' javascript:void (0) ' rel= ' "+totalpage+" ' > Last </a></span> ";
}
$ ("#pagecount"). HTML (PAGESTR);
}
Update data
function NewData () {
$ ("#pagecount span a"). On (' click ', function () {
var rel = $ (this). attr ("rel");
if (rel) {
GetData (rel);
$ ("#fy_n"). Val (rel);
}
});
}
Jump function
function Go () {
$ ("#go"). On (' click ', function () {
var value = $ (' #fy_n '). Val ();
Console.log (value);
if (value) {
GetData (value);
}
});
}
})

</script>
</body>

ajax+ Sub-page

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.