花了2天时间对iscroll进行了一些封装,方便进行ajax读取数据和显示

1、IScroll直接使用的话还是挺麻烦的,特别是牵涉到分页加载动态加载数据时,以下是核心实现代码。

2、Loading提示,和空数据提示样式,由于篇幅限制不再粘贴,大家可以自己完善。

3、版本是IScroll5

var UseIScrollHelper = {
myScroll: null, //iScroll对象
scrollId: 'divscroll',//默认scrollid
wrapperId: 'wrapper',//默认wrapperid
fillList: null, //对应的回调函数
isMore: false, //是否可刷新标记
isNoData: false, //是否无数据
isLoading: false,//是否加载数据中
isUsePage: true, //是否使用分页
headAndBottomHeight:0, //顶部预留高度
pageIndex: 1,
pageSize: 10,
url: '',
datas: '',
//显示空文本提示
checkIsScrollEmptyAndTiShi: function () {
if ($("#" + this.scrollId).html().trim() === "") {
this.isNoData = true;
this.showEmptyTiShi("#" + this.scrollId, "暂无数据");
$("#" + this.scrollId).css("min-height", "0px");
$("#" + this.scrollId).height("");
}
},
//空列表时显示提示信息
showEmptyTiShi: function (target, description) {
try {
var tishi = "<div>无数据</div>";
$(target).html(tishi);
}
catch (e) { alert(e); }
},
//设置ScrollHeight
setScrollHeight: function () {
var temp_height = 0;
temp_height = $("#"+this.wrapperId).height();
try {
var showHeight = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - this.headAndBottomHeight;
if (temp_height !== showHeight)
temp_height = showHeight;
}
catch (e) { ; };
$("#" + this.wrapperId).height(temp_height);
if (!this.isNoData)//有数据
{
$("#" + this.scrollId).css("min-height", temp_height + 1);
$("#" + this.scrollId).height("");
} else {//无数据
$("#" + this.scrollId).css("min-height", 0);
$("#" + this.scrollId).height("");
}
if (this.myScroll === undefined || this.myScroll === null) this.loadSroll();
else this.myScroll.refresh();
},
//清空数据
clearData: function () {
$("#" + this.scrollId).html("");
$("#" + this.scrollId).css("min-height", 0);
$("#" + this.scrollId).height("");
if (this.myScroll === undefined || this.myScroll === null) this.loadSroll();
else this.myScroll.refresh();
},
//加载Iscroll
loadSroll: function () {
setTimeout(function (obj) {
obj.myScroll = new IScroll("#" + obj.wrapperId, { click: true });
obj.myScroll.on('scrollStart', function () {
document.activeElement.blur();
});
obj.myScroll.on('scrollEnd', function () { if (obj.isMore === false) {
obj.setScrollHeight();
return;
}
if (this.y <= this.maxScrollY) {
if (obj.isMore === false) {
obj.setScrollHeight();
this.refresh();
return;
}
if (obj.getData !== null) {
obj.getData();
}
this.refresh();
} else {
this.refresh();
}
});
}, 100,this);
},
//从服务端读数据
getData: function () {
try {
if (this.isLoading) return;
if (this.isNoData) return;
this.isLoading = true;
var obj = this;
var url = this.url;
//有分页标志才启用分页参数
if (this.isUsePage) {
url = url + "&pageIndex=" + this.pageIndex + "&pageSize=" + this.pageSize;
}
$.ajax({
url: url,
type: "post",
dataType: "json",
data: this.datas,
success: function (p_datas) {
bhttooler.nologinDeal(p_datas);
if (p_datas[0].result === "success") {
if (obj.fillList !== null)
obj.fillList(p_datas[0].datas);
if (obj.isUsePage) {//有分页标志进行判断
if (p_datas[0].isMore === "True") {
obj.pageIndex = obj.pageIndex + 1;
obj.isMore = true;
} else {
obj.isMore = false;
}
}
}
obj.checkIsScrollEmptyAndTiShi();
bhttooler.hideLoadding();
obj.setScrollHeight();
obj.isLoading = false;
},
fail: function () {
obj.checkIsScrollEmptyAndTiShi();
obj.setScrollHeight();
obj.isLoading = false;
}
});
}
catch (e) {
this.checkIsScrollEmptyAndTiShi();
this.setScrollHeight();
this.isLoading = false;
} } };

前台示例使用方法:

<div id="celltemplate" style="display:none">
<div class="weui-cells">
<a class="weui-cell weui-cell_access" href="#">
<div class="weui-cell__hd ">
#title#
</div>
</a>
</div>
</div>
<div class="page tabbar js_show">
<div class="page__bd" style="height: 100%;">
<div class="weui-tab">
<div class="weui-tab__panel" style="padding-bottom: 0px;">
<div id="wrapper" >
<div id="divscroll">
</div>
</div>
</div>
</div>
</div>
</div>
 window.onload = function () {
mobanhtml = $('#celltemplate').html();
UseIScrollHelper.fillList = FillList;
UseIScrollHelper.url = "XXXXXXXXXXXXXXXX";
UseIScrollHelper.datas = "";
UseIScrollHelper.getData();
}
function FillList(listdata) { for (var i = 0; i < listdata.length; i++) {
var datas = listdata[i];
var inserthtml = mobanhtml
.replace("#title#", datas.title)
$('#divscroll').append(inserthtml);
}
}
#wrapper {
overflow: hidden;
position:relative;
}
/*必须设置为absolute不然会遮挡一部分*/
#divscroll {
position: absolute;
width: 100%; }

如果一个页面有多个IScroll,请使用以下代码生成实例

var renYuanScroller = Object.create(UseIScrollHelper);

后台返回数据代码(C#示例)。

 string strJson = "\"datas\":" + Newtonsoft.Json.JsonConvert.SerializeObject(dtTemp, new Newtonsoft.Json.Converters.DataTableConverter());
string Json = "[{\"result\":\"success\",\"isMore\":\"" + isMore + "\"," + strJson + "}]";
Response.Write(Json);

一个Ajax读数据并使用IScroll显示辅助类的更多相关文章

  1. 为大家分享一个 Ajax Loading —— spin.js

    我们在做Ajax 异步请求的时候,一般都会利用一个动态的 Gif 小图片来制作一个Ajax Loading ,以便增加用户体验. 今天在网上发现了一个 Spin.js ,该 js 脚本压缩后5k,可以 ...

  2. 写一个ajax程序就是如此简单

    写一个ajax程序就是如此简单 ajax介绍: 1:AJAX全称为Asynchronous JavaScript and XML(异步JavaScript和XML),指一种创建交互式网页应用的网页开发 ...

  3. 为大家分享一个 Ajax Loading —— spin.js(转)

    原文地址:http://www.cnblogs.com/lxblog/p/3425599.html 我们在做Ajax 异步请求的时候,一般都会利用一个动态的 Gif 小图片来制作一个Ajax Load ...

  4. Ajax上传文件进度条显示

    要实现进度条的显示,就要知道两个参数,上传的大小和总文件的大小 html5提供了一个上传过程事件,在上传过程中不断触发,然后用已上传的大 小/总大小,计算上传的百分比,然后用这个百分比控制div框的显 ...

  5. 一个ajax的Post要求

    <1> $.post(url,[data],[callback],[type]) 第一个参数是地址,第二个参数是一个参数传递.第三个参数是一个回调函数.參数是请求返回数据的类型 //一个a ...

  6. jquery通过AJAX从后台获取信息并显示在表格上,并支持行选中

    不想用Easyui的样式,但是想要他的表格功能,本来一开始是要到网上找相关插件的,但是没找到就开始自己写,没想到这么简单. 后台代码:(这个不重要) public ActionResult GetDi ...

  7. Javascript:来一个AJAX封装函数

    前不久换工作了,最近一直在出差,忙得跟狗一样,所以博客都荒废许久了. 最近的工作中涉及到大量的ajax操作,本来该后台做的事也要我来做了.而现在使用的ajax函数是一个后台人员封装的—-但他又是基于 ...

  8. asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现

    asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现 用这个可以做模板首页进行输出,也可以自已自定义进行扩展 //得到读取到的文本到string中 string resultTe ...

  9. 利用jquery+iframe做一个ajax上传效果

    以下是自学it网--中级班上课笔记 网址:www.zixue.it html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict ...

随机推荐

  1. SQL优化小技巧

    我们要做到不但会写SQL,还要做到写出性能优良的SQL语句. 1.使用表的别名(Alias): 当在SQL语句中连接多个表时, 请使用表的别名并把别名前缀于每个Column上.这样一来,就可以减少解析 ...

  2. 最好用的jQuery-Ajax缓存插件

    AJAX-Cache    最好用的jQuery-Ajax缓存插件 介绍 AJAX-Cache是一款jQuery插件,基于localStorage/sessionStorage实现异步请求缓存功能,并 ...

  3. 2 OpenWrt路由器系统开发与网页设计

    https://www.zhongkerd.com/news/content-729.html 摘 要: 目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户 ...

  4. An internal error occurred during: Initializing Java Tooling.

    详细错误信息: An internal error occurred during: "Initializing Java Tooling". java.lang.NullPoin ...

  5. 优先队列Priority Queue和堆Heap

    对COMP20003中的Priority queue部分进行总结.图片来自于COMP20003 queue队列,顾名思义特点先进先出 priority queue优先队列,出来的顺序按照优先级prio ...

  6. keras03 Aotuencoder 非监督学习 第一个自编码程序

    # keras# Autoencoder 自编码非监督学习# keras的函数Model结构 (非序列化Sequential)# 训练模型# mnist数据集# 聚类 https://www.bili ...

  7. OO第二单元总结——多线程电梯

    第五次作业分析 1.设计策略 调度器采用单例模式,内部设请求队列,对请求队列的一切操作(查.增.删)都在调度器内完成,且都要求串行,从而确保线程安全.接收器和电梯是两个线程:接收器接受请求调用调度器来 ...

  8. 关于echarts.js 柱形图

    echarts.js官网: http://www.echartsjs.com/index.html 这是我所见整理最详细echarts.js 柱形图博客: https://blog.csdn.net/ ...

  9. linux cp 直接覆盖不提示信息 解决方法

    默认情况下,cp覆盖时,无论加什么参数 -f 之类的 还是提示是否覆盖. 原因是:服务器会默认增加别名 alias cp=’cp -i’,当你执行cp时,其实执行的是cp –i. [root@ltt0 ...

  10. open live writer 测试

    什么是Hash表 1.定义 Hash(散列/哈希),就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是,散 ...