[英]Displaying data from firestore into HTML table
I need to display the data of my exercises collection into an HTML table.我需要将练习集的数据显示到 HTML 表中。 I thought of getting it done by using getElementByID but it is displaying only 1 row.
我想通过使用 getElementByID 来完成它,但它只显示 1 行。 What is the proper way of doing this?
这样做的正确方法是什么? Thank You.
谢谢你。
Please see codes below:请看下面的代码:
HTML code HTML代码
<div class="container">
<h2>Manage Exercises:</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Exercise Name</th>
<th>Body Part</th>
<th>Exercise Type</th>
<th>Sets + Reps/Duration</th>
<th>Image</th>
</tr>
</thead>
<tbody>
<tr id="tr">
<td id="ename"></td>
<td id="body_part"></td>
<td id="etype"></td>
<td id="esets"></td>
<td id="eimage"></td>
</tr>
</tbody>
</table>
Javscript code: Javascript代码:
console.log("Initialisation Successful!");
var db = firebase.firestore();
var exRef = db.collection('Exercises');
var allex = exRef
.get()
.then(snapshot => {
snapshot.forEach(doc => {
var EName = doc.data().Name;
var Type = doc.data().Type;
var BodyPart = doc.data(). BodyPart;
var Sets = doc.data().Sets;
const Image = doc.data().Image;
document.getElementById("ename").value = EName;
});
})
.catch(err => {
console.log('Error getting documents', err);
});
Update:更新:
Replaced: document.getElementById("ename").value = EName;
替换:
document.getElementById("ename").value = EName;
By: document.getElementById("ename").innerText = EName;
作者:
document.getElementById("ename").innerText = EName;
It is now displaying one record only, how can I display all of them?现在只显示一条记录,如何显示所有记录?
I have tried these code and it works for me.我已经尝试过这些代码,它对我有用。 I did not code based on your case, but your case is similar to mine.
我没有根据您的情况进行编码,但您的情况与我的情况相似。 I hope it helps!
我希望它有帮助!
HTML Code HTML 代码
<h3>ACTIVE USER</h3>
<table id="tbl_account_list" border="2" cellpadding="10" style="border-collapse:collapse;">
<thead>
<th>EMAIL</th>
<th>FULL_NAME</th>
<th>UNI_ID</th>
</thead>
</table>
JavaScript Code JavaScript 代码
firestore.collection('account').get().then((snapshot) => {
snapshot.docs.forEach(doc => {
renderAccount(doc);
})
})
const accountList = document.querySelector('#tbl_account_list') ;
function renderAccount(doc){
let tr = document.createElement('tr');
let td_email = document.createElement('td');
let td_full_name = document.createElement('td');
let td_uni_id = document.createElement('td');
tr.setAttribute('data-id', doc.id);
td_email.textContent = doc.data().email;
td_full_name.textContent = doc.data().full_name;
td_uni_id.textContent = doc.data().uni_id;
tr.appendChild(td_email);
tr.appendChild(td_full_name);
tr.appendChild(td_uni_id);
accountList.appendChild(tr);
}
You can append text in dom element like this:您可以在 dom 元素中使用 append 文本,如下所示:
document.getElementById("ename").value += EName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.