vendredi 23 août 2019

Assign an Document ID to each dynamically created row in Javascript using Firebase Firestore

I am retrieving data from Firebase Firestore and outputting each document held within a collection, as a table row. For example, in a collection of users, each document is a different user. Therefore each row represents a different user.

I would like to implement edit functionality so that if you click a row, you can edit that document specifically. I'm not sure how to go about doing this.

 function getData() {
      var table = document.getElementById("user-list-table");
      var index = 0;
      db.collection("users").get().then(function (querySnapshot) {
        querySnapshot.forEach(function (doc) {
          index++;
          var row = table.insertRow(index);
          var documentID = row.insertCell(0);
          var firstName = row.insertCell(1);
          var lastName = row.insertCell(2);
          var emailAddress = row.insertCell(3);
          var editbutton = row.insertCell(4);
          var deletebutton = row.insertCell(5);
          documentID.innerHTML = doc.id;
          firstName.innerHTML = doc.data().firstName;
          lastName.innerHTML = doc.data().lastName;
          emailAddress.innerHTML = doc.data().emailAddress;
          editbutton.innerHTML = '<button type="button" data-toggle="modal" data-target="#editModal" class="btn btn-primary btn-sm">Edit</button>'
          deletebutton.innerHTML = '<button type="button" onclick="deleteBanner()" class="btn btn-danger btn-sm">Delete</button>';
        });
      });
    }




Aucun commentaire:

Enregistrer un commentaire