Datatable only shows one entry
Datatable only shows one entry
jay123
Posts: 3Questions: 1Answers: 0
I am building a web app with firestore as its database. Currently, I am retrieving all documents from a collection then injecting those documents into a datatable using an array. The problem is, only one document is shown and the collection has 10 documents within it. What could be the problem? Below is the code:
html:![](https://datatables.net/forums/uploads/editor/mj/o83ctkt7bqwy.png "")
![](https://datatables.net/forums/uploads/editor/l2/eolop8dcsc5n.png "")
<table class="table table-striped table-bordered" style="width:fit-content; " id="mydatatable">
<thead>
<tr>
<th scope="col">Document id</th>
<th scope="col">title</th>
<th scope="col">details</th>
<th scope="col">timestamp</th>
<th scope="col">name</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col">Document id</th>
<th scope="col">title</th>
<th scope="col">details</th>
<th scope="col">timestamp</th>
<th scope="col">name</th>
</tr>
</tfoot>
</table>
javascript:
db.collection("Emergency_Feeds").orderBy("timestamp","desc").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
var documentId = doc.id;
var username = doc.data().name;
var emTitle = doc.data().title;
var emDets = doc.data().details;
var emTimeDate = doc.data().timestamp.toDate();
tableData =[
[
documentId,
emTitle,
emDets,
emTimeDate,
username
]
]
console.log('Data:'+tableData);
$('#mydatatable').DataTable( {
retrieve: true,
data:tableData
} );
});
});
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You need to initialize your array before the loop then use push() to add the new records to the array. You also need to move the Datatables initialization outside of the loop so it executes only once. Something like this:
Didn't test it so there may be errors but should give you an idea of what to do.
Kevin
solved the issue , thank you
Hello, can Anyone help me addapt mine? I am completely rookie, have tried to search for documentation but I can't seem to find... So I need practical help
Thanks in advance
@maramedinan We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin