-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
125 lines (107 loc) · 2.88 KB
/
index.php
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
<?php
$server = "localhost";
$username = "root";
$password = "root";
$dbname = "sortable";
//Create Connection
$conn = new mysql($server, $username, $password, $dbname);
//Check Connection
if ($conn->connect_error){
die("Connection Failed: ". $conn->connect_error);
}
$sql = "Select *, groups.title AS group_title, groups.order_id AS group_order_id
FROM groups LEFT JOIN items ON items.group_id = groups.id
ORDER BY groups.order_id, items.order_id";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
print_r($row)
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sortable Like Trello</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
</head>
<style type="text/css">
body{
background: #ec407a;
}
.wrapper{
width: 80%;
margin:10px auto;
}
</style>
<body>
<div class="wrapper">
<div id="items" class="row">
<?
$prev_group_id = "";
while ($row = $result->fetch_assoc()):
?>
<? if($prev_group_id != $row['group_id']): ?>
<div id="group1" class="group col s4" data-id="<?= $row['group_id'] ?>">
<h1><?= $row['group_id'] ?></h1>
<div class="group-items collection">
<li class="collection-item" data-id="<?= $row['group_id'] ?>"><?= $row['title'] ?></li>
<? endif; ?>
<? if($prev_group_id != $row['group_id']): ?>
</div>
</div>
<? endif; ?>
<?
$prev_group_id = $row['group_id'];
endwhile;
?>
</div>
</div>
<script type="text/javascript" src="node_modules/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="node_modules/sortablejs/Sortable.min.js"></script>
<script type="text/javascript">
var container = document.getElementById('items');
Sortable.create(container,{
handle:'h1',
draggable:'.group',
onUpdate: function(evt){
update_via_ajax(evt.from.children,'groups');
}
});
var group = document.getElementsByClassName('group-items');
for (var i = 0; i<group.length; i++) {
Sortable.create(group.item(i),{
group: 'item-list'.
onUpdate:function(evt){
update_via_ajax(evt.from.children,'items');
},
onAdd:function(evt){
update_via_ajax(evt.item.parentElement.children,'items');
data = {
action : "on_add",
type : "items",
items : parseInt(evt.item.getAttribute('data-id'));
group_id : parseInt(evt.item.parentElement.parentElement.getAttribute('data-id'));
};
$.post('update.php',data).done(function(data){
console.log(data)
});
}
});
}
function update_via_ajax(evt,type){
var items = [];
for (var i = 0; i < children.length; i++) {
items.push(parseInt(children[i].getAttribute('data-id')));
}
//request ajax
data = {
action : "on_update",
type : type,
items : items
};
$.post('update.php',data).done(function(data){
console.log(data)
});
}
</script>
</body>
</html>