javascript - 如何使用 Firebase 数据在一个 html 页面中显示多个表格?
问题描述
我想在同一页面中显示多个表格。
我创建了两个表。但是firebase数据只到最后一个表,其他表是空的。如何解决这个问题?
var Sno;
// get all data
function SelectAllData(){
document.getElementById('tbody1').innerHTML="";
Sno=0;
firebase.database().ref('Picture').once('value',
function(AllRecords)
{
AllRecords.forEach(
function(CurrentRecord){
var Sname = CurrentRecord.val().Name;
var img = CurrentRecord.val().Link;
var des = CurrentRecord.val().Desc;
AddItemsToTable(Sname,img,des);
}
);
}
);
}
window.onload = SelectAllData;
// filling the table
var s_list =[];
function AddItemsToTable(Sname,img,des){
var tbody = document.getElementById('tbody1');
var trow =document.createElement('tr');
var td1 =document.createElement('td');
var td2 =document.createElement('td');
var td3 =document.createElement('td');
var td4 =document.createElement('td');
s_list.push([Sname,img,des]);
td1.innerHTML=++Sno;
td2.innerHTML= Sname;
td3.innerHTML= img;
td4.innerHTML= des;
trow.appendChild(td1);
trow.appendChild(td2);
trow.appendChild(td3);
trow.appendChild(td4);
var ControlDiv = document.createElement("div");
ControlDiv.innerHTML = '<button type="button" class="btn btn-primary my-2" data-toggle="modal" data-target="#exampleModalCenter" onclick="FillTboxes(null)">Add New Record</button>'
ControlDiv.innerHTML += '<button type="button" class="btn btn-primary my-2 ml-2" data-toggle="modal" data-target="#exampleModalCenter" onclick="FillTboxes('+Sno+')">Edit Record</button>'
trow.appendChild(ControlDiv);
tbody.appendChild(trow);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<!DOCTYPE html>
<html lang="en">
<head>
<title>Basic</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="workout.css" />
</head>
<body>
<div class="container mt-3">
<table class="table table-dark">
<div class="btnadd">
<H1>Day 1</H1>
<button onclick="window.location.href='/Workout/BasicSchedule.html'">Add Workout</button>
</div>
<thead>
<th>S_no</th>
<th>Workout Name</th>
<th>Description</th>
<th>Photo</th>
<th>Duration</th>
<th>Action</th>
</thead>
<tbody id="tbody1"></tbody>
</table>
</div>
<hr>
<div class="container mt-2">
<table class="table table-dark">
<div class="btnadd">
<H1>Day 2</H1>
<button onclick="window.location.href='/Workout/AddDays/day2.html'">Add Workout</button>
</div>
<thead>
<th>S_no</th>
<th>Workout Name</th>
<th>Description</th>
<th>Photo</th>
<th>Duration</th>
<th>Action</th>
</thead>
<tbody id="tbody2"></tbody>
</table>
</div>
<hr>
<script src="/Workout/fireConfig.js"></script>
<!-- day1 -->
<script id="MainScript">
// -----------------------------Filling the table---------------------------------------------
var sNo =0;
var tbody = document.getElementById('tbody1');
function AddItemsToTable(Photo,Name,Desc,Duration){
var trow =document.createElement('tr');
var td0 =document.createElement('td');
var td1 =document.createElement('td');
var td2 =document.createElement('td');
var td3 =document.createElement('td');
var td4 =document.createElement('td');
// planList.push([days,breakfast,lunch,dinner]);
td0.innerHTML=++sNo;
td1.innerHTML= Photo;
td2.innerHTML= Name;
td3.innerHTML= Desc;
td4.innerHTML= Duration;
trow.appendChild(td0);
trow.appendChild(td1);
trow.appendChild(td2);
trow.appendChild(td3);
trow.appendChild(td4);
tbody.appendChild(trow);
}
function AddAllItemsToTable(Schedule)
{
sNo = 0;
tbody.innerHTML="";
Schedule.forEach(element => {
AddItemsToTable(element.Name,element.Desc,element.Link, element.Duration);
})
}
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-app.js";
const firebaseConfig = {
apiKey: "AIzaSyCutH42eSiILx1boEblkK9eH7vpD3AQKb8",
authDomain: "theftinessgym.firebaseapp.com",
databaseURL: "https://theftinessgym-default-rtdb.firebaseio.com",
projectId: "theftinessgym",
storageBucket: "theftinessgym.appspot.com",
messagingSenderId: "484591601328",
appId: "1:484591601328:web:4df3212fbee7ff3d53dd75",
measurementId: "G-Z4QRKRJHKW"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
import { getDatabase, ref, child, onValue,get }
from "https://www.gstatic.com/firebasejs/9.4.0/firebase-database.js";
const db = getDatabase();
// -----------------------------Getting all data---------------------------------------------
function GetAllDataOnce(){
const dbRef = ref(db);
get(child(dbRef,"Picture"))
.then((snapshot)=> {
var schedule=[];
snapshot. forEach(childsnapshot=> {
schedule.push(childsnapshot.val());
});
AddAllItemsToTable(schedule);
});
}
function GetAllDataRealtime(){
const dbRef = ref(db,"Picture");
onValue(dbRef,(snapshot)=>{
var schedule = [];
snapshot.forEach(childsnapshot =>{
schedule.push(childsnapshot.val());
});
AddAllItemsToTable(schedule);
});
}
window.onload = GetAllDataOnce;
</script>
<script>
// -----------------------------Filling the table---------------------------------------------
var sNo2 =0;
var tbody2 = document.getElementById('tbody2');
function AddItemsToTable2(Photo2,Name2,Desc2,Duration2){
var trow =document.createElement('tr');
var td0 =document.createElement('td');
var td1 =document.createElement('td');
var td2 =document.createElement('td');
var td3 =document.createElement('td');
var td4 =document.createElement('td');
// planList.push([days,breakfast,lunch,dinner]);
td0.innerHTML=++sNo2;
td1.innerHTML= Photo2;
td2.innerHTML= Name2;
td3.innerHTML= Desc2;
td4.innerHTML= Duration2;
trow.appendChild(td0);
trow.appendChild(td1);
trow.appendChild(td2);
trow.appendChild(td3);
trow.appendChild(td4);
tbody2.appendChild(trow);
}
function AddAllItemsToTable2(Schedule2)
{
sNo2 = 0;
tbody2.innerHTML="";
Schedule2.forEach(element => {
AddItemsToTable2(element.Name,element.Desc,element.Link, element.Duration);
})
}
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-app.js";
const firebaseConfig = {
apiKey: "AIzaSyCutH42eSiILx1boEblkK9eH7vpD3AQKb8",
authDomain: "theftinessgym.firebaseapp.com",
databaseURL: "https://theftinessgym-default-rtdb.firebaseio.com",
projectId: "theftinessgym",
storageBucket: "theftinessgym.appspot.com",
messagingSenderId: "484591601328",
appId: "1:484591601328:web:4df3212fbee7ff3d53dd75",
measurementId: "G-Z4QRKRJHKW"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
import { getDatabase, ref, child, onValue,get }
from "https://www.gstatic.com/firebasejs/9.4.0/firebase-database.js";
const db2 = getDatabase();
// -----------------------------Getting all data---------------------------------------------
function GetAllDataOnce2(){
const dbRef2 = ref(db2);
get(child(dbRef2,"Day2"))
.then((snapshot)=> {
var schedule2=[];
snapshot. forEach(childsnapshot=> {
schedule2.push(childsnapshot.val());
});
AddAllItemsToTable2(schedule2);
});
}
function GetAllDataRealtime2(){
const dbRef2 = ref(db2,"Day2");
onValue(dbRef2,(snapshot)=>{
var schedule2 = [];
snapshot.forEach(childsnapshot =>{
schedule2.push(childsnapshot.val());
});
AddAllItemsToTable2(schedule2);
});
}
window.onload = GetAllDataOnce2;
</script>
</body>
</html>
解决方案
推荐阅读
- go - go中符文文字中的多个字符
- javascript - 当我设置初始值时,反应输入不会改变
- amazon-web-services - 从 docker 容器(Spring Boot 应用程序)托管的 Amazon AWS ECS 集群以编程方式发送电子邮件的最佳方式?
- varnish - Varnish 不对不经常请求的缓存进行原始调用
- php - 我得到了 API 控制器的新功能,但它在这方面不起作用
- c++ - 突然之间,我的 Qt 应用程序显示为 100% 而不是正常的 200%,我做了什么更改使其失去了该功能?
- django - 当我通过链接到启用站点的目录启用文件时出现 Nginx 问题
- node.js - 在这种情况下我们应该使用 Promise.all 吗?内容细节
- python - TimeDistributed 层,但权重不同
- php - 如何将简码插入现有的 html 元素(wordpress)?