base_list_ajax.html
4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!--
作者:dailc
时间:2017-12-27
描述: 基本的列表页面-ajax
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<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">
<title>base list ajax</title>
<link rel="stylesheet" href="../../dist/debug/minirefresh.css" />
<link rel="stylesheet" href="../common/common.css" />
</head>
<body>
<nav class="navbar-header">
<a class="nav-btn-left" href="../index.html">dashboard</a>
最基础的新闻列表-ajax
</nav>
<div class="content">
<div id="minirefresh" class="minirefresh-wrap">
<div class="minirefresh-scroll">
<ul class="data-list" id="listdata">
</ul>
</div>
</div>
</div>
<script type="text/javascript" src="../../dist/debug/minirefresh.js"></script>
<!--
之所以引入mui,仅仅是想用它的ajax功能,换位其它库均可
-->
<script type="text/javascript" src="../scroll-nested/libs/mui/js/mui.js"></script>
<script>
// 记录一个演示最大值
var maxDataSize = 30;
var listDom = document.querySelector('#listdata');
// 延迟只是为了看出效果而已,完全可以去掉
var requestDelayTime = 600;
function ajaxData(success, error) {
setTimeout(function() {
mui.ajax({
type: 'GET',
url: './listdata.json',
success: success,
error: error,
});
}, requestDelayTime);
}
function generateHtmlByData(response) {
if (!response || !response.code || !Array.isArray(response.data)) {
return '';
}
var template = '<li class="list-item"><h3 class="msg-title">{{title}}</h3><span class="msg-fs14 msg-date">{{date}}</span></li>';
var html = '';
var data = response.data;
var len = data.length;
for (var i = 0; i < len; i++) {
// 字符串的拼接效率问题一般现代浏览器都会有优化
html += template.replace(/{{([^{}]*)}}/g, function($0, $1) {
return ($1 && data[i][$1]) || '';
});
}
return html;
}
function parseHtml(strHtml) {
if (!strHtml || typeof(strHtml) !== 'string') {
return null;
}
// 创一个灵活的div
var i,
a = document.createElement('div');
var b = document.createDocumentFragment();
a.innerHTML = strHtml;
while ((i = a.firstChild)) {
b.appendChild(i);
}
return b;
}
var miniRefresh = new MiniRefresh({
container: '#minirefresh',
down: {
callback: function() {
// 每次下拉刷新后,默认情况上拉的状态会被自动重置
ajaxData(function(response) {
// 简单的修改dom,仅示例
listDom.innerHTML = generateHtmlByData(response);
miniRefresh.endDownLoading(true);
}, function(error) {
miniRefresh.endDownLoading(true);
});
}
},
up: {
isAuto: true,
callback: function() {
// endUpLoading,true之后代表无法继续上啦
ajaxData(function(response) {
var html = generateHtmlByData(response);
html && listDom.appendChild(parseHtml(html));
miniRefresh.endUpLoading(listDom.children.length >= maxDataSize ? true : false);
}, function(error) {
// 失败中一定要关闭上拉,防止无限加载
miniRefresh.endUpLoading(true);
});
}
}
});
</script>
</body>
</html>