利用 Linq+Jquery+Ajax 实现异步分页功能可简化带宽压力
来源: 阅读:1247 次 日期:2014-08-05 17:08:43
温馨提示: 小编为您整理了“利用 Linq+Jquery+Ajax 实现异步分页功能可简化带宽压力”,方便广大网友查阅!

在Web显示的时候我们经常会遇到分页显示,而网上的分页方法甚多,但都太过于消耗带宽,所以我想到了用Ajax来分页,利用返回的Json来处理返回的数据,大大简化了带宽的压力。先说下思路,无非就是异步执行ajax 把新列表所需要的数据用json格式返回来,输出table,你可以输出ui li(输出效率高) 在页面上。

效果图:

图片一

Html代码:

代码如下:

设置它们的Class = "page" 以便于给它们增加Click事件操作分页

用下面的div输出返回的结果

Css代码:

代码如下:

/*分页*/

.pages {

cursor: pointer;

text-align: center;

margin: 0 auto;

padding-right: 0px;

padding-bottom: 2px;

padding-top: 2px;

font-family: verdana, helvetica, arial, sans-serif;

}

.pages a {

border-right: 1px solid;

padding-right: 6px;

border-top: 1px solid;

padding-left: 6px;

padding-bottom: 0px;

overflow: hidden;

border-left: 1px solid;

line-height: 20px;

margin-right: 2px;

padding-top: 0px;

border-bottom: 1px solid;

height: 30px;

}

.pages a {

border-left-color: #e6e7e1;

border-bottom-color: #e6e7e1;

color: #09c;

border-top-color: #e6e7e1;

background-color: #fff;

border-right-color: #e6e7e1;

}

.pages a:hover {

text-decoration: none;

border-left-color: #09c;

border-bottom-color: #09c;

border-top-color: #09c;

border-right-color: #09c;

}

.pages a.next {

border-left-color: #09c;

border-bottom-color: #09c;

border-top-color: #09c;

border-right-color: #09c;

}

JS代码:

引入: //可以为其他版本

代码如下:

$(document).ready(function ()

{

//检索条件

var search = $("#txtFactroy").val() + "_" + $("#txtTimeSelect").val()

+ "_" + $("#txtPinfan").val() + "_" +

$('input[type="checkbox"][name="option1"]:checked').val();

$.ajax({

type: "post",//回传格式

url: "ResponseHandler.ashx"//回传到一般处理程序中处理//回传参数表示请求的是第几页,encodeURIComponent 格式化中文以防乱码

data: "BadProductWhere=" + encodeURIComponent(search) + "&currPage=1",

datatype: "json",//把返回来的数据 json

async: false,//禁止使用浏览器缓存

success: function (returnData, textstatus, xmlhttprequest)

{

$("#showPage").css('display', 'block');//显示分页

$("#divBadProductInfo").html(returnData.split('_')[0]);//返回值分割显示

var page = returnData.split('_')[1].split(',');

$("#SumCount").text(page[0]);//共多少条数据

$("#ItemCount").text(page[1]);//每页多少条数据

$("#Index").text(page[2]);//当前页

$("#PageCount").text(page[3]);//共多少页 }

});

//清除转向页面

$("#txtGoPage").val("");

//分页操作动作

$(".pages").click(function () {

//总页数大于1的情况下上下首末页可用

if (parseFloat($("#PageCount").html()) > 1) {

//取得控件类型是ID还是class

var type = $(this).attr("id");

//取得当前是多少页

var thisindex = $("#Index").text();

var search = $("#txtFactroy").val() + "_" + $("#txtTimeSelect").val()

+ "_" + $("#txtPinfan").val() + "_" +

$('input[type="checkbox"][name="option1"]:checked').val();

switch (type) {

case 'first':

{

$("#txtGoPage").val("");

badpageindex = 1;

BadPageIndex(1, search);//Ajax 回传函数

return;

}

case 'prev':

{

$("#txtGoPage").val("");

badpageindex = parseInt(thisindex) - 1;

if (badpageindex < 1) return;

BadPageIndex(badpageindex, search);

return;

}

case 'next':

{

$("#txtGoPage").val("");

badpageindex = parseInt(thisindex) + 1;

if (badpageindex > parseInt($("#PageCount").html())) return;

else

BadPageIndex(badpageindex, search);

return;

}

case 'last':

{

var max = parseInt($("#PageCount").html());

$("#txtGoPage").val("");

badpageindex = max;

BadPageIndex(max, search);

return;

}

case 'go':

{

var _go = $("#txtGoPage").val();

badpageindex = _go;

BadPageIndex(_go, search);

return;

}

}

}

})

});

代码如下:

var badpageindex;

//index,页面索引例如1,2,3

//BadProductWhere 查询条件

function BadPageIndex(index, searchwhere) {

$.ajax({

type: "post",

url: "ResponseHandler.ashx",

data: "BadProductWhere=" + encodeURIComponent(searchwhere) + "&currPage=" + index,

datatype: "json",

async: false,

success: function (returnData, textstatus, xmlhttprequest) {

$("#divDisplay").css('display', 'none');

$("#showPage").css('display', 'block');

$("#divBadProductInfo").html(returnData.split('_')[0]);

var page = returnData.split('_')[1].split(',');

$("#SumCount").text(page[0]);

$("#ItemCount").text(page[1]);

$("#Index").text(page[2]);

$("#PageCount").text(page[3]);

},

error: function () {

alert("服务错误");

}

});

}

C# 代码:(ResponseHandler.ashx)

代码如下:

///

/// 每页显示条数

///

private int pageSize = 20;

StringBuilder sbBadProductInfo = new StringBuilder();

if (!string.IsNullOrEmpty(context.Request["BadProductWhere"]) &&

!string.IsNullOrEmpty(context.Request["currPage"]))

{

#region // B品标题信息

sbBadProductInfo.Append(@"

B品箱单信息

");

sbBadProductInfo.Append(@"

");

#endregion

List lstGetBadProductData = (from p in lstGetBadProductData

where !string.IsNullOrEmpty(p.出库确认人) && p.出库日期 != null

select p).ToList();

string pageInfo = lstGetBadProductData.Count() + "," + pageSize + "," + context.Request["currPage"] +

"," + (lstGetBadProductData.Count() % 20 == 0 ? (lstGetBadProductData.Count() / 20) :

(lstGetBadProductData.Count() / 20 + 1));

List lstGetBadItemData = (lstGetBadProductData.Count > pageSize ?

lstGetBadProductData.Skip(int.Parse(context.Request["currPage"]) == 1 ? 0 :

pageSize * (int.Parse(context.Request["currPage"]) - 1)).Take(pageSize)

: lstGetBadProductData).ToList();

#region ==>B品箱单信息

foreach (var item in lstGetBadItemData)

{

var cssName = rowCount % 2 == 0 ? "warning" : "error";

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

sbBadProductInfo.Append(@"

");

rowCount++;

}

sbBadProductInfo.Append(@"

箱单编号 装箱生成日期 工厂名称 装箱品番 装箱箱号 装箱件数 仓库确认 出库确认人 出库日期 查看明细
" + item.箱单编号 + " " + item.装箱生成日期 + " " + item.工厂名称 + " " + item.装箱品番 + " " + item.装箱箱号 + " " + item.装箱件数 + " " + item.仓库确认 + " " + item.出库确认人 + " " + item.出库日期 + "

style='width: 80px; height: 30px;' value='查看明细'");

sbBadProductInfo.Append(@" onclick='OpenBadInfo(" + item.箱编号 + ");'/>

");

#endregion

context.Response.Write(sbBadProductInfo.ToString() + "_" + pageInfo);

context.Response.End();

分页效果

图片二

更多信息请查看IT技术专栏

更多信息请查看 网络编程
【点此处就本文及相关问题在本站进行非正式的简要咨询(便捷快速)】 【点此处查询各地各类考试咨询QQ号码及交流群】
上一篇: jquery中的ajax如何返回结果而非回调方式即为同顺序执行
下一篇: ajax的responseText乱码的问题的解决方法
手机网站地址: 利用 Linq+Jquery+Ajax 实现异步分页功能可简化带宽压力
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
相关阅读 网络编程

2026上岸·考公考编培训报班

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
最新信息
公考类
招聘类
各类考试
关于我们| 联系我们| 人才招聘| 网站声明| 网站帮助| 非正式的简要咨询| 简要咨询须知| 新媒体/短视频平台| 手机站点| 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65099533/13759567129 获取招聘考试信息及咨询关注公众号:
咨询QQ:1093837350(9:00—18:00) 版权所有:
云南网警报警专用图标
Baidu
map