Javascript - Displaying table data using boolean to either append or create new table












0















I've written code to fill in my table with data from a database. Currently, I'm using a boolean variable that will set to false after all of the corresponding students have been displayed in the table. That way, on the next querying of the database, the table will begin anew.



But my code seems to always hit my else and triggers the .html() option, overwriting every student except the last one.



To confirm that it wasn't an issue with the data, I logged to the console each student matching the query; indeed, all the data is there, but I want to append until the query is complete. Then, on a new query, I want to clear my table for the next set of data.



The function of the following code is for querying with a "last name":



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









share|improve this question





























    0















    I've written code to fill in my table with data from a database. Currently, I'm using a boolean variable that will set to false after all of the corresponding students have been displayed in the table. That way, on the next querying of the database, the table will begin anew.



    But my code seems to always hit my else and triggers the .html() option, overwriting every student except the last one.



    To confirm that it wasn't an issue with the data, I logged to the console each student matching the query; indeed, all the data is there, but I want to append until the query is complete. Then, on a new query, I want to clear my table for the next set of data.



    The function of the following code is for querying with a "last name":



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









    share|improve this question



























      0












      0








      0








      I've written code to fill in my table with data from a database. Currently, I'm using a boolean variable that will set to false after all of the corresponding students have been displayed in the table. That way, on the next querying of the database, the table will begin anew.



      But my code seems to always hit my else and triggers the .html() option, overwriting every student except the last one.



      To confirm that it wasn't an issue with the data, I logged to the console each student matching the query; indeed, all the data is there, but I want to append until the query is complete. Then, on a new query, I want to clear my table for the next set of data.



      The function of the following code is for querying with a "last name":



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









      share|improve this question
















      I've written code to fill in my table with data from a database. Currently, I'm using a boolean variable that will set to false after all of the corresponding students have been displayed in the table. That way, on the next querying of the database, the table will begin anew.



      But my code seems to always hit my else and triggers the .html() option, overwriting every student except the last one.



      To confirm that it wasn't an issue with the data, I logged to the console each student matching the query; indeed, all the data is there, but I want to append until the query is complete. Then, on a new query, I want to clear my table for the next set of data.



      The function of the following code is for querying with a "last name":



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






      javascript html firebase firebase-realtime-database






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 5:24









      Frank van Puffelen

      237k29382408




      237k29382408










      asked Nov 25 '18 at 4:48









      gbm0102gbm0102

      395




      395
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Your if-then-else looks like this:



          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


          You need to understand that the child_added call back function will run for each child it found from the database, and after all of the children have been iterated, then the function execution ends, and then you hit the line:



          firstTime = false;


          So, to fix your case, you need to move this line to the true part of your if statement, inside the callback function



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





          share|improve this answer
























          • Your logic definitely makes sense; the code, however, still seems to only add the last student into the table.

            – gbm0102
            Nov 25 '18 at 5:45











          • In that case, change child_added event to value to force getting all existing children. Because child_added will only be triggered for newly added children, and will disregard pre-existing records

            – Ahmad
            Nov 25 '18 at 5:49











          • I tried that, but my table only populated with undefined values.

            – gbm0102
            Nov 25 '18 at 5:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464729%2fjavascript-displaying-table-data-using-boolean-to-either-append-or-create-new%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Your if-then-else looks like this:



          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


          You need to understand that the child_added call back function will run for each child it found from the database, and after all of the children have been iterated, then the function execution ends, and then you hit the line:



          firstTime = false;


          So, to fix your case, you need to move this line to the true part of your if statement, inside the callback function



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





          share|improve this answer
























          • Your logic definitely makes sense; the code, however, still seems to only add the last student into the table.

            – gbm0102
            Nov 25 '18 at 5:45











          • In that case, change child_added event to value to force getting all existing children. Because child_added will only be triggered for newly added children, and will disregard pre-existing records

            – Ahmad
            Nov 25 '18 at 5:49











          • I tried that, but my table only populated with undefined values.

            – gbm0102
            Nov 25 '18 at 5:54
















          1














          Your if-then-else looks like this:



          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


          You need to understand that the child_added call back function will run for each child it found from the database, and after all of the children have been iterated, then the function execution ends, and then you hit the line:



          firstTime = false;


          So, to fix your case, you need to move this line to the true part of your if statement, inside the callback function



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





          share|improve this answer
























          • Your logic definitely makes sense; the code, however, still seems to only add the last student into the table.

            – gbm0102
            Nov 25 '18 at 5:45











          • In that case, change child_added event to value to force getting all existing children. Because child_added will only be triggered for newly added children, and will disregard pre-existing records

            – Ahmad
            Nov 25 '18 at 5:49











          • I tried that, but my table only populated with undefined values.

            – gbm0102
            Nov 25 '18 at 5:54














          1












          1








          1







          Your if-then-else looks like this:



          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


          You need to understand that the child_added call back function will run for each child it found from the database, and after all of the children have been iterated, then the function execution ends, and then you hit the line:



          firstTime = false;


          So, to fix your case, you need to move this line to the true part of your if statement, inside the callback function



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





          share|improve this answer













          Your if-then-else looks like this:



          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


          You need to understand that the child_added call back function will run for each child it found from the database, and after all of the children have been iterated, then the function execution ends, and then you hit the line:



          firstTime = false;


          So, to fix your case, you need to move this line to the true part of your if statement, inside the callback function



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 5:14









          AhmadAhmad

          8,27543663




          8,27543663













          • Your logic definitely makes sense; the code, however, still seems to only add the last student into the table.

            – gbm0102
            Nov 25 '18 at 5:45











          • In that case, change child_added event to value to force getting all existing children. Because child_added will only be triggered for newly added children, and will disregard pre-existing records

            – Ahmad
            Nov 25 '18 at 5:49











          • I tried that, but my table only populated with undefined values.

            – gbm0102
            Nov 25 '18 at 5:54



















          • Your logic definitely makes sense; the code, however, still seems to only add the last student into the table.

            – gbm0102
            Nov 25 '18 at 5:45











          • In that case, change child_added event to value to force getting all existing children. Because child_added will only be triggered for newly added children, and will disregard pre-existing records

            – Ahmad
            Nov 25 '18 at 5:49











          • I tried that, but my table only populated with undefined values.

            – gbm0102
            Nov 25 '18 at 5:54

















          Your logic definitely makes sense; the code, however, still seems to only add the last student into the table.

          – gbm0102
          Nov 25 '18 at 5:45





          Your logic definitely makes sense; the code, however, still seems to only add the last student into the table.

          – gbm0102
          Nov 25 '18 at 5:45













          In that case, change child_added event to value to force getting all existing children. Because child_added will only be triggered for newly added children, and will disregard pre-existing records

          – Ahmad
          Nov 25 '18 at 5:49





          In that case, change child_added event to value to force getting all existing children. Because child_added will only be triggered for newly added children, and will disregard pre-existing records

          – Ahmad
          Nov 25 '18 at 5:49













          I tried that, but my table only populated with undefined values.

          – gbm0102
          Nov 25 '18 at 5:54





          I tried that, but my table only populated with undefined values.

          – gbm0102
          Nov 25 '18 at 5:54




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464729%2fjavascript-displaying-table-data-using-boolean-to-either-append-or-create-new%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'