-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivity-list.html
423 lines (388 loc) · 15.4 KB
/
activity-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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<!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 class="layui-input" autocomplete="off" placeholder="开始日" name="start" id="start">
</div>
<div class="layui-inline layui-show-xs-block">
<input class="layui-input" autocomplete="off" placeholder="截止日" name="end" id="end">
</div> -->
<div class="layui-inline layui-show-xs-block">
<input type="text" name="keyword" id="keyword" placeholder="请输入活动名" id="searchBtn" autocomplete="off"
class="layui-input">
</div>
<div class="layui-inline layui-show-xs-block">
<button type="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>
<button class="layui-btn" onclick="xadmin.open('添加活动','./activity-add.html',600,400)"><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>
<th>人数上限</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', 'table'], function () {
var laydate = layui.laydate,
table = layui.table;
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', //指定元素
trigger: 'click',
});
//执行一个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 + 'activities/search/', {
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({name: search_name}),
headers: headers
}).done(function (data) {
console.log(data);
data = data.sort(compare('pub_time'));
for (var i = 0; i < data.length; i++) {
activity_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 + 'activities/', {
method: 'GET',
headers: headers
}).done(function (data) {
console.log(data);
data = data.sort(compare('pub_time'));
for (var i = 0; i < data.length; i++) {
activity_add(data[i]);
}
}).fail(function (xhr, status) {
console.log(xhr);
console.log(status);
layer.msg("网络错误", { icon: 2 });
}).always(function () {
layer.close(index);
});
}
var a = table.render({
elem: "#test"
});
$('#searchBtn').on('click', function () {
var type = $(this).data('type');
active[type] ? active[type].call(this) : '';
});
var active = {
reload: function () {
var demoReload = $('#demoReload'); //得到搜索框里已输入的数据
//执行重载
table.reload('test', {
page: {
curr: 1 //重新从第 1 页开始
}
, where: {
name: demoReload.val() //在表格中进行搜索
}
});
}
};
// $('#btn .layui-btn').on('click', function(){
// var type = $(this).data('type');
// active[type] ? active[type].call(this) : '';
// });
});
function compare(property){
return function (obj1,obj2){
return Date.parse(obj2[property])-Date.parse(obj1[property]);
}
}
/*用户-停用*/
function app_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 activity_edit(obj, id) {
//TODO:用户不存在时如何提示?
localStorage.activity_id_edit = id;
xadmin.open('编辑', 'activity-edit.html', 600, 400);
}
function del_req(obj, id, silence) {
var result = false;
$.ajax(server + `activities/${id}/`, {
method: 'DELETE',
headers: headers
}).done(function (data) {
$(obj).parents("tr").remove();
if (!silence) {
layer.msg('已删除!', { icon: 1, time: 1000 });
}
result = true;
}).fail(function (xhr, status) {
console.log(xhr);
console.log(status);
if (!silence) {
if (xhr.status === 404) {
layer.msg('该活动不存在', { icon: 5 });
}
else {
layer.msg("网络错误", { icon: 2 });
}
}
}).always(function () {
//layer.close(index);
});
return result;
}
/*删除*/
function activity_del(obj, id) {
//TODO:用户不存在时如何提示?
layer.confirm('确认要删除吗?', function (index) {
//发异步删除数据
del_req(obj, id, false);
});
}
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++) {
status &= del_req(elems[i], ids[i], true);
}
// if (status === true) {
layer.msg('删除成功', { icon: 1 });
// }
// else {
// layer.msg('部分活动未能成功删除,请再次尝试', { icon: 5 });
// }
//$(".layui-form-checked").not('.header').parents('tr').remove();
});
}
function activity_add(data) {
cnt++;
var td = document.createElement('tr');
activity_update(td, data);
tbody.appendChild(td);
form.render('checkbox');
}
function activity_update(td, data) {
for (var key in data) {
if (key === 'review') {
if (data[key]) {
data[key] = '是';
}
else {
data[key] = '否';
}
}
else if (key === 'org') {
if (data[key] == null) {
data[key] = '无';
}
else {
data[key] = data[key].name;
}
}
else if (data[key] == null) {
data[key] = '';
}
}
var type='无';
if(data.type){
type=data.type.name;
}
$(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>${data.owner.name}</td>
<td>开始 ${data.begin_time}<br>结束 ${data.end_time}</td>
<td>${data.pub_time}</td>
<td>${data.description}</td>
<td>${data.contain}</td>
<td>${type}</td>
<td>${data.location.name}</td>
<td>${data.org}</td>
<td>${data.block.name}</td>
<td class="td-manage">
<a title="编辑" onclick="activity_edit(this,${data.id})"
href="javascript:;">
<i class="layui-icon"></i>
</a>
<a title="删除" onclick="activity_del(this,${data.id})" href="javascript:;">
<i class="layui-icon"></i>
</a>
</td>`);
form.render('checkbox');
}
</script>
</html>