Home >Web Front-end >JS Tutorial >Example sharing of MUI implementation of pull-up loading and pull-down refresh

Example sharing of MUI implementation of pull-up loading and pull-down refresh

零下一度
零下一度Original
2018-05-25 10:07:343561browse

This article mainly introduces the pull-up loading and pull-down refresh effects of MUI to everyone in detail. It has certain reference value. Interested friends can refer to it.

The examples in this article share MUI with everyone. The specific code to implement pull-up loading and pull-down refresh display is for your reference. The specific content is as follows

WritingStored ProcedurePaging(T-SQL is used here)

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

print @strSql
exec (@strSql)

set nocount off;

webApi interface (ADO.NET is partially encapsulated, here is the calling form)

/// 测试mui下拉刷新
    /// </summary>
    /// <param name="flag"></param>
    /// <returns></returns>
    [HttpPost]
    public object test(JObject data)
    {

      using (var db = new DbBase())
      {
        SqlParameter[] arr = { 
                   new SqlParameter{ ParameterName="tab",Value=data["tab"].ToString()},
                   new SqlParameter{ ParameterName="strFld",Value=data["strFld"].ToString()},
                   new SqlParameter{ ParameterName="strWhere",Value=data["strWhere"].ToString()},
                   new SqlParameter{ ParameterName="PageIndex",Value=Convert.ToInt32(data["PageIndex"])},
                   new SqlParameter{ ParameterName="PageSize",Value=Convert.ToInt32(data["PageSize"])},
                   new SqlParameter{ ParameterName="Sort",Value=data["Sort"].ToString()},
                   new SqlParameter{ ParameterName="IsGetCount",Value=Convert.ToInt32(data["IsGetCount"])},
                   };


      return  RepositoryBase.ExecuteReader(db, "Common_PageList", arr);


      }

Page implementation

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <title>Hello MUI</title>
    <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>
  </head>
  <body>
    
      <header class="mui-bar mui-bar-nav">
      <h1 class="mui-title">下拉刷新(单webview模式)</h1>
    </header>
    
    <p id="pullrefresh" class="mui-content mui-scroll-wrapper">
      <p class="mui-scroll">
        
          <ul id="container" class="mui-table-view mui-table-view-chevron"></ul>
      
      </p>
    </p>
     <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>
  
        /**
         数据源分页参数对象
         * */
        var obj={ tab:&#39;SystemUsers&#39;,
              strFld:&#39;code,Username&#39;,
              strWhere:&#39;1=1&#39;,
              PageIndex:1,
              PageSize:10,
              Sort:&#39;Username&#39;,
              IsGetCount:0,
              pageCount:0
            }
        
      //webApi服务器接口  
      var apiUrl="http://192.168.200.114:8123/api/Common/Base/test";
      
      
        /**
         * 定义数据源按什么html方式展示,动态生成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(&#39;@name&#39;,data[i].Username); //替换参数叠加
            }
             
           return html;
        }
    
      mui.ready(function(){
       /**
         MUI配置项
         * */    
      mui.init({
        pullRefresh: {
          container: &#39;#pullrefresh&#39;,
          down: {
            callback: pulldownRefresh
          }, //END 下拉刷新
          up : {  
             contentrefresh : "正在加载...",//可选,正在加载状态时,上拉加载控件上显示的标题内容
             contentnomore:&#39;没有更多数据了&#39;,//可选,请求完毕若没有更多数据时显示的提醒内容;
             callback :pullupRefresh //必选,刷新函数,根据具体业务来编写,比如通过ajax从服务器获取新数据;
          } //END 上拉加载
        }
      });
             
        //统计:数据总数、分页总数  
        obj.IsGetCount=1;           
        loadData(apiUrl,obj,0);
       
        //初始化列表数据(第一页)
        obj.IsGetCount=0;    
         loadData(apiUrl,obj,0,"down",function(data){           
           //此处实现动态绘制DOM的逻辑,根据数据源自行处理要展示的html方式                     
           return drawHtml(data);
           
         });
        
        
      });
             
      /*
       读取数据源
       url:api地址
       dataObj:数据源分页查询参数对象
       Timeout:指定多少时间后绘制页面DOM展示对象,
               动态生成的元素代码包含在一个setTimeout函数里,
               用  setTimeout,主要对于下拉刷新间隔时间
       loadType:加载方式:up(上拉加载)、down(上拉刷新)    
       drawFunction:回调函数,处理拿到数据源,绘制DOM展示界面的html
                   ,要接收返回的html字符串
       * */
      
      var loadData=function(url,dataObj,Timeout,loadType,drawFunction){
        
        mui.ajax(url, {
                type: "post",
                data:dataObj,
                async:false,
                headers: {&#39;Content-Type&#39;: &#39;application/json&#39;},
                success: function(data) {
                    
                  //统计出数据总数
                  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(&#39;#pullrefresh&#39;).pullRefresh().endPullupToRefresh(true);
                        }
                        else
                        {
                          mui(&#39;#pullrefresh&#39;).pullRefresh().endPullupToRefresh(); 
                        }
                      
                    //将下一页数据追加到容器  
                    document.getElementById("container").innerHTML=document.getElementById("container").innerHTML+html;
                  }
                  else if(loadType=="down") //下拉刷新
                  {
                    // 该方法的作用是关闭“正在刷新”的样式提示,内容区域回滚顶部位置
                    mui(&#39;#pullrefresh&#39;).pullRefresh().endPulldownToRefresh(); 
                    
                    //将第一页数据覆盖到容器
                    document.getElementById("container").innerHTML=html;
                    
                    //启用上拉加载
                    mui(&#39;#pullrefresh&#39;).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(&#39;重置数据,初始到第一页&#39;);
          obj.PageIndex=1;
  
           loadData(apiUrl,obj,1000,"down",function(data){
           //此处实现动态绘制DOM的逻辑,根据数据源自行处理要展示的html方式                     
           return drawHtml(data);
             
           });
    
    } //END pulldownRefresh() 下拉刷新函数
        
  
      /**
       * 上拉加载具体业务实现
       */
      function pullupRefresh() {
        obj.PageIndex++;//当前页累加,加载下一页的数据       
        console.log("加载第:"+obj.PageIndex+"页");
        console.log("页总数:"+obj.pageCount);
          
       loadData(apiUrl,obj,1000,"up",function(data){
           //此处实现动态绘制DOM的逻辑,根据数据源自行处理要展示的html方式                     
           return drawHtml(data);
           
       });
        

      }
      
    

    </script>
  </body>

</html>

The above is the detailed content of Example sharing of MUI implementation of pull-up loading and pull-down refresh. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn