#### 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.  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*.  **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 »</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.  <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:  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  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 »</a></p> <p><a class="btn btn-primary" href="#" role="button" onclick="editItem('+item.val().time+')">Edit »</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.  <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:  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
author | duski.harahap | ||||||
---|---|---|---|---|---|---|---|
permlink | create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754 | ||||||
category | utopian-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"]} | ||||||
created | 2018-11-17 11:29:18 | ||||||
last_update | 2018-11-17 11:29:18 | ||||||
depth | 0 | ||||||
children | 5 | ||||||
last_payout | 2018-11-24 11:29:18 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 17.754 HBD | ||||||
curator_payout_value | 5.892 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 9,943 | ||||||
author_reputation | 60,094,717,098,672 | ||||||
root_title | "Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 100,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 75,438,546 | ||||||
net_rshares | 39,291,295,417,660 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
arcange | 0 | 30,620,583,037 | 4% | ||
raphaelle | 0 | 2,858,479,319 | 4% | ||
matildapurse | 0 | 2,136,458,810 | 25% | ||
eforucom | 0 | 3,362,621,472 | 1% | ||
miniature-tiger | 0 | 95,481,965,979 | 50% | ||
aleister | 0 | 9,987,641,827 | 25% | ||
jga | 0 | 2,690,592,742 | 14.45% | ||
codingdefined | 0 | 7,669,275,623 | 7.5% | ||
bachuslib | 0 | 21,266,512,488 | 100% | ||
accelerator | 0 | 18,359,246,681 | 1.4% | ||
espoem | 0 | 1,639,519,021 | 1% | ||
mcfarhat | 0 | 8,987,932,035 | 12.38% | ||
martusamak | 0 | 8,846,903,487 | 25% | ||
pataty69 | 0 | 10,480,103,417 | 25% | ||
utopian-io | 0 | 38,026,852,470,020 | 28.91% | ||
shammi | 0 | 86,578,454,954 | 90% | ||
steemtaker | 0 | 1,085,050,155 | 1% | ||
cheneats | 0 | 330,043,764 | 4% | ||
newsrx | 0 | 1,368,157,112 | 100% | ||
scipio | 0 | 39,395,219,997 | 16.66% | ||
funtraveller | 0 | 9,113,344,216 | 1% | ||
dedicatedguy | 0 | 153,172,590,843 | 100% | ||
amosbastian | 0 | 65,315,692,240 | 30.95% | ||
portugalcoin | 0 | 2,949,667,104 | 15% | ||
tobias-g | 0 | 7,693,250,040 | 5% | ||
osazuisdela | 0 | 665,662,359 | 20% | ||
micaelacf | 0 | 912,900,562 | 25% | ||
legko | 0 | 214,004,649 | 1% | ||
properfraction | 0 | 661,529,494 | 100% | ||
hakancelik | 0 | 21,272,865,815 | 26% | ||
onepercentbetter | 0 | 12,949,713,535 | 5% | ||
effofex | 0 | 79,926,696 | 0.7% | ||
acehero | 0 | 4,624,703,707 | 100% | ||
enlighted | 0 | 81,901,309 | 25% | ||
bogdasha | 0 | 335,389,911 | 10% | ||
yougotresteemed | 0 | 91,016,429 | 4% | ||
merlion | 0 | 5,458,370,872 | 5% | ||
algo.coder | 0 | 40,935,483,264 | 60% | ||
suonghuynh | 0 | 6,157,470,445 | 0.15% | ||
missmanavi | 0 | 494,911,402 | 100% | ||
disragafmi | 0 | 470,949,560 | 100% | ||
molichyga | 0 | 567,166,646 | 100% | ||
contmeafgere | 0 | 475,035,392 | 100% | ||
motmagerbti | 0 | 488,337,057 | 100% | ||
dobulribi | 0 | 552,327,747 | 100% | ||
steemchoose | 0 | 62,408,713,453 | 3.5% | ||
tbtek | 0 | 827,243,643 | 25% | ||
duarte9sousa | 0 | 2,493,303,681 | 2.5% | ||
munhenhos | 0 | 1,730,058,981 | 25% | ||
isabelpereira | 0 | 99,372,686 | 5% | ||
chillibeans | 0 | 85,540,650 | 2% | ||
bhaski | 0 | 2,191,905,732 | 25% | ||
pitondessert | 0 | 549,500,176 | 100% | ||
pullsympathize | 0 | 549,352,605 | 100% | ||
chokingsqueak | 0 | 550,250,146 | 100% | ||
fizzyreadymade | 0 | 549,470,024 | 100% | ||
scubaelaborate | 0 | 550,358,229 | 100% | ||
moistmidpoint | 0 | 550,804,216 | 100% | ||
merlin7 | 0 | 8,004,866,030 | 0.35% | ||
steem-ua | 0 | 395,485,835,166 | 3.2% | ||
devidhodz | 0 | 550,643,968 | 100% | ||
dzhonmey | 0 | 551,563,980 | 100% | ||
tobireynol | 0 | 551,682,819 | 100% | ||
roman.senchukov | 0 | 551,751,161 | 100% | ||
rondorajon8 | 0 | 550,314,025 | 100% | ||
votes4minnows | 0 | 450,581,048 | 3% | ||
kaczynski | 0 | 51,850,869 | 100% | ||
nfc | 0 | 65,812,376,750 | 5% | ||
hdu | 0 | 452,655,284 | 1.5% | ||
screechingchothy | 0 | 553,477,599 | 100% | ||
ploverka | 0 | 553,575,078 | 100% | ||
sessionscells | 0 | 553,473,336 | 100% | ||
bangersmippery | 0 | 553,601,773 | 100% | ||
cheesylawn | 0 | 553,686,968 | 100% | ||
woldsfiver | 0 | 552,586,269 | 100% | ||
northernsound | 0 | 552,526,522 | 100% | ||
vitalikrude | 0 | 553,489,930 | 100% | ||
toxa555 | 0 | 553,325,380 | 100% | ||
carrotsstop | 0 | 553,201,238 | 100% | ||
smellbaryon | 0 | 552,441,995 | 100% | ||
punhatdelighted | 0 | 553,312,513 | 100% | ||
babiesvaulter | 0 | 553,221,222 | 100% | ||
multicackcoward | 0 | 553,297,240 | 100% | ||
charbodge | 0 | 553,390,664 | 100% | ||
bowedfrazzled | 0 | 553,347,784 | 100% | ||
jacksolstice | 0 | 552,549,725 | 100% | ||
ampereepoch | 0 | 553,303,567 | 100% | ||
whitebot | 0 | 18,860,440,143 | 1% | ||
chrnerd | 0 | 595,730,158 | 100% |
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/)
author | portugalcoin |
---|---|
permlink | re-duskiharahap-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181117t132824345z |
category | utopian-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"} |
created | 2018-11-17 13:28:24 |
last_update | 2018-11-17 13:28:24 |
depth | 1 |
children | 2 |
last_payout | 2018-11-24 13:28:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 4.446 HBD |
curator_payout_value | 1.431 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 855 |
author_reputation | 599,460,589,822,571 |
root_title | "Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 75,443,334 |
net_rshares | 9,339,939,080,645 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
utopian-io | 0 | 9,214,451,421,081 | 6.34% | ||
emrebeyler | 0 | 102,167,511 | 0.01% | ||
amosbastian | 0 | 26,968,812,614 | 12.82% | ||
organicgardener | 0 | 4,844,306,110 | 25% | ||
penghuren | 0 | 374,365,920 | 60% | ||
reazuliqbal | 0 | 9,308,341,503 | 8% | ||
hakancelik | 0 | 21,199,342,516 | 26% | ||
statsexpert | 0 | 6,843,828,239 | 100% | ||
mightypanda | 0 | 52,577,737,320 | 35% | ||
fastandcurious | 0 | 1,164,030,760 | 30% | ||
icurate | 0 | 2,104,727,071 | 54% |
thanks for your feedback @portugalcoin :)
author | duski.harahap |
---|---|
permlink | re-portugalcoin-re-duskiharahap-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181119t043739067z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"users":["portugalcoin"],"app":"steemit/0.1"} |
created | 2018-11-19 04:37:39 |
last_update | 2018-11-19 04:37:39 |
depth | 2 |
children | 0 |
last_payout | 2018-11-26 04:37:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 41 |
author_reputation | 60,094,717,098,672 |
root_title | "Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 75,533,126 |
net_rshares | 0 |
Thank you for your review, @portugalcoin! Keep up the good work!
author | utopian-io |
---|---|
permlink | re-re-duskiharahap-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181117t132824345z-20181120t030529z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-11-20 03:05:30 |
last_update | 2018-11-20 03:05:30 |
depth | 2 |
children | 0 |
last_payout | 2018-11-27 03:05:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 64 |
author_reputation | 152,955,367,999,756 |
root_title | "Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 75,591,192 |
net_rshares | 0 |
#### 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)**
author | steem-ua |
---|---|
permlink | re-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181117t133148z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-11-17 13:31:48 |
last_update | 2018-11-17 13:31:48 |
depth | 1 |
children | 0 |
last_payout | 2018-11-24 13:31:48 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 292 |
author_reputation | 23,214,230,978,060 |
root_title | "Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 75,443,495 |
net_rshares | 0 |
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>
author | utopian-io |
---|---|
permlink | re-create-a-system-admin-panel-using-firebase-4-edit-update-and-delete-in-firebase-database-1542454152754-20181118t033955z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-11-18 03:39:57 |
last_update | 2018-11-18 03:39:57 |
depth | 1 |
children | 0 |
last_payout | 2018-11-25 03:39:57 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 595 |
author_reputation | 152,955,367,999,756 |
root_title | "Create a system admin panel using Firebase #4 : Edit, Update, and Delete in firebase database" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 75,474,969 |
net_rshares | 0 |