简体   繁体   English

编辑和删除Firebase

[英]Edit and Delete firebase

   <table>
      <tr>
         <td>Name: </td>
         <td><input type="text" name="user_name" id="user_name"  /></td>
      </tr>
      <tr>
         <td>Phone: </td>
         <td><input type="text" name="user_phone" id="user_phone"  /></td>
      </tr>
      <tr>
         <td>Address </td>
         <td><select name="user_ads" id="user_ads">
             <option value="A">A</option>
             <option value="B">B</option>
             <option value="C">C</option>
             </select>
      <tr>
         <td colspan="2">
             <input type="button" value="Save" onclick="save_user();"  />
             <input type="button" value="Update" onclick="update_user();"  />
         </td>
      </tr>
    </table>

  <table id="users_list" border="1">
   <tr>
       <td>NAME</td>
       <td>PHONE</td>
       <td>ADDRESS</td>
       <td>ACTION</td>
   </tr>
   </table>

   var usersList = document.getElementById('users_list');
   var databaseRef = firebase.database().ref('users/');
   var rowIndex = 1;


   databaseRef.once('value', function(snapshot) {
     snapshot.forEach(function(childSnapshot) {
        var childKey = childSnapshot.key;
        var childData = childSnapshot.val();
        var button = document.createElement("button");
        var button2 = document.createElement("button");
        button.innerHTML = "Edit";
        button2.innerHTML = "Delete";

        var row = usersList.insertRow(rowIndex);
        var cellName = row.insertCell(0);
        var cellPhone = row.insertCell(1);
        var cellAddress = row.insertCell(2);
        var cellButton = row.insertCell(3);
        cellName.appendChild(document.createTextNode(childData.name));
        cellPhone.appendChild(document.createTextNode(childData.phone));
        cellAddress.appendChild(document.createTextNode(childData.address));
        cellButton.appendChild(button);
        cellButton.appendChild(button2);

        button.onclick = delete;
        button2.onclick = edit;

        rowIndex = rowIndex + 1;
     });
    });


    function delete(){
           var row = document.getElementById("users_list");
           firebase.database().ref().child('/users/' + user_id).remove();

           alert('The user is deleted successfully!');

           reload_page();

          }

    function edit {

     }


        function update_user(){

           var data = {
               user_id: user_id,
               user_name: user_name
           }

           var updates = {};
           updates['/users/' + user_id] = data;
           firebase.database().ref().update(updates);

           alert('The user is updated successfully!');

           reload_page();
        }

</script>

I've created a database named users in firebase with data store name, phone and address. 我在Firebase中创建了一个名为users的数据库,其中包含数据存储名称,电话和地址。 I've retrieved the data from firebase in the table and created a new column with edit button and delete button in the table. 我已经从表中的Firebase中检索了数据,并在表中使用“编辑”按钮和“删除”按钮创建了一个新列。 What should I add in delete function that enable to delete the firebase data in the row when the button delete was click and when edit button in the row was click it retrieve the firebase data in the row to the table form and click update to update the data in firebase. 我应该在删除功能中添加哪些功能,以便在单击删除按钮时单击该行中的Firebase数据,并在单击该行中的编辑按钮时单击它,将行中的Firebase数据检索到表格形式,然后单击“更新”以更新Firebase中的数据。

A similar output: 类似的输出: 在此处输入图片说明

use this code to delete, 使用此代码删除,

considering users is your root, use users/ instead of /users/ 考虑到users是您的根,请使用users/而不是/users/

  var ref = firebase.database().ref().child('users/' + user_id)
    ref.once("value")
    .then(function(snapshot) {
       snapshot.ref.remove();
       alert("user deleted..!");
   }).catch(function(error) {alert("Data could not be deleted." + error);});

use this code to update, 使用此代码进行更新,

       var updates = {};
       updates['/users/' + user_id] = data;
       var ref = firebase.database().ref().child('users/' + user_id)
       ref.update(updates).then(function(){
          ref.on('value', function(snapshot) {
            alert("user updated");
         });
       }).catch(function(error) {alert("Data could not be saved." + error);});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM