分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

jQuery+AJAX实现纯js分页功能

发布时间:2023-09-06 01:23责任编辑:顾先生关键词:jsjQuery

使用jQuery的AJAX技术,在bootstrap的框架下搭建的纯js分页

bootstrap作为Twitter推的一款前端框架,效果个人还是觉得很不错的。这次只是拿来作为网页元素的css样式表使用,比较省力,效果也会比自己做要漂亮多了。

使用数据为单独的json文件data.json

[plain] view plain copy
 
[ ?????{ ?????????"name": "bootstrap-table", ?????????"stargazers_count": "526", ?????????"forks_count": "122", ?????????"description": "An extended Bootstrap table with radio, checkbox, sort, pagination, and other added features. (supports twitter bootstrap v2 and v3) " ?????}, ?... ?] ?

  

把主要代码和过程总结一下

html页面index.html如下

<!DOCTYPE html> ?<html> ?<head> ?<title>Index</title> ?<script type="text/javascript" src="js/jquery-1.8.1.min.js"></script> ?<script type="text/javascript" src="js/index.js"></script> ?<link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.css"> ?</head> ???<body> ?????<div> ??????<table id="table" class="table table-bordered table-hover"> ?????????????<thead> ?????????????<tr> ?????????????????<th>ID</th> ?????????????????<th>Item Name</th> ?????????????????<th>Item Price</th> ?????????????<th>Item Operate</th> ?????????????</tr> ??????????????????????</thead> ?????????????<tbody> ???????????????</tbody> ?????????</table> ?????</div> ?????<nav align="center" id="page_nav"> ?????<ul class="pagination" id="page_prev"> ?????????<li id="prev"> ???????????<a href="#" aria-label="Previous"> ?????????????<span aria-hidden="true">?</span> ???????????</a> ?????????</li> ?????</ul> ???????<ul class="pagination" id="page_ul"> ????????????????</ul> ???????<ul class="pagination" id="page_next"> ?????????<li id="next"> ???????????<a href="#" aria-label="Next"> ?????????????<span aria-hidden="true">?</span> ???????????</a> ?????????</li> ???????</ul> ?????</nav> ?</body> ???</html> ?

  

js代码index.js如下

 
var pageTotal=0;//总页数 ?var rowTotal=0;//总行数 ?var currentPage=0;//当前页数 ?var startRow=0;//页开始行数 ?var endRow=0;//页结束行数 ?var pageSize=2;//每页行数 ???function page(){ ?????$.ajax({ ?????????url:"data.json", ?????????type:"POST", ?????????dataType:"json", ?????????timeout:1000, ?????????error:function(){ ?????????????alert("ajax error"); ?????????}, ?????????success:function(data){ ?????????????rowTotal=data.length; ?????????????pageTotal=Math.ceil(rowTotal/pageSize);//上取整 ?????????????currentPage=1; ?

  


 
<span style="white-space:pre"> ???????????</span>//绘制数据table ?????????????if(pageTotal==1){ ?????????????????for(var i=0;i<pageSize;i++){ ?????????????????????$("#table tbody").append( ?????????????????????$("<tr><td>"+ ?????????????????????????data[i].name+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].stargazers_count+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].forks_count+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].description+ ?????????????????????????"</td></tr>") ?????????????????????); ?????????????????} ?????????????}else{ ?????????????????for(var i=0;i<pageSize;i++){ ?????????????????????$("#table tbody").append( ?????????????????????$("<tr><td>"+ ?????????????????????????data[i].name+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].stargazers_count+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].forks_count+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].description+ ?????????????????????????"</td></tr>") ?????????????????????); ?????????????????} ?

  


 
<span style="white-space:pre"> ???????????????</span>//绘制页面ul ?????????????????for(var i=1;i<pageTotal+1;i++){ ?????????????????????$("#page_ul").append( ?????????????????????????$("<li><a href=‘#‘>"+i+"</a><li>") ?????????????????????); ?????????????????} ?????????????} ?????????} ?????}); ?} ?//翻页 ?function gotoPage(pageNum){ ?????$.ajax({ ?????????url:"data.json", ?????????type:"POST", ?????????dataType:"json", ?????????timeout:1000, ?????????error:function(){ ?????????????alert("ajax error"); ?????????}, ?????????success:function(data){ ?????????????currentPage=pageNum; ?????????????startRow=pageSize*(pageNum-1); ?????????????endRow=startRow+pageSize; ?????????????endRow=(rowTotal>endRow)?endRow:rowTotal; ?????????????$("#table tbody").empty(); ?????????????for(var i=startRow;i<endRow;i++){ ?????????????????$("#table tbody").append( ?????????????????????$("<tr><td>"+ ?????????????????????????data[i].name+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].stargazers_count+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].forks_count+ ?????????????????????????"</td><td>"+ ?????????????????????????data[i].description+ ?????????????????????????"</td></tr>") ?????????????????????); ?????????????} ???????????????????????} ?????}); ?} ?????$(function(){ ?????page(); ???????$("#page_ul li").live("click",function(){ ?????????var pageNum=$(this).text(); ?????????gotoPage(pageNum); ?????}); ???????$("#page_prev li").live("click",function(){ ?????????if(currentPage==1){ ???????????}else{ ?????????????gotoPage(--currentPage); ?????????} ?????}); ???????$("#page_next li").live("click",function(){ ?????????if(currentPage==pageTotal){ ???????????}else{ ?????????????gotoPage(++currentPage); ?????????} ?????}) ?}); ?

  

jQuery+AJAX实现纯js分页功能

原文地址:http://www.cnblogs.com/moxiaowohuwei/p/7804544.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved