This is what I have
Firebase Database:
setores: {
-KkBgUmX6BEeVyrudlfK: {
id: '-KkBgUmX6BEeVyrudlfK',
nome: 'test'
}
-KkDYxfwka8YM6uFOWpH: {
id: '-KkDYxfwka8YM6uFOWpH',
nome: 'test1'
}
}
JavaScript
var setorRef = firebase.database().ref().child("setores");
/* DELETE ROW */
$("#tbody_setores").on('click','.delete-btn', function(e){
var $row = $(this).closest('tr'),
rowId = $row.data('id');
var rowId = $row.data('id');
//it should remove the firebase object in here
setorRef.child(rowId).remove()
.then(function() {
//after firebase confirmation, remove table row
$row.remove();
})
.catch(function(error) {
console.log('Synchronization failed');
});
});
setorRef.on("child_changed", snap => {
var setorKey = snap.child("id").val();
var nome = snap.child("nome").val();
$("#tbody_setores").append("<tr data-id'"+setorKey+">"+
"<td class='mdl-data-table__cell--non-numeric'>" + nome + "</td>" +
"<td class='mdl-data-table__cell--non-numeric'>"+
"<div buttons>"+
"<button class='delete-btn mdl-button mdl-js-button mdl-button--icon mdl-button--colored'><i class='material-icons'>delete</i></button>"+" "+
"</div>"+
"</td>"+
"</tr>");
});
Now, what I want to do with this code is: when I click on the remove button that has been appended on the table, it should remove the row and the corresponding object in the database. I was using this same code on another table (here), and it works just fine, but on this one, the rowId is returning undefined instead of the id.
snap.child("nome").val(), while the JSON hasname: 'test'. It's probably not the cause of the problem you asked about (since it's not aboutid), but still a thing you'll want to fix.