forked from Daddies-of-SE/ReedSailing-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmember-list.html
347 lines (318 loc) · 12.7 KB
/
member-list.html
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
<meta charset="UTF-8">
<title>用户管理-一苇以航</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<link rel="stylesheet" href="./css/font.css">
<link rel="stylesheet" href="./css/xadmin.css">
<script src="./lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="./js/xadmin.js"></script>
<script src="./js/jquery.min.js"></script>
<script src="./js/app.js"></script>
<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="layui-fluid">
<div class="layui-row layui-col-space15">
<div class="layui-col-md12">
<div class="layui-card">
<div class="layui-card-body ">
<form class="layui-form layui-col-space5">
<div class="layui-inline layui-show-xs-block">
<input type="text" name="username" id="keyword" placeholder="请输入用户名" autocomplete="off"
class="layui-input">
</div>
<div class="layui-inline layui-show-xs-block">
<button class="layui-btn" lay-submit="" lay-filter="sreach" onclick="search()"><i
class="layui-icon"></i>
</button>
</div>
<div class="layui-inline layui-show-xs-block" style="float:right">
<a class="layui-btn layui-btn-small" style="line-height:1.6em"
onclick="location.reload()" title="刷新">
<i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
</a>
</div>
</form>
</div>
<div class="layui-card-header">
<button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除
</button>
</div>
<div class="layui-card-body layui-table-body layui-table-main">
<table class="layui-table layui-form">
<thead>
<tr>
<th>
<input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
</th>
<th>ID</th>
<th>昵称</th>
<th>头像</th>
<th>邮箱</th>
<th>签名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="layui-card-body " style="display:none">
<div class="page">
<div>
<a class="prev" href=""><<</a>
<a class="num" href="">1</a>
<span class="current">2</span>
<a class="num" href="">3</a>
<a class="num" href="">489</a>
<a class="next" href="">>></a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
headers = { 'token': localStorage.token };
var cnt, tbody;
layui.use(['laydate', 'form'], function () {
var laydate = layui.laydate;
window.form = layui.form;
cnt = 0;
tbody = document.getElementsByTagName('tbody')[0];
// // 监听全选
// form.on('checkbox(checkall)', function (data) {
// if (data.elem.checked) {
// $('tbody input').prop('checked', true);
// } else {
// $('tbody input').prop('checked', false);
// }
// form.render('checkbox');
// });
form.render();
// 实现 全选 反选
form.on('checkbox(checkall)', function (data) {
$(this).parent().parent().parent().siblings("tbody").find("input").prop("checked", this.checked);
form.render('checkbox');
});
/**
* 以下代码实现:
* 1 当子项全部选中时,全选被选中
* 2 当子项未全部选中时,全选不选中
*/
form.on('checkbox(authority)', function (data) {
if (this.checked) {//当子项全部选中时,全选被选中
if ($(this).parent().parent().siblings("tr").find("input").not("input:checked").length === 0) {
$(this).parent().parent().parent().siblings("thead").find('input').prop("checked", true);
form.render('checkbox');
}
} else { // 子项未选中 全选不选中
$(this).parent().parent().parent().siblings("thead").find('input').prop("checked", false);
form.render('checkbox');
}
});
//执行一个laydate实例
laydate.render({
elem: '#start' //指定元素
});
//执行一个laydate实例
laydate.render({
elem: '#end' //指定元素
});
var index = layer.open({
type: 3,
icon: 1
});
var search_name = getQueryVariable();
console.log(search_name);
if (search_name) {
$.ajax(server + 'users/search/', {
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({name: search_name}),
headers: headers
}).done(function (data) {
console.log(data);
for (var i = 0; i < data.length; i++) {
member_add(data[i]);
}
}).fail(function (xhr, status) {
console.log(xhr);
console.log(status);
layer.msg("网络错误", { icon: 2 });
}).always(function () {
layer.close(index);
});
}
else {
$.ajax(server + 'users/', {
method: 'GET',
headers: headers
}).done(function (data) {
console.log(data);
for (var i = 0; i < data.length; i++) {
member_add(data[i]);
}
}).fail(function (xhr, status) {
console.log(xhr);
console.log(status);
layer.msg("网络错误", { icon: 2 });
}).always(function () {
layer.close(index);
});
}
});
/*用户-停用*/
function member_stop(obj, id) {
layer.confirm('确认要停用吗?', function (index) {
if ($(obj).attr('title') == '启用') {
//发异步把用户状态进行更改
$(obj).attr('title', '停用')
$(obj).find('i').html('');
$(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
layer.msg('已停用!', { icon: 5, time: 1000 });
} else {
$(obj).attr('title', '启用')
$(obj).find('i').html('');
$(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
layer.msg('已启用!', { icon: 5, time: 1000 });
}
});
}
function search() {
var url = self.location.href;
var search_name = getQueryVariable();
var value = $("#keyword").val();
console.log(value);
var end = url.indexOf('=');
if (end != -1) {
url = url.substring(0, end + 1);
url = url.concat(encodeURI(value));
}
else {
url = url.concat("?search_key=");
url = url.concat(encodeURI(value));
}
window.location.href = url;
}
function getQueryVariable() {
var query = window.location.search.substring(1);
if (query == "") {
console.log("No search")
return null;
}
var pair = query.split("=");
return decodeURI(pair[1]);
}
/*用户-删除*/
function member_del(obj, id) {
//TODO:用户不存在时如何提示?
layer.confirm('确认要删除吗?', function (index) {
//发异步删除数据
$.ajax(server + `users/${id}/`, {
method: 'DELETE',
headers: headers
}).done(function (data) {
$(obj).parents("tr").remove();
layer.msg('已删除!', { icon: 1, time: 1000 });
}).fail(function (xhr, status) {
console.log(xhr);
console.log(status);
if (xhr.status === 404) {
layer.msg('该用户不存在', { icon: 5 });
}
else {
layer.msg("网络错误", { icon: 2 });
}
}).always(function () {
//layer.close(index);
});
});
}
/*用户-编辑*/
function member_edit(obj, id) {
//TODO:用户不存在时如何提示?
localStorage.member_id_edit = id;
xadmin.open('编辑', 'member-edit.html', 600, 400);
}
function delAll(argument) {
var ids = [];
var elems = [];
// 获取选中的id
$('tbody input').each(function (index, el) {
if ($(this).prop('checked')) {
ids.push($(this).val());
elems.push($(this));
}
});
layer.confirm('确认要删除吗?' + ids.toString(), function (index) {
//捉到所有被选中的,发异步进行删除
//TODO:用户不存在时如何提示?
var status = true;
for (var i = 0; i < ids.length; i++) {
$.ajax(server + `users/${ids[i]}/`, {
method: 'DELETE',
headers: headers
}).done(function (data) {
$(elems[i]).parents("tr").remove();
//layer.msg('已删除!', { icon: 1, time: 1000 });
}).fail(function (xhr, status) {
console.log(xhr);
console.log(status);
//layer.msg("网络错误");
status = false;
}).always(function () {
//layer.close(index);
});
}
// if (status === true) {
layer.msg('删除成功', { icon: 1 });
// }
// else {
// layer.msg('部分用户未能成功删除,请再次尝试', { icon: 5 });
// }
//$(".layui-form-checked").not('.header').parents('tr').remove();
});
}
function member_add(data) {
cnt++;
var td = document.createElement('tr');
member_update(td, data);
tbody.appendChild(td);
form.render('checkbox');
}
function member_update(td, data) {
for (var key in data) {
if (data[key] == null) {
data[key] = '';
}
}
$(td).html(`
<td><input type="checkbox" name="id" value="${data.id}" lay-skin="primary" lay-filter="authority"></td>
<td>${data.id}</td>
<td>${data.name}</td>
<td><img src="${data.avatar}"></td>
<td>${data.email}</td>
<td>${data.sign}</td>
<td class="td-manage">
<a title="编辑" onclick="member_edit(this,${data.id})"
href="javascript:;">
<i class="layui-icon"></i>
</a>
<a title="删除" onclick="member_del(this,${data.id})" href="javascript:;">
<i class="layui-icon"></i>
</a>
</td>`);
}
</script>
</html>