将数据从Firestore显示到HTML表中

时间:2019-10-06 16:20:40

标签: javascript firebase html-table google-cloud-firestore

我需要将练习集的数据显示到HTML表中。 我想通过使用getElementByID完成它,但它只显示1行。正确的做法是什么?谢谢。

请参见以下代码:

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>

脚本代码:

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);
  });

更新

已替换:document.getElementById("ename").value = EName;

通过:document.getElementById("ename").innerText = EName;

现在仅显示一条记录,我如何显示所有记录?

3 个答案:

答案 0 :(得分:1)

我认为您可能想使用元素上的innerText属性来尝试类似的事情:

document.getElementById("ename").innerText = EName;

答案 1 :(得分:0)

我尝试了这些代码,它对我有用。我没有根据您的情况进行编码,但是您的情况与我的情况相似。希望对您有所帮助!

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代码

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);

            }

答案 2 :(得分:0)

您可以像这样在 dom 元素中附加文本:

document.getElementById("ename").value += EName