-
Notifications
You must be signed in to change notification settings - Fork 1
/
main2.js
84 lines (65 loc) · 2.26 KB
/
main2.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
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.6.0/firebase-app.js";
import { getDatabase, ref, get, child, onValue } from "https://www.gstatic.com/firebasejs/10.6.0/firebase-database.js";
const firebaseConfig = {
apiKey: "AIzaSyD6nDqwgjdIjbdlq9EvPnRTr7tAwyYaSgE",
authDomain: "codejam-4934c.firebaseapp.com",
databaseURL: "https://codejam-4934c-default-rtdb.firebaseio.com",
projectId: "codejam-4934c",
storageBucket: "codejam-4934c.appspot.com",
messagingSenderId: "1030276208419",
appId: "1:1030276208419:web:bd48475b7a045271bd5e96",
measurementId: "G-HS4QR21Y3P"
};
const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
//filling the table
var rating = 0;
var tbody = document.getElementById("user-list");
function AddItemToTable(username, item, photo, swap_info) {
let trow = document.createElement("tr");
let td1 = document.createElement("td");
let td2 = document.createElement("td");
let td3 = document.createElement("td");
let td4 = document.createElement("td");
let td5 = document.createElement("td");
td1.innerHTML= ++rating;
td2.innerHTML= username;
td3.innerHTML= item;
td4.innerHTML= photo;
td5.innerHTML= swap_info;
trow.appendChild(td1);
trow.appendChild(td2);
trow.appendChild(td3);
trow.appendChild(td4);
trow.appendChild(td5);
tbody.appendChild(trow);
}
function AddAllItemsToTable(TheUser) {
rating=0;
tbody.innerHTML="";
TheUser.forEach(element => {
AddItemToTable(element.username, element.item, element.photo, element.swap_info);
});
}
// async function GetAllDataOnce() {
// const dbRef = ref(db, "users");
// await get(child(dbRef, "users"))
// .then((snapshot) => {
// var allUsers = [];
// snapshot.forEach(childSnapshot =>{
// students.push(childSnapshot.val());
// });
// AddAllItemsToTable(allUsers);
// });
// }
function GetAllDataRealtime() {
const dbRef = ref(db, 'users');
onValue(dbRef, (snapshot)=> {
var allUsers = [];
snapshot.forEach(childSnapshot => {
allUsers.push(childSnapshot.val());
});
AddAllItemsToTable(allUsers);
});
}
window.onload = GetAllDataRealtime();