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
| $(document).ready(main);
//SENTENCIAS SQL PARA LA APLICACIÓN
var sqlTable = "CREATE TABLE IF NOT EXISTS notas (id INTEGER PRIMARY KEY AUTOINCREMENT,fecha VARCHAR(50),texto TEXT)";
var sqlInsert = "INSERT INTO notas (fecha,texto) VALUES (?,?)";
var sqlSelect = "SELECT * FROM notas";
var sqlDelete = "DELETE FROM notas WHERE id=?";
//1º. Crear y abrir el fichero que representa a la BD
var db = openDatabase("BlocDeNotas", "1.0", "Base de datos principal", 20000);
//FUNCIÓN PRINCIPAL==========================
function main() {
//2º Llamar a la función para crear las tablas
createTable();
//3º Mostrar las notas de la tabla
showNotes();
//4º Eliminar el retraso de 300ms al hacer click
FastClick.attach(document.body);
}
//FUNCIÓN SQL==========================
function createTable() {
db.transaction(function (tx) {
tx.executeSql(sqlTable);
});
}
function saveNota() {
//1º Obtener la fecha del sistema
var d = new Date();
var fecha = d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear();
//2º Obtener el valor del campo de texto
var texto = $(".formulario textarea").val();
//3º Contacto con la db
db.transaction(function (tx) {
tx.executeSql(sqlInsert, [fecha, texto]);
});
//4º Vaciar el campo de texto
$(".formulario textarea").val("");
//5º Cerrar la ventana
showNotes();
closeAlert();
}
function showNotes() {
//1º LIMPIAR LISTA
$("main ul").html("");
//2º EJECUTAR SENTENCIA EN LA BASE DE DATOS
db.transaction(function (tx) {
tx.executeSql(sqlSelect, [], function (tx, results) {
//podemos obtner el número de filas
var numOfRows = results.rows;
console.log("Numero de filas: " + numOfRows.length);
//recorremos los resultados:
for (var i = 0; i < numOfRows.length; i++) {
var nota = numOfRows.item(i);
//PINTAR LAS NOTAS EN LA LISTA, ES DECIR.... CREAR LOS LI
$("main ul").prepend('<li><h2>' + nota["fecha"] + '</h2><p>' + nota["texto"] + '</p><div class="btn-borrar" onclick="deleteNote(' + nota["id"] + ')"></div></li>');
}
});
});
}
function deleteNote(id) {
navigator.notification.confirm("Supprimer", function (indexButton) {
if (indexButton == "1") {
db.transaction(function (tx) {
tx.executeSql(sqlDelete, [id], showNotes());
});
}
}, "Confirmer", ["oui", "non"]);
}
//FUNCIÓN OTROS==========================
function showAlert() {
$(".alert").fadeIn(500);
}
function closeAlert() {
$(".alert").fadeOut(500);
} |
Partager