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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| var firebaseConfig = { apiKey: "", authDomain: "", databaseURL: "", projectId: "", storageBucket: "", messagingSenderId: "", appId: "" }; // Initialize Firebase firebase.initializeApp(firebaseConfig);
var auth; var storage; var db; var dtTable;
function initFirebase() { console.log("init firebase"); auth = firebase.auth(); storage = firebase.storage(); db = firebase.firestore(); //onAuthStateChanged(this); }
function getData() { var query = db.collection("employees"); query.orderBy("createDate", "desc") .get().then(function (docData) { if (docData.size) { var arrObj = []; docData.forEach(function (data) { var obj = data.data(); obj.id = data.id; // assign id to data; arrObj.push(obj); });
console.log("data", arrObj);
dtTable.clear(); dtTable.rows.add(arrObj);
//dtTable.draw(); dtTable.order( [ 0, 'desc' ] ).draw(); } }, function (error) { console.log("error=", error); }); }
function saveData() { var id = $("#hd-id").val(); var name = $("#txt-name").val(); var email = $("#txt-email").val(); var phone = $("#txt-phone").val(); var data = { name: name, email: email, phone: phone }
if (!data.id) { addDB(data); } else { updateDB(data); } }
function deleteData() { var id = $("#hd-id").val(); deleteDB(id); }
function addDB(obj) { obj.createDate = firebase.firestore.FieldValue.serverTimestamp(); db.collection("employees").doc().set(obj) .then(function () { getData(); $('#myModal').modal('toggle'); }).catch(function (error) { console.error("Error writing document: ", error); }); }
function updateDB(obj) { obj.updateDate = firebase.firestore.FieldValue.serverTimestamp(); db.collection("employees").doc(obj.id).update(obj) .then(function () { getData(); $('#myModal').modal('toggle'); }); } function deleteDB(id) { db.collection("employees").doc(id).delete() .then(function () { getData(); $('#myModal').modal('toggle'); }); }
function clearInput() { $("#hd-id").val(''); $("#txt-name").val(''); $("#txt-email").val(''); $("#txt-phone").val(''); }
$(document).ready(function () { dtTable = $('#example').DataTable({ columns: [ { data: "createDate" }, { data: "id" }, { data: "name" }, { data: "email" }, { data: "phone" } ], columnDefs: [ { targets: [0], visible: false }, { targets: [1], visible: false } ] });
dtTable.on('click', 'tr', function () { if ($(this).hasClass('selected')) { $(this).removeClass('selected'); } else { dtTable.$('tr.selected').removeClass('selected'); $(this).addClass('selected'); } if (dtTable.row(this).data()) { var selectedData = dtTable.row(this).data(); //console.log("select=", selectedData); clearInput(); $("#hd-id").val(selectedData.id); $("#txt-name").val(selectedData.name); $("#txt-email").val(selectedData.email); $("#txt-phone").val(selectedData.phone); $('#myModal').modal('show') $("#btnDelete").show(); } });
$("#btnAdd").on('click', function () { clearInput(); $('#myModal').modal('show') $("#btnDelete").hide(); });
$("#btnSave").on('click', function () { saveData(); });
$("#btnDelete").on('click', function () { deleteData(); });
initFirebase();
getData(); });
|