create account

Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database by duski.harahap

View this thread on: hive.blogpeakd.comecency.com
· @duski.harahap ·
$23.65
Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database
#### Repository
https://github.com/firebase

#### What Will I Learn?
- Delete data
- Set Attribute in element
- Edit and Update data

#### Requirements
- Basic HTML, CSS, and javascript
- Use firebase
- Local server (Xampp, Wampp, or etc)


#### Resources
- Firebase - https://firebase.google.com/

#### Difficulty
Basic

### Tutorial Content

We will continue the series of tutorials about firebase, for those of you who have not followed the previous tutorial I suggest you take part in the previous [tutorial](https://steemit.com/utopian-io/@duski.harahap/create-a-system-admin-panel-using-firebase-3-generate-unique-id-and-retrieve-data-1542206827928) in the curriculum section. In previous tutorials, we have created a ***CRUD*** system in Firebase and learn about ***unique IDs*** in our firebase database. But we only learn how to create data in the database. now we will continue by learning how to ***delete and update***. For that, we just start this tutorial.


### CRUD in firebase database

We will do the CRUD system applied by the firebase that we have created. in the previous tutorial, we made ***CREATE***. in this tutorial, it goes on and we will ***DELETE*** the database in firebase.

- **Create elements for unique id**

to delete. naturally, we need a unique ID so that the data we delete is appropriate, In previous tutorials, we have learned how to create unique IDs. firebase does not recognize the system of relations between tables, therefore we *cannot* create **PRIMARY KEY** and **FOREIGN KEY**. so we must create an own unique ID. The following is a unique ID that we have made in the previous tutorial.

![Screenshot_4.png](https://ipfs.busy.org/ipfs/QmYPZ95rH2XUqC27iqKJnSGwXbwa9UEXdrEqVurS76aNJN)


We already have a **unique id** that represents each data. as we see above the unique id is **KEY** of the **JSON** object.

We will put the **id** into the additional element that we created with the tag element ```<a>```. The **unique id** that we mean is the time that is in the *object*.

![Screenshot_6.png](https://ipfs.busy.org/ipfs/QmaorNzYuTdpZpg5BzB2ocZmdetwbn8R8yRN5E2zzESWBb)

**time** we can make unique id because time has unique and special values and it is different from other keys on the object. Therefore time can be a marker to represent an object. well after we get key ***time***. We will put it in the ```<a>``` element tag like the following:

**Function ```showData()```**

```
 function showData(items) {
    var content = document.getElementById('content');
    var data    = '';

    items.forEach(function(item) {
      console.log(item.val())
      data += '<div class="col-md-4"> <h2>'+ item.val().title + '</h2> <p>'+ item.val().desc +'</p> <p><a class="btn btn-danger" href="#" role="button" onclick="removeItem('+item.val().time+')">Delete &raquo;</a></p> </div>';
    })
    content.innerHTML = data;
  }
```

We can put the **onclick** event on the ```<a>``` element. in the **onclick** event we will call the ```removeItem(item.val().time)``` and will pass the unique id. that is ```item.val().time``` and we can see the HTML element event and time is attached to the element.


![Screenshot_8.png](https://ipfs.busy.org/ipfs/QmbZKQAeQTR7nic9rqHgdrMxJHhzKi7aFsd3Hd31WCFnDK)

<br>

- **Create a remove function**

Now it will start creating a function to **delete** data in the database. In *firebase* we can delete data using the ```remove()``` method. The following is an example:

```
function removeItem(id) {
    database.ref('/blogs/'+ id).remove()
}
```
	
- Because we will use the function or event from *firebase.database*. We must first define a variable in this tutorial we have defined the database variable ```var database  = firebase.database();```.

- and then we have to select data that we will delete with ```ref ()```. We will delete ```/blogs/'+ id```.It's means **blogs** is the parents of the object **id** that we get from the parameters we receive in the ```removeItem(id) ``` function. We can see the database directory as shown below:

![Screenshot_9.png](https://ipfs.busy.org/ipfs/QmTPQ2wMhNb8vYsJLZFYNqmd2mKTJUT4hSci8W4j2U2Mzj)

We can see the database directory as shown below ```'/blogs/1542198684582'```.
 
- If all have been completed we can test to delete one of the data in the database. the following are the results

![ezgif.com-video-to-gif (2).gif](https://ipfs.busy.org/ipfs/QmbjXQoZwZsbS9EHDixQbR1GVMgyeW4eMRHMptTu1t2Ej2)

We have been successful to delete data in the database in real-time. Next, we will learn how to edit data.

### Edit data firebase

as well as updating data on other databases. to edit we need a ***unique id*** that represents that data. not much different from the delete function we will also use a unique id which in this tutorial we get from **time**.

- **Create edit function**

Before we make the edit function. We have to make a button so that the user can interact with our system. I add the following button to the HTML element:


```
function showData(items) {
    var content = document.getElementById('content');
    var data    = '';

    items.forEach(function(item) {
      //render html
      data += '<div class="col-md-4"> <h2>'+ item.val().title + '</h2> <p>'+ item.val().desc +'</p> <p><a class="btn btn-danger" href="#" role="button" onclick="removeItem('+item.val().time+')">Delete &raquo;</a></p> <p><a class="btn btn-primary" href="#" role="button" onclick="editItem('+item.val().time+')">Edit &raquo;</a></p> </div>';
    })
    content.innerHTML = data;
  }
```

We will use the ***onclick*** event on the element we created and in that event we will call the ```editItem ()``` and in this function we will pass the unique id from key **time** ```item.val().time```.
<br>
**function ```editItem()```**

in the ```editItem()``` function we can received the **id** parameter that we will use as a marker of the data object we will edit. For more details, we can see the code below:

```
function editItem(id) {
    document.getElementById('btnEditData').setAttribute('data-id', id);
    blogsRef.child(id).once('value', function(item){
      console.log(item.val().title)
    })
}
```

- Because what we are editing is the specific data we need to *reference* the ***parent data or table*** that we will edit. We have defined blogs in variable references ```var blogsRef  = database.ref('blogs');```.

- to access child data we can use the ```child ()``` method and pass the unique ID **(id)** and then we can use ```once()```, because we will only run it *once*.

- We can see the data in callback function ```function(item)``` before we go far we can check whether the data can be taken properly. to get the data using the ```val ()```.

- We will use the setAttribute method to add attributes to the button element that we press. we will add the **data-id** attribute ```document.getElementById('btnEditData').setAttribute('data-id', id);```. we need an id to update specific data, so we need id by using the attribute in the element.

![ezgif.com-video-to-gif (3).gif](https://ipfs.busy.org/ipfs/QmUT55qAGorGLQMVvupGXnHvks9nNrAcE4PfwWxaSpruMg)
<br>
**Create function onclick in update button**

After we have got the data we need, we will create a new button that will be used to update the data, the element like this:

***Element***

```<button id="btnEditData" class="btn btn-success">Update</button>```

***Defined in javascript***

```var btnEdit     = document.getElementById('btnEditData');```


***Create function onclick in button update***

```
btnEdit.addEventListener('click', function() {
    var id = document.getElementById('btnEditData').getAttribute('data-id'); // get data id that we set in editItem()

    blogsRef.child(id).update({ //Use event update()
      title: title.value,
      desc: desc.value
    })

    title.value =  '';
    desc.value  = '';
  })
```
- In the ```editItem()``` function we have set **data-id** based on the unique id. Now we will **get the data-id** that we get from ```var id = document.getElementById('btnEditData').getAttribute('data-id');```.

- Because we will update data that comes from the child, we have to access child, we can access the child with ```child() ``` and we pass the parameter **id** that we get from **data-id**. 

- to update we can use the ```update()``` method and we pass the new value from the object we are editing.
```
{
      title: title.value,
      desc: desc.value
 }
```

After all the steps are done we can run our application and see the results as shown below:

![ezgif.com-video-to-gif (4).gif](https://ipfs.busy.org/ipfs/QmQnzcoMd1P3i6hJ41EqcCCKThR8Jo9aYWnnEw3xHL2CoT)


We can see in the picture above. We have done to edit and update the data that is in our firebase database. this means we have completed the **CRUD** system on our application. the tutorial that we follow is just an illustration so you can understand how firebase databases work. the implementation may be different but the concepts we learn are the same. I hope this tutorial is useful for you. thank you

#### Curriculum

[Create a system admin panel using Firebase #1 : initialization, user admin settings, and authentication user admin](https://steemit.com/utopian-io/@duski.harahap/create-a-system-admin-panel-using-firebase-1-initialization-user-admin-settings-and-authentication-user-admin-1541779162747)

[Create a system admin panel using Firebase #2 : Use database and Add child data](https://steemit.com/utopian-io/@duski.harahap/create-a-system-admin-panel-using-firebase-2-use-firebase-database-and-add-child-data-in-realtime-database-1542036820373)

[Create a system admin panel using Firebase #3 : Create a system admin panel using Firebase #3 : Generate unique ID and Retrieve data](https://steemit.com/utopian-io/@duski.harahap/create-a-system-admin-panel-using-firebase-3-generate-unique-id-and-retrieve-data-1542206827928)

#### Proof of work done
https://github.com/milleaduski/firebaseAdminPanel
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 25 others
properties (23)
authorduski.harahap
permlinkcreate-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754
categoryutopian-io
json_metadata{"app":"steeditor/0.1.2","format":"markdown","image":["https://ipfs.busy.org/ipfs/QmYzf1D1dxRtKHmNV8RdXTcGoWcxnx4nTFzQvCPbbDoZ6J","https://ipfs.busy.org/ipfs/QmYPZ95rH2XUqC27iqKJnSGwXbwa9UEXdrEqVurS76aNJN","https://ipfs.busy.org/ipfs/QmaorNzYuTdpZpg5BzB2ocZmdetwbn8R8yRN5E2zzESWBb","https://ipfs.busy.org/ipfs/QmbZKQAeQTR7nic9rqHgdrMxJHhzKi7aFsd3Hd31WCFnDK","https://ipfs.busy.org/ipfs/QmTPQ2wMhNb8vYsJLZFYNqmd2mKTJUT4hSci8W4j2U2Mzj","https://ipfs.busy.org/ipfs/QmbjXQoZwZsbS9EHDixQbR1GVMgyeW4eMRHMptTu1t2Ej2","https://ipfs.busy.org/ipfs/QmUT55qAGorGLQMVvupGXnHvks9nNrAcE4PfwWxaSpruMg","https://ipfs.busy.org/ipfs/QmQnzcoMd1P3i6hJ41EqcCCKThR8Jo9aYWnnEw3xHL2CoT"],"tags":["utopian-io","tutorials","firebase","javascript"],"users":["duski"],"links":["https://github.com/firebase","https://firebase.google.com/","https://steemit.com/utopian-io/@duski.harahap/create-a-system-admin-panel-using-firebase-3-generate-unique-id-and-retrieve-data-1542206827928","https://steemit.com/utopian-io/@duski.harahap/create-a-system-admin-panel-using-firebase-1-initialization-user-admin-settings-and-authentication-user-admin-1541779162747","https://steemit.com/utopian-io/@duski.harahap/create-a-system-admin-panel-using-firebase-2-use-firebase-database-and-add-child-data-in-realtime-database-1542036820373","https://github.com/milleaduski/firebaseAdminPanel"]}
created2018-11-17 11:29:18
last_update2018-11-17 11:29:18
depth0
children5
last_payout2018-11-24 11:29:18
cashout_time1969-12-31 23:59:59
total_payout_value17.754 HBD
curator_payout_value5.892 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,943
author_reputation60,094,717,098,672
root_title"Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database"
beneficiaries
0.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id75,438,546
net_rshares39,291,295,417,660
author_curate_reward""
vote details (89)
@portugalcoin ·
$5.88
Thank you for your contribution @duski.harahap.
After an analysis of your tutorial we only have one point to suggest:

- We suggest you enter comments in your code. The comments are very important for a better interpretation of the code.

Thanks again for your good work. The quality of your tutorials is getting better.
We look forward to your next contribution.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/21311313).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , ,
properties (23)
authorportugalcoin
permlinkre-duskiharahap-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181117t132824345z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["duski.harahap"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/21311313","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-11-17 13:28:24
last_update2018-11-17 13:28:24
depth1
children2
last_payout2018-11-24 13:28:24
cashout_time1969-12-31 23:59:59
total_payout_value4.446 HBD
curator_payout_value1.431 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length855
author_reputation599,460,589,822,571
root_title"Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,443,334
net_rshares9,339,939,080,645
author_curate_reward""
vote details (11)
@duski.harahap ·
thanks for your feedback @portugalcoin :)
properties (22)
authorduski.harahap
permlinkre-portugalcoin-re-duskiharahap-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181119t043739067z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["portugalcoin"],"app":"steemit/0.1"}
created2018-11-19 04:37:39
last_update2018-11-19 04:37:39
depth2
children0
last_payout2018-11-26 04:37:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length41
author_reputation60,094,717,098,672
root_title"Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,533,126
net_rshares0
@utopian-io ·
Thank you for your review, @portugalcoin! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-duskiharahap-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181117t132824345z-20181120t030529z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-20 03:05:30
last_update2018-11-20 03:05:30
depth2
children0
last_payout2018-11-27 03:05:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length64
author_reputation152,955,367,999,756
root_title"Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,591,192
net_rshares0
@steem-ua ·
#### Hi @duski.harahap!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181117t133148z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-17 13:31:48
last_update2018-11-17 13:31:48
depth1
children0
last_payout2018-11-24 13:31:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length292
author_reputation23,214,230,978,060
root_title"Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,443,495
net_rshares0
@utopian-io ·
Hey, @duski.harahap!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181118t033955z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-18 03:39:57
last_update2018-11-18 03:39:57
depth1
children0
last_payout2018-11-25 03:39:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length595
author_reputation152,955,367,999,756
root_title"Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,474,969
net_rshares0