851276240@qq.com
851276240@qq.com
  • 发布:2019-09-02 10:24
  • 更新:2019-09-02 10:24
  • 阅读:799

ajax请求的上拉加载更多案例

分类:MUI
页面用的是jsp
<div class="mui-off-canvas-wrap mui-draggable">  
        <div class="mui-inner-wrap">  
            <!-- <header class="mui-bar mui-bar-nav">   
                <h1 class="mui-title"></h1>  
            </header> -->  
            <div id="pullrefresh" class="mui-content mui-scroll-wrapper">  
                <div class="mui-scroll">  
                    <ul class="mui-table-view">  
                        <li class="mui-table-view-cell" style="text-align: center;" id="total" value="${total/100}">  
                            合计:¥${total/100 }  
                        </li>  
                    </ul>  
                    <ul class="mui-table-view" id="ulId">  
                        <c:forEach items="${checks}" var="check">  
                            <li class="mui-table-view-cell" id="list${check.id }" value="${check.id}" onclick="detail(${check.id})">  
                                <p class="mui-badge">¥${check.money/100}</p> ${check.remark}  
                                <p class="mui-ellipsis">车牌号:${check.carId}</p>  
                                <p class='mui-ellipsis' id="createDate"><fmt:formatDate value="${check.create_date}" pattern="yyyy-MM-dd HH:mm:ss" /></p>  
                            </li>  
                        </c:forEach>  
                    </ul>  
                </div>  
            </div>  
        </div>  
    </div>  
    <script src="<%=basePath%>open/js/mui.min.js"></script>  
    <script type="text/javascript">  
            mui.init({  
              pullRefresh : {  
                container : "#pullrefresh",//待刷新区域标识,querySelector能定位的css选择器均可,比如:id、.class等  
                up : {  
                  contentrefresh : "正在加载...",//可选,正在加载状态时,上拉加载控件上显示的标题内容  
                  callback : getCheckInfo //必选,刷新函数,根据具体业务来编写,比如通过ajax从服务器获取新数据;  
                }  
              }  
            });  
            var page = 1;  

            // 上拉加载操作  
            function getCheckInfo() {  
                page++;  
                mui.ajax('url?token=<%=token%>&page=' + page, {  
                        dataType: 'json', //服务器返回json格式数据  
                        type: 'POST', //HTTP请求类型  
                        timeout: 10000, //超时时间设置为10秒;  
                        headers: {  
                            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'  
                        },  
                        success: function(data) {  
                            setTimeout(function() {  
                                mui('#pullrefresh').pullRefresh().endPullupToRefresh((data.data.length==0)); // 参数为true代表没有更多数据了。  
                                var table = document.body.querySelector('#ulId');  
                                var cells = document.body.querySelectorAll('.mui-table-view-cell');  
                                var result = data.data;  
                                for (var i = 0; i < result.length; i++) {  
                                    var li = document.createElement('li');  
                                    li.className = 'mui-table-view-cell';  
                                    li.id = 'list' + result[i].id;  
                                    li.value = result[i].id;  
                                    li.innerHTML = '<p class="mui-badge">¥' + result[i].money/100 + '</p>' +  result[i].remark + '<p class="mui-ellipsis">车牌号:' + result[i].carId + '</p><p class="mui-ellipsis" id="createDate">' + result[i].create_date + '</p>';  
                                    table.appendChild(li);  
                                }  
                            },1500);  
                        },  
                        error: function(xhr, type, errorThrown) {  
                            console.log(errorThrown);  
                        }  
                    });  
            }  
            mui.init({  
              gestureConfig:{  
               tap: true, //默认为true  
               doubletap: true, //默认为false  
               longtap: true, //默认为false  
               swipe: true, //默认为true  
               drag: true, //默认为true  
               hold:false,//默认为false,不监听  
               release:false//默认为false,不监听  
              }  
            });  
            mui("#ulId").on("longtap","li",function(list) {  
                // 逻辑代码,例如跳转详情页、ajax  
                    mui.confirm( "是否删除该记录?", "提示", ["是", "否"], function(e) {  
                    if (e.index == 0) {  
                    if (list.path[1].value == undefined) {  
                        delCheck(list.path[0].value);  
                    } else {  
                        delCheck(list.path[1].value);  
                    }  

                    }  
                });  
            });  

            // 执行删除操作  
            function delCheck(id) {  
                mui.ajax('url?token=<%=token%>&id=' + id, {  
                    dataType: 'json', //服务器返回json格式数据  
                    type: 'POST', //HTTP请求类型  
                    timeout: 10000, //超时时间设置为10秒;  
                    headers: {  
                        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'  
                    },  
                    success: function(data) {  
                        var total = data.data/100;  
                        if (parseInt(total, 10) === total) {  
                            total = total + '.0';  
                        }  
                        document.getElementById("list" + id).remove();  
                        mui.alert("删除成功!", "提示", "确定");  
                        document.getElementById("total").innerHTML = "合计:¥" + total;  
                        document.getElementById("total").value = total;  
                    },  
                    error: function(xhr, type, errorThrown) {  
                        console.log(errorThrown);  
                    }  
                });  
            };  
        </script>
0 关注 分享

要回复文章请先登录注册