【问题标题】:Create HTML table with Firebase Data使用 Firebase 数据创建 HTML 表
【发布时间】:2019-04-13 11:52:26
【问题描述】:

我设置了一个 firebase 数据库,我需要创建一个包含必要信息的 HTML 表。以下是我设置 Firebase 数据的方式。

{
  "Markets" : {
    "Athens" : {
      "Item" : {
        "name" : "Beef",
        "price" : 10,
        "salePrice" : 3
      }
    }
  }
}

这就是我设置 HTML 表和尝试检索数据的代码的方式。该表正在适当地创建,但我没有看到任何数据。任何帮助将不胜感激。

<table style="width:100%" id="ex-table">
  <tr id="tr">
    <th>Name:</th>
    <th>Price:</th> 
    <th>Sale Price:</th>

 </table> 

<script>
    var database = firebase.database();
    database.ref().once('value', function(snapshot){
        if(snapshot.exists()){
            var content = '';
            snapshot.forEach(function(data){
                var val = data.val();
                content +='<tr>';
                content += '<td>' + val.name + '</td>';
                content += '<td>' + val.price + '</td>';
                content += '<td>' + val.salePrice + '</td>';

                content += '</tr>';
            });
            $('#ex-table').append(content);
        }
    });
</script>

【问题讨论】:

    标签: javascript html firebase firebase-realtime-database


    【解决方案1】:

    现在您正在读取根节点。在您访问 namepricesalesPrice 属性之前,您的 JSON 中还有更多级别的数据。您需要在回调或查询中导航这些级别。后者的一个例子:

    var database = firebase.database();
    database.ref('Markets/Athens').once('value', function(snapshot){
        if(snapshot.exists()){
            var content = '';
            snapshot.forEach(function(data){
                var val = data.val();
                content +='<tr>';
                content += '<td>' + val.name + '</td>';
                content += '<td>' + val.price + '</td>';
                content += '<td>' + val.salePrice + '</td>';
    
                content += '</tr>';
            });
            $('#ex-table').append(content);
        }
    });
    

    【讨论】:

    • 这仍然没有返回任何值?它只返回我的表头文本值。
    猜你喜欢
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    相关资源
    最近更新 更多