Google Firestore & Datatables integration

Google Firestore & Datatables integration

demarilydemarily Posts: 7Questions: 2Answers: 0

This may not be the best approach but I don't have many examples. I'm trying to load an empty table then in the initComplete option add the data, remove the data and/or modify the data. Below is an example of just adding a row using a querySnapshot.

var routeStepsRef = db.collection("routes").doc(objModel.routeId()).collection("routeSteps").onSnapshot(function (querySnapshot) {
    querySnapshot.docChanges().forEach(function (change) {
        switch (change.type) {
            case "added":
                $table.row.add(change.doc.data()).draw(false);
                console.log("New address: ", change.doc.data());
                break;
            case "modified":
                console.log("Modified address: ", change.doc.data());
                break;
            case "removed":
                console.log("Removed address: ", change.doc.data());
                break;
        }
    });
});

Again this would happen inside the initComplete or at least after the table loads with no data... It would be awesome if someone knows of a plugin or something to use these two library's together.

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    I'm not aware of a library that will do this for you I'm afraid. That you have looks okay - although depending on what change.doc.data() gives you, you might need to use rows.add().

    What does change.doc.data() give?

    Allan

  • demarilydemarily Posts: 7Questions: 2Answers: 0

    The "added" case adds each object as they come in. The "modified" and "removed" cases only affect one object at a time. I think i just need to start the columns over and build back up again.

  • demarilydemarily Posts: 7Questions: 2Answers: 0

    Do I have to have a data or ajax option? I would like the data loading to happen after the table initializes.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    No - you don't need either the ajax or data option. If neither are given, DataTables will attempt to load the data from the HTML - which could be empty and then you can use the API to add rows.

    You do need to know the column information when you initialise the DataTable - you can't dynamically add and remove columns at this time.

    Allan

This discussion has been closed.