Javascript-使用布尔值显示表数据以追加或创建新表

时间:2018-11-25 04:48:35

标签: javascript html firebase firebase-realtime-database

我编写了代码,用数据库中的数据填充表中。当前,我正在使用布尔变量,该变量将在表中显示所有对应的学生之后将其设置为false。这样,在下一次查询数据库时,该表将重新开始。

但是我的代码似乎总是命中我的else并触发.html()选项,覆盖了除最后一个学生以外的所有学生。

为确认数据不是问题,我将匹配查询的每个学生登录到控制台;确实,所有数据都在那里,但是我想追加直到查询完成。然后,在一个新查询中,我想清除我的表以获取下一组数据。

以下代码的功能用于查询“姓氏”:

var firstTime = true;
$('#querylastName').click(function(){
    var lastNameSelected = $('#userlName').val();
    var ref = firebase.database().ref("students");

    ref.orderByChild('lastName').equalTo(lastNameSelected).on("child_added", function(snapshot){
        console.log(snapshot.val());

        // Variables to hold retrieved data from the database.
        var fname_val = snapshot.val().firstName;
        var lname_val = snapshot.val().lastName;
        var hometown_val = snapshot.val().hometown;
        var ethnicity_val = snapshot.val().race;
        var gender_val = snapshot.val().gender;
        var program_val = snapshot.val().program;
        var school_val = snapshot.val().school;
        var concentration_val = snapshot.val().concentration;
        var gradYear_val = snapshot.val().gradDate;
        var timeToComplete_val = snapshot.val().timeToComplete;
        var afterGrad_val = snapshot.val().afterGrad;
        var explain_val = snapshot.val().elaborate;

        // Append read data into the table.
        if(firstTime){
            $("#table_body").append("<tr><td>" + fname_val + "</td><td>" + lname_val + "</td><td>" + hometown_val + "</td><td>" + ethnicity_val + "</td><td>" + gender_val + "</td><td>" + program_val + "</td><td>" + school_val + "</td><td>" + concentration_val + "</td><td>" + gradYear_val + "</td><td>" + timeToComplete_val + "</td><td>" + afterGrad_val + "</td><td>" + explain_val + "</td></tr>");
        }
        else{
            $("#table_body").html("<tr><td>" + fname_val + "</td><td>" + lname_val + "</td><td>" + hometown_val + "</td><td>" + ethnicity_val + "</td><td>" + gender_val + "</td><td>" + program_val + "</td><td>" + school_val + "</td><td>" + concentration_val + "</td><td>" + gradYear_val + "</td><td>" + timeToComplete_val + "</td><td>" + afterGrad_val + "</td><td>" + explain_val + "</td></tr>");
        }
    })
    firstTime = false;
})

1 个答案:

答案 0 :(得分:1)

您的if-then-else如下所示:

ref.orderByChild('lastName').equalTo(lastNameSelected).on("child_added", function(snapshot){
    //
    // irrelevant code reduced
    // 
    //
    if(firstTime){
        $("#table_body").append(... etc ...);
    }
    else{
         $("#table_body").html(... etc ...);
    }
});
firstTime = false;   // this will only be called after iterating through all children

您需要了解,child_added回调函数将针对从数据库中找到的每个子代运行,并且在所有子代都被迭代之后,函数执行结束,然后您开始执行:

firstTime = false;

因此,要解决这种情况,您需要将此行移到true语句的if部分中,并在回调函数中

ref.orderByChild('lastName').equalTo(lastNameSelected).on("child_added", function(snapshot){
    //
    // irrelevant code reduced
    // 
    //
    if(firstTime){
        $("#table_body").append(... etc ...);
        firstTime = false;         // <-----------   move it here
    }
    else{
         $("#table_body").html(... etc ...);
    }
});