-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
137 lines (119 loc) · 5.55 KB
/
db.js
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
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var db;
(function () {
var itemData = [
{ name: "Aminophylline : 225 mg", type: "Tablet", price: "5.09" },
{ name: "Vit E : 100 mg", type: "Capsule", price: "5.34" },
{ name: "Analgin : 500 mg", type: "Injection", price: "4.54" },
{ name: "Analgin : 500 mg", type: "Tablet", price: "8.27" },
{ name: "Aspirin : 50 mg", type: "Tablet", price: "1.1" }
];
function initDb() {
var request = indexedDB.open("ItemDB", 1);
request.onsuccess = function (evt) {
db = request.result;
};
request.onerror = function (evt) {
console.log("IndexedDB error: " + evt.target.errorCode);
};
request.onupgradeneeded = function (evt) {
var objectStore = evt.currentTarget.result.createObjectStore(
"item", { keyPath: "id", autoIncrement: true });
objectStore.createIndex("name", "name", { unique: false });
for (i in itemData) {
objectStore.add(itemData[i]);
}
};
}
function contentLoaded() {
initDb();
var btnAdd = document.getElementById("btnAdd");
var btnDelete = document.getElementById("btnDelete");
var btnPrint = document.getElementById("btnPrint");
var reinitialize_db = document.getElementById("reinitialize_db");
var btnSearch = document.getElementById("btnSearch");
btnAdd.addEventListener("click", function () {
var name = $("#name").val();
var price = $("#price").val();
// var transaction = db.transaction("item", IDBTransaction.READ_WRITE);
var transaction = db.transaction(["item"], "readwrite")
var objectStore = transaction.objectStore("item");
var request = objectStore.add({ name: name, price: price });
request.onsuccess = function (evt) {
// do something after the add succeeded
};
}, false);
btnSearch.addEventListener("click", function () {
var searchString = $("#srchString").val();
var transaction = db.transaction(["item"], "readonly");
var store = transaction.objectStore("item");
var index = store.index("name");
var request = index.get(searchString);
var search_products_tbody = $("#search_products_tbody");
search_products_tbody.html('');
request.onsuccess = function() {
var record = request.result;
if (record !== undefined) {
search_products_tbody.append("<tr>"
+ getCell(record.name)
+ getCell(record.price)
+ "</tr>");
} else {
// No match was found.
console.log("no match found");
}
};
request.onerror = function (ev) {
console.log("index.get failed. Error: " + ev.message);
}
}, false);
reinitialize_db.addEventListener("click", function () {
var transaction = db.transaction(["item"], "readwrite");//db.transaction("item", IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore("item");
objectStore.clear().onsuccess = function(event) {
console.log("Database cleared");
};
transaction = db.transaction(["item"], "readwrite");//db.transaction("item", IDBTransaction.READ_WRITE);
objectStore = transaction.objectStore("item");
for (i in itemData) {
objectStore.add(itemData[i]);
}
}, false);
// btnDelete.addEventListener("click", function () {
// var id = document.getElementById("txtID").value;
// var transaction = db.transaction(["item"], "readwrite");//db.transaction("item", IDBTransaction.READ_WRITE);
// var objectStore = transaction.objectStore("item");
// var request = objectStore.delete(id);
// request.onsuccess = function(evt) {
// // It's gone!
// };
// }, false);
btnPrint.addEventListener("click", function () {
var products_tbody = $("#products_tbody");
products_tbody.html('');
var transaction = db.transaction(["item"], "readwrite")//= db.transaction("item", IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore("item");
var request = objectStore.openCursor();
request.onsuccess = function(evt) {
var cursor = evt.target.result;
if (cursor) {
products_tbody.append("<tr>"
// + getCell(cursor.key)
+ getCell(cursor.value.name)
+ getCell(cursor.value.price)
+ "</tr>");
// products_tbody.append("<tr><td>"+cursor.key+"</td><td>"+cursor.value.name+"</td></tr>");
cursor.continue();
}
else {
console.log("No more entries!");
}
};
}, false);
}
window.addEventListener("DOMContentLoaded", contentLoaded, false);
})();
function getCell(value){
return "<td>" + value + "</td>";
}