基于Jquery的Ajax分页,只有上一页和下一页
最近项目中用到ajax分页
在网上找到一个非常好用的分页插件jquery-pagination-ajax,以下是链接
http://www.cnblogs.com/linzheng/archive/2010/11/07/1871069.html
但是无奈,不太适合项目。看了jquery-pagination,大体明白了其原理,所以,自己写你一个很简单的
分页功能,算是完成任务,如图
个人认为ajax分页和普通的分页,核心原理是大致相同的。
我是用了一个隐藏域来存储当前页面的索引。
贴一下主要的前台代码吧 ,有点乱
<body>
<form id="form1" runat="server">
<input type="hidden" id="hdPageCount_1" value="<%=pageCount %>">" /><!--总页数-->
<input type="hidden" id="hdCurrentPageCount_1" value="1" /><!--当前页数-->
<div id="Main-right-marriage">
<div class="next" style="top: 155px; left: 221px;">
<a href="javascript:void(0)" id="btn_prev"><img src="data:image/prev.png" /></a>
</div>
<div class="photo_wall-top">
<div class="left"> <span class="show_big_img"></span></div>
<div class="right"> </div><!-- to right-->
</div><!-- to photo_wall-top-->
<div class="next" style="top:155px; right: 8px;">
<a href="javascript:void(0)" id="btn_next"><img src="data:image/next.png" /></a>
</div>
</div>
</form>
</body>
JS部分的代码
<script type="text/javascript">
$(function () { /*鼠标悬浮效果*/
$(".box a img").mouseover(function () {
var $div = $(this).parent().parent().parent().parent().find(".left");
var $bigImg = $div.find(".bigImg"); $bigImg.attr("src", $(this).attr("src"));
});
//ajax 无效果 bug修改
$(".box a img").live("mouseover", function () { var $div = $(this).parent().parent().parent().parent().find(".left");
var $bigImg = $div.find(".bigImg"); $bigImg.attr("src", $(this).attr("src"));
}); ajax.ProductList(("#btn_next"), 1, 4); //第一次请求初始化 // 分頁效果
var pageSize = 4; //每页显示条数初始化,修改显示条数,修改这里即可
var pageCount_QH = parseInt($("#hdPageCount_1").val()); $("#btn_next").click(function () {
var currentPageIndex = parseInt($("#hdCurrentPageCount_1").val());
if (currentPageIndex * pageSize > pageCount_QH) {
return false;
} else {
$("#hdCurrentPageCount_1").val(currentPageIndex + 1);
ajax.ProductList(this, $("#hdCurrentPageCount_1").val(), pageSize, 1);
}
});
$("#btn_prev").click(function () {
var currentPageIndex = $("#hdCurrentPageCount_1").val(); if (currentPageIndex > 1) {
ajax.ProductList(this, currentPageIndex - 1, pageSize, 1);
$("#hdCurrentPageCount_1").val(currentPageIndex - 1);
}
else {
return false;
}
});
});
</script>
Ashx后台代码:
/// <summary>
/// 获取产品
/// </summary>
public void GetProduct()
{
HttpContext context = HttpContext.Current;
int pageIndex = Convert.ToInt32(context.Request.Form["_pageIndex"]);//页面索引
int pageSize = Convert.ToInt32(context.Request.Form["_pageSize"]);//显示条数
int count = ;
List<PagerTestModels.Product> list = new PagerTestBLL.ProductManager().GetAllProduct(pageSize, pageIndex, "", out count); if (list.Count == )
return; StringBuilder sbFirst = new StringBuilder();//获取第一个大图
StringBuilder sbAll = new StringBuilder();//获取是个小图 foreach (var item in list)
{
if (sbFirst.Length == )
{
sbFirst.AppendFormat("<a href=\"#\" class=\"bigImg_link\" ><img class=\"bigImg\" src=\"{0}\" height=\"260\" width=\"260\" /><br/><span class=\"title bigImg_title\"></span></a>", item.Thumb);
}
sbAll.AppendFormat("<div class=\"box\"><a href=\"{2}\" ref=\"#\"><img src=\"{0}\" height=\"124\" width=\"124\" alt=\"{1}\" /></a></div>", item.Thumb, item.Title, item.PID);
}
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("oneproduct", sbFirst.ToString());
dic.Add("allproduct", sbAll.ToString()); string str = GetJson(dic); HttpContext.Current.Response.ContentType = "plain/text";
HttpContext.Current.Response.Write(str);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End(); } /// <summary>
/// 字典转化为Json
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
private string GetJson(Dictionary<string, string> dic)
{
StringBuilder sb = new StringBuilder();
sb.Append("{"); foreach (KeyValuePair<string, string> item in dic)
{
//反義引號 sb.AppendFormat("\"{0}\":\"{1}\",", item.Key, item.Value.Replace("\"", "\\\""));
} //刪除最尾一個逗號 string valRtn = Regex.Replace(sb.ToString(), @",$", "}");//替换最后的,号为}号
return valRtn;
}
基于Jquery的Ajax分页,只有上一页和下一页的更多相关文章
- 源码来袭!!!基于jquery的ajax分页插件(demo+源码)
前几天打开自己的博客园主页,无意间发现自己的园龄竟然有4年之久了.可是看自己的博客列表却是空空如也,其实之前也有写过,但是一直没发布(然而好像并没有什么卵用).刚开始学习编程时就接触到博客园,且在博客 ...
- 基于jQuery的ajax对WebApi和OData的封装
基于jQuery的ajax对WebApi和OData的封装 WebApi 的使用带来了一个显著的特点,对type有一定的要求.一般ajax的type无非就是两种,GET和POST.如果用JSONP来跨 ...
- jQuery Pagination Ajax分页插件中文详解(摘)
jQuery Pagination Ajax分页插件中文详解 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxin ...
- 基于jQuery封装的分页组件
前言: 由于项目需要实现分页效果,上jQuery插件库找了下,但是木有找到自己想要的效果,于是自己封装了个分页组件. 思路: 主要是初始化时基于原型建立的分页模板然后绑定动态事件并实现刷新DOM的分页 ...
- 浅谈jQuery Pagination Ajax 分页插件的使用
插件介绍 此插件是jQuery的ajax分页插件.分页切换时无刷新也无延迟,因为是一次性加载的.如果你用到此插件作分页的时候,涉及到的数据量大,建议不要使用此插件,因为加载慢会导致用户体验不好! 插件 ...
- 基于 Django的Ajax实现 文件上传
---------------------------------------------------------------遇到困难的时候,勇敢一点,找同学朋友帮忙,找导师求助. Ajax Ajax ...
- jQuery、Ajax分页
1.效果预览 2.HTML代码 <div class="row"> <div class="col-lg-12 col-sm-12 col-xs-12 ...
- Python Django 之 基于JQUERY的AJAX 登录页面
一.基于Jquery的Ajax的实现 1.url 2.vews 3.templates
- 分页-jquery.page.js插件在使用时重复触发“上一页”和“下一页”操作
HTML代码 <!-- <div class="zxf_pagediv" id="Pagination" style="display:b ...
随机推荐
- gitlab 安装报错:Could not find modernizr-2.6.2 in any of the sources
2014-04-30 15:27:44 标签:gitlab 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://davidbj.b ...
- poj--1517
n从0取到9,一个for循环,n++,n=0,n<=9 n=4,求1/4!+1/3!+1/2!+1/1!+1/0! 需要一个for循环,i=n,i>=0,i-- sum+=factoria ...
- ios的标志常量
1 dec 2 fixed 3 hex 4 internal 5 left 6 oct 7 right 8 scientific 9 showbase 10 showpoint 11 showpos ...
- Minimum Transport Cost(floyd+二维数组记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- Mongo散记--聚合(aggregation)& 查询(Query)
mongo官网:http://www.mongodb.org/ 工作中使用到Mongo,可是没有系统的学习研究过Mongo,仅对工作过程中,在Mongo的使用过程中的一些知识点做一下记录,并随时补充, ...
- jquery设置文本框值 与获取文本框的值
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- javascript高级知识点——函数原型
代码信息来自于http://ejohn.org/apps/learn/. 向函数的原型中添加方法 function Ninja(){} Ninja.prototype.swingSword = fun ...
- 在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)
在Linux下如何用Shell脚本读写XML?现有一个config.xml <?xml version="1.0" encoding="UTF-8"?&g ...
- Qt 控件
一.布局管理器 QHBoxLayout 水平布局 QVBoxLayout 垂直布局 QGridLayout 格点布局 QFormLayout 关联布局 QSplitter 分裂器 Spacers 间隔 ...
- [C++空间分配]new运算符、operator new、placement new的区别于联系
先科普一下: 1. new的执行过程: (1)通过operator new申请内存 (2)使用placement new调用构造函数(内置类型忽略此步) (3)返回内存指针 2. new和malloc ...