Maverick
Maverick

Reputation: 11

How can i delete data from my Firebase and update my table?

I am making a schedule for trains, the information is coming straight from user input. The user input gets stored in to my Firebase, as well as getting displayed in to a table. Each row of information has a delete button. When you click on the delete button, I am trying to delete the data from my Firebase along with updating the table.

Here is where I pushed the train information to my Firebase:

 database.ref().push({
      name: name,
      dest: dest,
      time: time,
      freq: freq,
      minutesAway: mins,
      dateAdded: firebase.database.ServerValue.TIMESTAMP
    })

And then i have it displayed like this:

 $('.table').append('<tr><td>' + name +
    '</td>' + '<td>' + dest +
    '</td>' + '<td>' + time +
    '</td>' + '<td>' + freq +
    '</td>' + '<td>' + mins +
    '</td>' + '<td>' + '<button type="button" class="btn btn-danger btn-sm delete"></button>' +
    '</td>' + '</tr>');

Here is my onClick delete:

$(".delete").on("click", function () {
    $(this).parent().prevAll().parent().remove();

  });

And this is what my current Firebase tree looks like:

https://mavs-firebase.firebaseio.com/
mavs-firebase
-L7p60IRXp29D54Pk3dw
dateAdded: 
1521309455658
dest: 
"North Pole"
freq: 
"200"
minutesAway: 
122
name: 
"Polar Express"
time: 
"12:59"

I have been trying many different ways to find a function that deletes the data from Firebase and from the table, I cannot find a solution anywhere i look. I have even researched Firebase docs and i can't seem to target the right table and/or delete the data.

Upvotes: 1

Views: 1025

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80944

To remove data from the database, lets say you want to target this -L7p60IRXp29D54Pk3dw, you can try the following:

var database = firebase.database();
var del=database.ref().child("-L7p60IRXp29D54Pk3dw").remove();

this will remove the all the child under the above key.

Upvotes: 3

Related Questions