2017-02-18 86 views
0

所以我正在一个web应用程序。基本上,我已经获得了以下代码,但是如何删除单击删除按钮时提取的每个员工?我还可以如何在删除小孩时更新列表。例如,现在如果我添加一个孩子,表格会自动更新新的孩子而不刷新页面。检索多个firebase对象

var rootRef = firebase.database().ref().child("Balkar/Employees"); 
rootRef.on('child_added', snap => { 
    var id = snap.child("ID").val(); 
    var name = snap.child("Name").val(); 
    var email = snap.child("Email").val(); 

    $("#table_body").append("<tr><td>" + id + "</td><td>" + name + "</td> <td>" + email + 
          "</td><td><button>Remove</button></td></tr>"); 
}); 

<h1>All Employees</h1> 
    <table> 
     <tr> 
      <th>Employee ID</th> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Remove</th> 
     </tr> 
     <tbody id="table_body"></tbody> 
    </table> 

编辑:

var rootRef = firebase.database().ref().child("Balkar/Employees"); 

rootRef.on('child_added', snap => { 
    var id = snap.child("ID").val(); 
    var key = snap.key; 
    var name = snap.child("Name").val(); 
    var email = snap.child("Email").val(); 
    var btn = "<button id='removeEmployee' class='mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent'>Remove</button>"; 

    $("#table_body").append("<tr id='"+key+"'><td>" + id + "</td><td>" + name + "</td> <td>" + email + 
          "</td><td><button>" + btn + "</button></td></tr>"); 

    $("#removeEmployee").click(
    function(){ 
     rootRef.on('child_removed', snap => { 
     var key = snap.key; 
     $('#'+key).remove(); 
     }); 
    } 
); 

}); 

回答

0

要当一个项目被删除更新列表,侦听child_removed活动,并通过查找该项目的关键

rootRef.on('child_added', snap => { 
    var id = snap.child("ID").val(); 
    var key = snap.key; 
    var name = snap.child("Name").val(); 
    var email = snap.child("Email").val(); 

    $("#table_body").append("<tr id='"+key+"'><td>" + id + "</td><td>" + name + "</td> <td>" + email + 
          "</td><td><button>Remove</button></td></tr>"); 
    }); 
}); 
rootRef.on('child_removed', snap => { 
    var key = snap.key; 
    $('#'+key).remove(); 
}); 
+0

我更新了上面的代码下的编辑,但是当我点击删除按钮 –

+0

我只提供代码的事情火力地堡边没有任何反应。对于处理按钮点击,你应该能够得到这些地方:http://stackoverflow.com/search?q=%5Bjquery%5D+button+click+handler –

+0

,但我认为点击事件监听器在确定其东西用我的firebase代码,不? –

0

首先,您正在创建具有相同ID的多个删除按钮。情况并非如此。其次,您需要将密钥与每个按钮相关联,以便当用户点击其中一个按钮时,您知道要删除哪个项目。以下代码应该有所帮助

var rootRef = firebase.database().ref().child("Balkar/Employees"); 

rootRef.on('child_added', snap => { 
    var id = snap.child("ID").val(); 
    var key = snap.key; 
    var name = snap.child("Name").val(); 
    var email = snap.child("Email").val(); 
    var btn = "<button key='"+ key +"' class='removeEmployee mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent'>Remove</button>"; 

    $("#table_body").append("<tr id='"+key+"'><td>" + id + "</td><td>" + name + "</td> <td>" + email + 
          "</td><td>" + btn + "</td></tr>"); 

}); 

// now this click handler can be (should be) moved outside 'child_added' callback 
    $('#table_body').on('click', ".removeEmployee", function(){ // note: using 'removeElement' as class, not as id 
    var key = $(this).attr('key'); 
    var itemToRemove = rootRef.child(key); 
    itemToRemove.remove() 
    .then(function() { // removed from Firebase DB 
    console.log("Remove succeeded.") 
    }) 
    .catch(function(error) { 
    console.log("Remove failed: " + error.message) 
    }); 

    }); 
// keeping this separate so that even if the item is removed by any other means (e.g.- directly from server console) this UI will remain in sync 
rootRef.on('child_removed', function(snap) { 
    var key = snap.key; 
    $('#'+key).remove(); 
}); 

裁判:https://firebase.google.com/docs/reference/js/firebase.database.Reference#remove

+0

它看起来像删除部分不工作。因为它甚至不再提取任何员工,但是当我注释掉它的删除部分时,它就起作用了。 –

+0

有一个错字。它是'itemToRemove.remove().. then'。删除多余的点,然后再试一次。 –

+0

uhm它现在加载数据,但它不会在单击按钮时删除对象。我添加了一个提醒,以查看按钮点击后该功能是否有效。它不是 –