· 8 years ago · Feb 13, 2018, 02:40 PM
1// Declare SQL Query for SQLite
2
3var createStatement = "CREATE TABLE IF NOT EXISTS Productos (id INTEGER PRIMARY KEY AUTOINCREMENT, producto TEXT, comprado INT)";
4
5var selectAllStatement = "SELECT * FROM Productos";
6
7var selectAllComprados = "SELECT * FROM Productos where comprado=1";
8
9var selectAllNoComprados = "SELECT * FROM Productos where comprado=0";
10
11var insertStatement = "INSERT INTO Productos (producto,comprado) VALUES (?, ?)";
12
13var updateStatement = "UPDATE Productos SET producto = ?,comprado = ? WHERE id=?";
14
15var updateComprado = "UPDATE Productos SET comprado = ? WHERE id=?";
16
17var deleteStatement = "DELETE FROM Productos WHERE id=?";
18
19var dropStatement = "DROP TABLE Productos";
20
21var db = openDatabase("ListaCompra", "1.0", "Mi Lista de la Compra", 200000); // Open SQLite Database
22
23var dataset;
24
25var DataType;
26
27testigo = 0;
28comprado=0;
29function initDatabase() // Function Call When Page is ready.
30
31{
32
33 try {
34
35 if (!window.openDatabase) // Check browser is supported SQLite or not.
36
37 {
38
39 alert('Databases are not supported in this browser.');
40
41 }
42
43 else {
44
45 createTable(); // If supported then call Function for create table in SQLite
46
47 }
48
49 }
50
51 catch (e) {
52
53 if (e == 2) {
54
55 // Version number mismatch.
56
57 console.log("Invalid database version.");
58
59 } else {
60
61 console.log("Unknown error " + e + ".");
62
63 }
64
65 return;
66
67 }
68
69}
70
71function createTable() // Function for Create Table in SQLite.
72
73{
74
75 db.transaction(function (tx) { tx.executeSql(createStatement, [], showRecords, onError); });
76
77}
78
79function insertRecord() // Get value from Input and insert record . Function Call when Save/Submit Button Click..
80
81{
82
83 var productotemp = $('#nombreDelProducto').val();
84
85 if (productotemp != "") {
86 $('.submitButton').text("Añadir");
87 db.transaction(function (tx) { tx.executeSql(insertStatement, [productotemp, comprado], loadAndReset, onError); });
88 $('#nombreDelProducto').val("");
89 }
90
91
92}
93
94function deleteRecord(id) // Get id of record . Function Call when Delete Button Click..
95
96{
97 alert("entra")
98 var iddelete = id.toString();
99
100 db.transaction(function (tx) { tx.executeSql(deleteStatement, [id], showRecords, onError);});
101
102 resetForm();
103
104
105}
106
107function updateRecord() // Get id of record . Function Call when Delete Button Click..
108
109{
110 var productoupdate = $('#nombreDelProducto').val().toString();
111 var useridupdate = $("#id").val();
112 db.transaction(function (tx) { tx.executeSql(updateStatement, [productoupdate, comprado, Number(useridupdate)], loadAndReset, onError); });
113 $("#submitButton").css("display","block");
114 $("#btnUpdate").css("display","none");
115 $("#nombreDelProducto").val("");
116}
117
118
119
120function dropTable() // Function Call when Drop Button Click.. Talbe will be dropped from database.
121
122{
123
124
125 db.transaction(function (tx) { tx.executeSql(dropStatement, [], showRecords, onError); });
126
127 resetForm();
128
129 initDatabase();
130
131}
132
133function loadRecord(i) // Function for display records which are retrived from database.
134
135{
136 $("#submitButton").css("display","none");
137 $("#btnUpdate").css("display","block");
138
139 var item = dataset.item(i);
140
141 $("#nombreDelProducto").val((item['producto']).toString());
142
143 $("#id").val((item['id']).toString());
144
145
146
147}
148
149function resetForm() // Function for reset form input values.
150
151{
152
153 $("#producto").val("");
154
155 $("#id").val("");
156
157}
158
159function loadAndReset() //Function for Load and Reset...
160
161{
162 resetForm();
163
164 showRecords();
165
166}
167
168function onError(tx, error) // Function for Hendeling Error...
169
170{
171
172 alert(error.message);
173
174}
175
176function Comprado(idProucto,comprado) {
177
178 if(comprado==0){
179
180 var compradoupdate = 1;
181
182 var useridupdate = idProucto;
183
184
185 db.transaction(function (tx) { tx.executeSql(updateComprado, [compradoupdate, Number(useridupdate)], loadAndReset, onError); });
186 }else{
187 var compradoupdate = 0;
188
189 var useridupdate = idProucto;
190
191 db.transaction(function (tx) { tx.executeSql(updateComprado, [compradoupdate, Number(useridupdate)], loadAndReset, onError); });
192 }
193
194}
195
196function showRecords() // Function For Retrive data from Database Display records as list
197{
198 $("#resultado").html('');
199
200 db.transaction(function (tx) {
201
202 tx.executeSql(selectAllStatement, [], function (tx, result) {
203
204 dataset = result.rows;
205 for (var i = 0, item = null; i < dataset.length; i++) {
206 item = dataset.item(i);
207 if(item['comprado']==1){
208 listItem =
209 '<div>' +
210 '<label style="float:left; width:83%;">' +
211 '<input style="background-color:green; text-align: right;" onclick="Comprado('+item['id'] +','+ item['comprado'] +')" id='+"a"+ item['id'] + ' value="'+ item['producto'] +'" type="button" >' +
212 '</label>' +
213 '<a style="float:left;margin-top:15px" data-role="button" data-role="button" data-icon="edit" data-iconpos="notext" href="#" onclick="loadRecord(' + i + ')"></a>'+
214 '<a style="float:left;margin-top:15px" data-role="button" data-role="button" data-icon="delete" data-iconpos="notext" href="#" onclick="deleteRecord(' + item['id'] + ')"></a>'+
215 '</div>';
216
217 $('#resultado').append(listItem);
218 $('#resultado').listview('refresh').trigger('create');
219 }else{
220 listItem =
221 '<div>' +
222 '<label style="float:left; width:83%;">' +
223 '<input style="background-color:red; text-align: right;" onclick="Comprado('+item['id'] +','+ item['comprado'] +')" id='+"a"+ item['id'] + ' value="'+ item['producto'] +'" type="button" >' +
224 '</label>' +
225 '<a style="float:left;margin-top:15px" data-role="button" data-role="button" data-icon="edit" data-iconpos="notext" href="#" onclick="loadRecord(' + i + ')"></a>'+
226 '<a style="float:left;margin-top:15px" data-role="button" data-role="button" data-icon="delete" data-iconpos="notext" href="#" onclick="deleteRecord(' + item['id'] + ')"></a>'+
227 '</div>';
228
229 $('#resultado').append(listItem);
230 $('#resultado').listview('refresh').trigger('create');
231 }
232
233
234 }
235
236
237 });
238 });
239}
240
241$(document).ready(function () // Call function when page is ready for load..
242
243{
244
245
246 $("body").fadeIn(2000); // Fede In Effect when Page Load..
247
248 initDatabase();
249
250 $("#submitButton").click(insertRecord); // Register Event Listener when button click.
251
252 $("#btnUpdate").click(updateRecord);
253
254 $("#btnReset").click(resetForm);
255
256 $("#btnDrop").click(dropTable);
257
258});