1. 程式人生 > >mui做的上拉重新整理和下拉載入和日期選擇器

mui做的上拉重新整理和下拉載入和日期選擇器

var pageCount = 1;//頁碼 var date_inputs = document.getElementsByClassName('select_date'); var today = new Date(); var thisyear = today.getFullYear(); var thismonth = today.getMonth(); today.setFullYear(thisyear,thismonth,1); var startDate,endDate; startDate = date_inputs[0
].value =formatter(today) ;//當月的1號 endDate = date_inputs[1].value =formatter(new Date()) ;//當前時間的日期 var resourse = {data:[],pageCount:0}; window.onload = function () { getData(startDate,endDate,'',pageCount); (function ($) { $.init( //初始化上拉下拉控制元件 { pullRefresh: { container: '#pullrefresh'
, down: { callback: pulldownRefresh }, up: { contentrefresh: '正在載入...', callback: pullupRefresh } } } ); var
inputs = $('.select_date'); inputs.each(function (i, input) { input.addEventListener('tap', function () { var _self = this; if (_self.picker) { _self.picker.show(function (rs) { input.value = rs.text; _self.picker.dispose(); _self.picker = null; }); } else { var optionsJson = this.getAttribute('data-options') || '{}'; var options = JSON.parse(optionsJson); _self.picker = new $.DtPicker(options); _self.picker.show(function (rs) { input.value = rs.text; //這個時候已經能拿到日期 compareDate(date_inputs[0].value,date_inputs[1].value); startDate = date_inputs[0].value; endDate = date_inputs[1].value; _self.picker.dispose(); _self.picker = null; }); } }, false); }); function pulldownRefresh() { setTimeout(function() { pageCount = 1; var text = document.getElementById('mysearch').value; getData(startDate,endDate,text,pageCount); // mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //refresh completed deteleTip(); }, 1500); } /** * 上拉載入具體業務實現 */ function pullupRefresh() { deteleTip(); setTimeout(function() { mui('#pullrefresh').pullRefresh().endPullupToRefresh(); pageCount++; // mui('#pullrefresh').pullRefresh().endPullupToRefresh((++pageCount>resourse.pageCount)); //引數為true代表沒有更多資料了。呼叫了一次後面再呼叫就不執行下拉載入了,不知道是不是我使用的方法不對,所以我手動寫了一個提示語notMassge‘沒有更多資料了。。。’ if(pageCount <= resourse.pageCount){ resourse.data = []; var text = document.getElementById('mysearch').value; getData(startDate,endDate,text,pageCount,'pullupRefresh'); }else{ var notMassge = document.createElement('div'); notMassge.className = 'notMassge'; notMassge.id = 'notMassge'; notMassge.innerHTML = '沒有更多資料了。。。'; var buylist = document.body.querySelector('.buylist'); buylist.after(notMassge); } }, 1500); } })(mui); }; var notbuy_list = document.getElementById("notbuy_list"); //網路請求資料 var getData = function (start_Date, end_Date, str, PageCount, Refresh) { $.ajax({ type: "POST", //傳值方式 url: "******", //後臺處理地址 data: { PageCount:PageCount //頁碼,必填,首次為1,下拉載入,下拉一次+1 }, success: function (res) { if(Refresh){ //如果是下拉重新整理 for (var i=0;i<res.Items.length;i++){ res.Items[i].PRESCRIPTDATE = ChangeDateFormat(res.Items[i].PRESCRIPTDATE); } var html = template("notbuy_list_data", {data: res.Items}); document.getElementById('notbuy_list_medic').insertAdjacentHTML('beforeEnd', html); }else { var conent = document.getElementById('pullrefresh'); if(res.Items.length >0){ conent.style.display = 'block'; resourse.pageCount = res.TotalPages; for (var i=0;i<res.Items.length;i++){ res.Items[i].PRESCRIPTDATE = ChangeDateFormat(res.Items[i].PRESCRIPTDATE); } resourse.data = res.Items; var notbuy_list_data = template("notbuy_list_data", resourse); document.getElementById('notbuy_list_medic').innerHTML = notbuy_list_data; }else{ conent.style.display = 'none'; } } } //引數 }); } function deteleTip() { //刪除‘沒有更多資料’提示語; var parent = document.getElementById('prescription_content'); var child = document.getElementById("notMassge"); if(child){ parent.removeChild(child); } } function pushPage(ID) { //寫跳轉頁面的方法 console.info(ID); // history.go(); } function seachText() { deteleTip(); resourse.data = []; pageCount = 1; var text = document.getElementById('mysearch').value; if(clearSpace(text).length >0){ if (compareDate(startDate,endDate)) { getData(startDate,endDate,text,pageCount); } }else{ alert('請輸入關鍵字'); } } function getUrlParam (id, url) { //獲得位址列中的引數 window.location.search var reg = "/(\\?|\\&|\\&&)" + id + "=([^\\&]+)/im"; reg = eval(reg); var result = url.match(reg); if (result && result[2]) { return result[2]; } else { return null; } } //去除字串空格 function clearSpace (str){ var result; result = str.replace(/(^\s+)|(\s+$)/g,""); result = result.replace(/\s/g,""); return result; }; //日期比較 function compareDate(start, end) { console.info(start+" "+end); start = new Date(start); end = new Date(end); if (end - start < 0) { alert("結束日期要大於開始日期"); return false; } return true; } function formatter(date) { var y = date.getFullYear(); var m = date.getMonth() + 1; var d = date.getDate(); return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d); }; function ChangeDateFormat(val) { if (val != null) { var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10)); //月份為0-11,所以+1,月份小於10時補個0 var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); return date.getFullYear() + "-" + month + "-" + currentDate; } return ""; }