简体   繁体   中英

How to get row data generated by firebase in another function

I am new to firebase. I am appending to a table using firebase.

<script>
  function loadcompanies(){
    var leadsRef = database.ref('companies');

    leadsRef.on('value', function(snapshot) {
      if (snapshot.exists()) {
        var content = '';

        snapshot.forEach(function(childSnapshot) {
          var childData = childSnapshot.val();

          content +=   '<tr class="header"  data-toggle="collapse" data-target="#accordion1" class="clickable">';
          //content +=  '<tr class="header">';
          content +=    '<td style="width: 55px; color: #42957F">'+childData.rooms+'</td>';
          content +=    '<td id="companyName" style="width: 102px; font-weight: bold">'+childData.company+'</td>';
          content +=    '<td style="width: 60px;">'+childData.code+'</td>';
          content +=    '<td><a href="#" style="font-size:18px;" data-toggle="modal" data-target="#myModal" id="addMore" title="Add More Person"><i class="fas fa-plus-circle plus"></i></a>Add Rooms';

          content +=    '</td>';
          content +=    '</tr>';

          //companyName.innerHTML += '<div>'+childData.company+'</div>';
          //document.getElementById("companyName").innerHTML = childData.company;

          //alert(childData.company);
        });

        $('#abctable').append(content);
      }
    });
  }
</script>

Now I want to use the company name that I got from the firebase and use it into another form to add rooms. I can't understand how to get the company name in another function.

Row that I get

Form to submit

You should be able to use the orderByChild method.

If you know the company name you can get the object by running a query.

var companyName = 'SomeCompanyName';

var companyRef = database.ref('companies').orderByChild('company').equalTo(companyName);

companyRef.on('value', function(snapshot) {
  var company = snapshot.val();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM