-
Notifications
You must be signed in to change notification settings - Fork 17
/
todo-webSQL-example.html
129 lines (116 loc) · 3.89 KB
/
todo-webSQL-example.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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Todo in webSQL</title>
<meta name="author" content="Ido Green">
<script src="http://code.jquery.com/jquery.min.js" ></script>
<style>
#ourList {
background: lightcyan;
padding: 25px;
border-radius: 15px;
}
h1 {
font-size: 130%;
}
h3 {
font-size: 110%;
margin-top: 45px;
}
#todo {
background: lightgoldenrodyellow;
width: 250px;
}
input {
width: 130px;
padding: 5px;
}
li {
background: lightblue;
padding: 5px;
}
</style>
</head>
<body>
<h1>ToDo list - WebSQL Example</h1>
<ul id="todoItems"></ul>
<input type="text" id="todo" name="todo" placeholder="What do you need to do?"/>
<input type="submit" value="Add Todo Item" onclick="addTodo(); return false;" />
<h3>Todos</h3>
<input type="submit" value="Show All Todos" onclick="showAll(); return false;" />
<input type="submit" value="Delete All Todos" onclick="cleanAll(); return false;" />
<div id="ourList">
</div>
<script>
function addTodo() {
var todo = document.getElementById("todo");
var task = {
"id": new Date().getTime(),
"text": todo.value };
database.transaction(function(tx) {
tx.executeSql('INSERT INTO tasks (id, text) values (?, ?)', [task.id, task.text]);
});
// now let clean it to the next todo
todo.value = "";
showAll();
}
function showAll() {
document.getElementById("ourList").innerHTML = "" ;
database.transaction(function(tx) {
tx.executeSql('SELECT * FROM tasks', [], function (tx, results) {
var len = results.rows.length
var ul = document.createElement("ul");
for (var i = 0; i < len; i++) {
var li = document.createElement("li");
var t = document.createTextNode(i+ ") key: " + results.rows.item(i).id +
" => Todo text: " + results.rows.item(i).text);
var a = document.createElement("a");
a.textContent = " [Delete]";
a.setAttribute('data-key', results.rows.item(i).id);
a.setAttribute('data-val', results.rows.item(i).text);
a.addEventListener("click", function() {
deleteTodo(this.getAttribute("data-key"),this.getAttribute("data-val") );
}, false);
li.appendChild(t);
li.appendChild(a);
ul.appendChild(li);
}
document.getElementById("ourList").appendChild(ul);
});
});
}
function deleteTodo(id, text) {
if (confirm("Are you sure you want to Delete "+ text +"?")) {
database.transaction(function(tx) {
tx.executeSql('DELETE FROM tasks WHERE id=?', [id]);
});
showAll();
}
}
function cleanAll() {
if (confirm("Are you sure you want to delete all Todos?")) {
document.getElementById("ourList").innerHTML = "" ;
database.transaction(function(tx) {
tx.executeSql('DROP TABLE IF EXISTS tasks', [])
});
}
}
function initDB() {
database = openDatabase('todos1', '1.0', 'todo list example db', 2*1024*1024);
if (database) {
database.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS tasks (id REAL UNIQUE, text TEXT)", []);
});
}
}
// start the party
$(function() {
console.log("-- started the main page and init our DB --");
initDB();
showAll();
});
</script>
</body>
</html>