本文实例为大家分享了MUI实现上拉加载和下拉刷新展示的具体代码,供大家参考,具体内容如下
编写存储过程分页
(此处使用T-sql)sql;">
CREATE PROC [dbo].[Common_PageList]
(
@tab nvarchar(max),---表名
@strFld nvarchar(max),--字段字符串
@strWhere varchar(max),--where条件
@PageIndex int,--页码
@PageSize int,--每页容纳的记录数
@Sort VARCHAR(255),--排序字段及规则,不用加order by
@IsGetCount bit --是否得到记录总数,1为得到记录总数,0为不得到记录总数,返回记录集
)
AS
declare @strsql nvarchar(max)
set nocount on;
if(@IsGetCount = 1)
begin
set @strsql='SELECT COUNT(0) FROM ' + @tab + ' WHERE ' + @strWhere
end
else
begin
set @strsql=' SELECT * FROM (SELECT ROW_NUMBER()
OVER(ORDER BY ' + @Sort + ') AS rownum,' + @strFld + ' FROM ' + @tab + ' where ' + @strWhere + ') AS Dwhere
WHERE rownum BETWEEN ' + CAST(((@PageIndex-1)*@PageSize + 1) as nvarchar(20)) + ' and ' + cast((@PageIndex*@PageSize) as nvarchar(20))
end
set nocount off;
webApi接口
(ADO.NET部分封装了,此处是调用形式)
///
///
[HttpPost]
public object test(JObject data)
{
using (var db = new DbBase())
{
<a href="/tag/sql/" target="_blank" class="keywords">sql</a>Parameter[] arr = {
new <a href="/tag/sql/" target="_blank" class="keywords">sql</a>Parameter{ ParameterName="tab",Value=data["tab"].ToString()},new <a href="/tag/sql/" target="_blank" class="keywords">sql</a>Parameter{ ParameterName="strFld",Value=data["strFld"].ToString()},new <a href="/tag/sql/" target="_blank" class="keywords">sql</a>Parameter{ ParameterName="strWhere",Value=data["strWhere"].ToString()},new <a href="/tag/sql/" target="_blank" class="keywords">sql</a>Parameter{ ParameterName="PageIndex",Value=Convert.ToInt32(data["PageIndex"])},new <a href="/tag/sql/" target="_blank" class="keywords">sql</a>Parameter{ ParameterName="PageSize",Value=Convert.ToInt32(data["PageSize"])},new <a href="/tag/sql/" target="_blank" class="keywords">sql</a>Parameter{ ParameterName="Sort",Value=data["Sort"].ToString()},new <a href="/tag/sql/" target="_blank" class="keywords">sql</a>Parameter{ ParameterName="IsGetCount",Value=Convert.ToInt32(data["IsGetCount"])},};
return RepositoryBase.ExecuteReader(db,"Common_PageList",arr);
}
页面实现
<Meta charset="utf-8">
Hello MUI
<Meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<Meta name="apple-mobile-web-app-capable" content="yes">
<Meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="stylesheet" href="../css/mui.min.css" rel="external nofollow" >
<style type="text/css">
</style>
<header class="mui-bar mui-bar-nav">
<h1 class="mui-title">下拉刷新(单webview模式)</h1>
</header>
<div id="pullrefresh" class="mui-content mui-scroll-wrapper">
<div class="mui-scroll">
<ul id="container" class="mui-table-view mui-table-view-chevron"></ul>
</div>
</div>
<ul id="temp" class="mui-table-view" style="display: none;">
<li class="mui-table-view-cell">
<a class="mui-navigate-right">
@name
</a>
</li>
</ul>
<script src="../js/mui.js" type="text/javascript" charset="utf-8"></script>
<script>
/**
数据源<a href="/tag/fenye/" target="_blank" class="keywords">分页</a>参数对象
* */
var obj={ tab:'SystemUsers',strFld:'code,Username',strWhere:'1=1',PageIndex:1,PageSize:10,Sort:'Username',IsGetCount:0,pageCount:0
}
//webApi服务器接口
var apiUrl="http://192.168.200.114:8123/api/Common/Base/test";
/**
* 定义数据源按什么html方式展示,动态<a href="/tag/shengcheng/" target="_blank" class="keywords">生成</a>html字符串的逻辑
**/
var drawHtml=function(data){
var html=""
for (var i=0;i<data.length;i++)
{
var temp=document.getElementById("temp").innerHTML; //模板
html+=temp.toString().replace('@name',data[i].Username); //替换参数叠加
}
return html;
}
mui.ready(function(){
/**
MUI配置项
* */
mui.init({
pullRefresh: {
container: '#pullrefresh',down: {
callback: pulldownRefresh
},//END 下拉刷新
up : {
contentrefresh : "正在加载...",//可选,正在加载状态时,上拉加载控件上<a href="/tag/xianshi/" target="_blank" class="keywords">显示</a>的<a href="/tag/biaoti/" target="_blank" class="keywords">标题</a><a href="/tag/neirong/" target="_blank" class="keywords">内容</a>
contentnomore:'没有更多数据了',//可选,请求完毕若没有更多数据时<a href="/tag/xianshi/" target="_blank" class="keywords">显示</a>的提醒<a href="/tag/neirong/" target="_blank" class="keywords">内容</a>;
callback :pullupRefresh //必选,刷新<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>,根据具体业务来编写,比如通过ajax从服务器<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>新数据;
} //END 上拉加载
}
});
//<a href="/tag/tongji/" target="_blank" class="keywords">统计</a>:数据总数、<a href="/tag/fenye/" target="_blank" class="keywords">分页</a>总数
obj.IsGetCount=1;
loadData(apiUrl,obj,0);
//初始化列表数据(第一页)
obj.IsGetCount=0;
loadData(apiUrl,"down",function(data){
//此处实现动态绘制DOM的逻辑,根据数据源自行处理要展示的html方式
return drawHtml(data);
});
});
/*
读取数据源
url:api地址
dataObj:数据源<a href="/tag/fenye/" target="_blank" class="keywords">分页</a><a href="/tag/chaxun/" target="_blank" class="keywords">查询</a>参数对象
Timeout:指定多少时间后绘制<a href="/tag/yemian/" target="_blank" class="keywords">页面</a>DOM展示对象,
动态<a href="/tag/shengcheng/" target="_blank" class="keywords">生成</a>的元素<a href="/tag/daima/" target="_blank" class="keywords">代码</a>包含在一个setTimeout<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>里,
用 setTimeout,主要对于下拉刷新间隔时间
loadType:加载方式:up(上拉加载)、down(上拉刷新)
drawFunction:回调<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>,处理拿到数据源,绘制DOM展示界面的html
,要接收返回的html字符串
* */
var loadData=function(url,dataObj,Timeout,loadType,drawFunction){
mui.ajax(url,{
type: "post",data:dataObj,async:false,headers: {'Content-Type': 'application/json'},success: function(data) {
//<a href="/tag/tongji/" target="_blank" class="keywords">统计</a>出数据总数
if(dataObj.IsGetCount==1)
{
obj.pageCount=Math.ceil(parseInt(data[0].Column1)/obj.PageSize) ;
return;
}
setTimeout(function() {
//动态绘制出的Dom元素,结合数据展现
var html= drawFunction(data);
if(loadType=="up") //上拉加载
{
if(obj.PageIndex==obj.pageCount)
{
//参数为true代表没有更多数据了。
mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
}
else
{
mui('#pullrefresh').pullRefresh().endPullupToRefresh();
}
//将<a href="/tag/xiayiye/" target="_blank" class="keywords">下一页</a>数据追加到容器
document.getElementById("container").innerHTML=document.getElementById("container").innerHTML+html;
}
else if(loadType=="down") //下拉刷新
{
// 该<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>的作用是<a href="/tag/guanbi/" target="_blank" class="keywords">关闭</a>“正在刷新”的样式<a href="/tag/tishi/" target="_blank" class="keywords">提示</a>,<a href="/tag/neirong/" target="_blank" class="keywords">内容</a>区域回滚顶部位置
mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
//将第一页数据覆盖到容器
document.getElementById("container").innerHTML=html;
//启用上拉加载
mui('#pullrefresh').pullRefresh().enablePullupToRefresh();
}
},Timeout);//END setTimeout();
},//END success();
error: function(xhr,type,errorThrown) {
console.log(type);
}//END error();
});//END ajax();
}//END loadData();
/**
* 下拉刷新具体业务实现
*/
function pulldownRefresh() {
console.log('重置数据,初始到第一页');
obj.PageIndex=1;
loadData(apiUrl,1000,function(data){
//此处实现动态绘制DOM的逻辑,根据数据源自行处理要展示的html方式
return drawHtml(data);
});
} //END pulldownRefresh() 下拉刷新<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a>
/**
* 上拉加载具体业务实现
*/
function pullupRefresh() {
obj.PageIndex++;//当前页累加,加载<a href="/tag/xiayiye/" target="_blank" class="keywords">下一页</a>的数据
console.log("加载第:"+obj.PageIndex+"页");
console.log("页总数:"+obj.pageCount);
loadData(apiUrl,"up",function(data){
//此处实现动态绘制DOM的逻辑,根据数据源自行处理要展示的html方式
return drawHtml(data);
});
}
</script>