【问题标题】:Displaying data from firestore into HTML table将 firestore 中的数据显示到 HTML 表中
【发布时间】:2019-10-06 16:20:40
【问题描述】:

我需要将我的练习集的数据显示到 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>

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

更新:

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

作者:document.getElementById("ename").innerText = EName;

现在只显示一条记录,如何显示全部?

【问题讨论】:

  • 只有像&lt;input&gt;这样的表单控件有value属性

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


【解决方案1】:

我想你会想尝试使用元素上的 innerText 属性来做更多类似的事情:

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

【讨论】:

  • 嘿,这有帮助,但我使用的是:document.getElementById("ename").innerHTML = EName;从 innerText 属性链接。但它只显示一个数据。
  • 对于 Rae 数据,innerText 优于 innerHTML。您添加的是文本,而不是 html。
【解决方案2】:

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

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

            }

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 2018-04-25
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多