This question has been asked many times before and is covered in the documentation for Working with Lists of Data. But here is a bundled example of one way to achieve what you want.
Assuming this is your table:
<table style="width:100%" id="gamehomeitems-table">
<tr id="gamehomeitems-headerrow">
<th>Category:</th>
<th>Home Team:</th>
<th>Away Team:</th>
</tr>
</table>
<!-- at the bottom of the body -->
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-database.js"></script>
<script>
firebase.initializeApp(/* ... */);
const gameHomeItemsRef = firebase.database().ref("gamehomeitems");
const gameHomeItemsTbl = document.getElementById("gamehomeitems-table");
const gameHomeItemsQuery = gameHomeItemsRef.orderByKey().limitToLast(20); // get 20 most recent entries
const gameHomeItemsChildAddedCallback =
gameHomeItemsQuery
.on('child_added', function(snapshot, keyOfPreviousRow) {
const rowData = snapshot.val();
let rowContent = '<tr id="item-' + snapshot.key + '">';
rowContent += "<td>" + rowData.category + "</td>";
rowContent += "<td>" + rowData.value1 + "</td>";
rowContent += "<td>" + rowData.value2 + "</td>";
rowContent += "</tr>";
gameHomeItemsTbl.append(rowContent);
// todo: make sure this row is inserted above the row for "keyOfPreviousRow" if newest at the top is required.
// currently it will be oldest at the top, newest at the bottom
});
const gameHomeItemsChildChangedCallback =
gameHomeItemsQuery
.on('child_changed', function(snapshot, keyOfPreviousRow) {
// todo: find row with id "item-KEY" and update it's data in the table
});
const gameHomeItemsChildRemovedCallback =
gameHomeItemsQuery
.on('child_removed', function(snapshot) {
// todo: find row with id "item-KEY" and remove it from the table
});
// later, when you no longer need the table to be updated live
// gameHomeItemsRef.off("child_added", gameHomeItemsChildAddedCallback);
// gameHomeItemsRef.off("child_changed", gameHomeItemsChildChangedCallback);
// gameHomeItemsRef.off("child_removed", gameHomeItemsChildRemovedCallback);
// todo: implement "child_moved" when not using orderByKey()
</script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…