我为此苦苦挣扎,它看起来很简单,但我做不到,我有javascript循环,我想将表格内的信息打印出来对我来说很好,但是我希望每行的最后一个单元格应为href向其添加ID,以便将href更改为最后一个单元格的第一行将被编辑。将编辑下一行的下一个单元格,这些由数据创建的数字来自循环中的数据,请检查代码,我尝试用单引号或双引号,但它不起作用,它将href内部的内容视为字符串,而不是函数或某些必须值
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>
答案 0 :(得分:2)
您可以这样尝试:
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>