Reputation: 31
I struggle on this , its look very simple but I cant do it , I have javascript loop I want to print the information inside the table works fine for me , but I want the last cell of every row should be an href with adding id to it so href will be changed for last cell first row would be edite next cell in the next row will be edit, those number created by the data come from data in the loop check the code, I tried with a single quote or double quote but it doesn't work it treat the what inside href as a string, not function or something have to value
var config = {
apiKey: "my_secret_key",
authDomain: "xyz.firebaseapp.com",
databaseURL: "https://xyz.firebaseio.com",
projectId: "xyz",
storageBucket: "xyz.appspot.com",
messagingSenderId: "0000000000000"
};
firebase.initializeApp(config);
var db = firebase.firestore();
db.collection("users").onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
var_data = doc.data().firt_name;
var_last = doc.data().last_name;
document.getElementById("table").innerHTML += "<tr><td>" + doc.data().first_name + " </td>" +
"<td>" + doc.data().last_name + "</td>" + "<td>" + doc.data().active +
"<td>" + "<td><a href='x.php?id='doc.data().first_name >Edit</a></tr>";
});
});
<script src="https://www.gstatic.com/firebasejs/4.8.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.8.1/firebase-firestore.js"></script>
<table id="table" >
<th>name</th>
<th>last</th>
<th>active</th>
<th>id</th>
<tr>
</tr>
</table>
Upvotes: 2
Views: 79
Reputation: 663
You can try it like this:
var config = {
apiKey: "my_secret_key",
authDomain: "xyz.firebaseapp.com",
databaseURL: "https://xyz.firebaseio.com",
projectId: "xyz",
storageBucket: "xyz.appspot.com",
messagingSenderId: "0000000000000"
};
firebase.initializeApp(config);
var db = firebase.firestore();
db.collection("users").onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
var_data = doc.data().firt_name;
var_last = doc.data().last_name;
document.getElementById("table").innerHTML += "<tr><td>" + doc.data().first_name + " </td>" +
"<td>" + doc.data().last_name + "</td>" + "<td>" + doc.data().active +
"<td>" + "<td><a href='x.php?id=" + doc.data().first_name + "'>Edit</a></tr>";
});
});
<script src="https://www.gstatic.com/firebasejs/4.8.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.8.1/firebase-firestore.js"></script>
<table id="table" >
<th>name</th>
<th>last</th>
<th>active</th>
<th>id</th>
<tr>
</tr>
</table>
Upvotes: 2