2017-07-11 8 views
0

Im Folgenden Code zeigen Daten aus Feuerbasis in TabelleRealtime-Update zwischen Firebase und HTML-Tabelle

<html> 
 
    <head> 
 
    </head> 
 
    <body > 
 
    
 
    <table style="width:100%;width:100%;" id="ex-table"> 
 
     <tr id="tr"> 
 
     <th>Brand</th> 
 
     <th>Description</th> 
 
     <th>Item Code</th> 
 
     <th>Product Category</th> 
 
    </table> 
 
    
 
    
 
    
 
    
 
    
 
    <script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
    <script> 
 
     // Initialize Firebase 
 
     var config = { 
 
     apiKey: "AIzaSyB5aqeEmEIB56IvRt4DL8RyOr0JX5Mw3HQ", 
 
     authDomain: "tp-firebase-fd827.firebaseapp.com", 
 
     databaseURL: "https://tp-firebase-fd827.firebaseio.com", 
 
     projectId: "tp-firebase-fd827", 
 
     storageBucket: "tp-firebase-fd827.appspot.com", 
 
     messagingSenderId: "191127646247" 
 
     }; 
 
     firebase.initializeApp(config); 
 
     
 
     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.brand + '</td>'; 
 
    \t \t \t \t content += '<td>' + val.description + '</td>'; 
 
    \t \t \t \t content += '<td>' + val.item_code + '</td>'; 
 
    \t \t \t \t content += '<td>' + val.prod_cat + '</td>'; 
 
        content += '</tr>'; 
 
       }); 
 
       $('#ex-table').append(content); 
 
      } 
 
     }); 
 
     
 
     
 
    </script> 
 
     
 
     
 
     
 
     
 
    
 
    
 
    </body> 
 
    </html>

jedoch automatisch ändern tut, wenn ich Daten in meiner Feuerbasis ändern. Wie kann ich ein Update in Echtzeit erreichen? Das ist das einzige, was ich tun muss. TYSM für zukünftige Hilfe

Antwort

0

Sie haben die "einmal" -Methode verwendet, die einmal auf Änderungen wartet und dann stoppt. Sie möchten die "on" -Methode verwenden, die ständig auf Änderungen wartet. zum Beispiel statt:

database.ref().once('value', function(snapshot){

Verwendung:

database.ref().on('value', function(snapshot){

relevante Dokumentation: https://firebase.google.com/docs/database/web/read-and-write#listen_for_value_events

+0

thnx für die Hilfe :) –

Verwandte Themen