Reputation: 49
I need to display the data of my exercises collection into an HTML table. I thought of getting it done by using getElementByID but it is displaying only 1 row. What is the proper way of doing this? Thank You.
Please see codes below:
HTML code
<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:
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;
By: document.getElementById("ename").innerText = EName;
It is now displaying one record only, how can I display all of them?
Upvotes: 1
Views: 5037
Reputation: 1
You can append text in dom element like this:
document.getElementById("ename").value += EName
Upvotes: 0
Reputation: 1
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
<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
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);
}
Upvotes: 0
Reputation: 317497
I think you'll want to try something more like this using the innerText property on the element:
document.getElementById("ename").innerText = EName;
Upvotes: 1