花了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. eclipse 开发web 项目,使用gradle 需要安装的插件

    1.Buildship Gradle 扩展 eclipse IDE 以支持使用 Gradle 构建软件.此解决方案由 Eclipse 基金会提供 2.EGradle Editor (主要用来编写gra ...

  2. VS与Windbg调试

    原文 :  https://blog.csdn.net/fin86889003/article/details/20126593 原文 :  https://blog.csdn.net/u014339 ...

  3. DOM中获取宽高、位置总结

    原生JS 一.文档.窗口的宽高和位置 // 获取屏幕的宽高 window.screen.height | window.screen.width // 屏幕可用工作区宽高 window.screen. ...

  4. springMVC 实现redis分布式锁

    1.先配置spring-data-redis 首先是依赖 <dependency> <groupId>org.springframework.data</groupId& ...

  5. On the structure of submodule of finitely generated module over PID

    I was absorbed into this problem for three whole days......

  6. Neutron: Load Balance as a Service(LBaaS)负载均衡

    load balancer 负责监听外部的连接,并将连接分发到 pool member.    LBaaS 有三个主要的概念: Pool Member,Pool 和 Virtual IP Pool M ...

  7. js作用域零碎的知识点,不同的script块,虽然同是全局变量

    如下代码,第一次弹出a,因为解析器里找到var a,赋予a变量undefined,弹出undefined <!DOCTYPE html> <html> <head> ...

  8. vi设置行号

     首先,我们先打开一个文件,用vim 文件名 就可以直接使用vim打开   我们事先写了一些内容在这个txt里面,我们可以看到如下内容   在这里,我们可以直接敲命令, :set number 或者 ...

  9. ABP拦截器之AuthorizationInterceptor

    在整体介绍这个部分之前,如果对ABP中的权限控制还没有一个很明确的认知,请先阅读这篇文章,然后在读下面的内容. AuthorizationInterceptor看这个名字我们就知道这个拦截器拦截用户一 ...

  10. 一、查看MVC4还是MVC5

    一.查看MVC版本找到那个dll.属性.就可以看到版本 二.MVC添加WebAPI Visual Studio 已向项目“Web”添加 ASP.NET Web API 2 的 全部集合 个依赖项. 项 ...