如何在表格中查看Firebase对象

时间:2018-11-28 06:13:01

标签: javascript html firebase firebase-realtime-database

我有一个医生的firebase表,我想在html表中显示它。我已经在控制台中执行了此操作,但是无法在html中显示。我尝试使用createElement(),但显示错误。我的代码在下面

var ref=database.ref('doctors');
  ref.on('value',getData, errData);

  function getData(data){
    //console.log(data.val());
    var doctors=data.val();
    var keys=Object.keys(doctors);
    console.log(keys);

    for (var i = 0; i < keys.length; i++) {
        var k=keys[i];
        var designation=doctors[k].designation;
        var doctor_email=doctors[k].doctor_email;
        var doctor_id=doctors[k].doctor_id;
        var doctor_phone=doctors[k].doctor_phone;
        var hospital_id=doctors[k].hospital_id;
        var isApproved=doctors[k].isApproved;
        var name=doctors[k].name;
        var specialization=doctors[k].specialization;
        var token=doctors[k].token;

       console.log(designation,doctor_email,doctor_id,doctor_phone,hospital_id,isApproved,name,specialization,token)
    }
  }

1 个答案:

答案 0 :(得分:1)

例如,只需添加一个ID为div_output的div,然后使用它来构建表,如下所示:

var output = document.getElementById("div_output");
output.innerHTML = "<table>";    // clearing any previous html content
                                 // and setting up header row
output.innerHTML += "<tr>"       
                            + "<th> Designation </th>"
                            + "<th> E-Mail </th>"
                            + "<th> ID </th>"
                            + "<th> Phone </th>"
                            + "<th> Hospital ID </th>"
                            + "<th> Approved? </th>"
                            + "<th> Name </th>"
                            + "<th> Specialization </th>"
                            + "<th> Token </th>"
                          + "</tr>";

for (var i = 0; i < keys.length; i++) {
    var k=keys[i];
    var designation=doctors[k].designation;
    var doctor_email=doctors[k].doctor_email;
    var doctor_id=doctors[k].doctor_id;
    var doctor_phone=doctors[k].doctor_phone;
    var hospital_id=doctors[k].hospital_id;
    var isApproved=doctors[k].isApproved;
    var name=doctors[k].name;
    var specialization=doctors[k].specialization;
    var token=doctors[k].token;
                                     // appending doctor's row of data
    output.innerHTML += "<tr>"
                        + "<td>" + designation + "</td>"
                        + "<td>" + doctor_email+ "</td>"
                        + "<td>" + doctor_id+ "</td>"
                        + "<td>" + doctor_phone+ "</td>"
                        + "<td>" + hospital_id+ "</td>"
                        + "<td>" + isApproved + "</td>"
                        + "<td>" + name + "</td>"
                        + "<td>" + specialization + "</td>"
                        + "<td>" + token + "</td>"
                      + "</tr>";

}

output.innerHTML += "</table>";  //closing the table