2017-07-11 16 views
0

FirebaseとHTMLテーブル間のリアルタイム更新firebaseからコードを表示データの下

<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>

。どのように私はリアルタイム更新を達成することができますか?それだけのことは私がする必要があります。 TYSM for future help

答えて

0

変更を一度聞いてから停止する "once"メソッドを使用しました。変更を待ち受けている "on"メソッドを使いたいとします。例えば、代わりの:

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

使用:

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

関連文書:助けをhttps://firebase.google.com/docs/database/web/read-and-write#listen_for_value_events

+0

のthnx :) –

関連する問題