about summary refs log tree commit diff
path: root/uploader/static/js/datatables.js
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/static/js/datatables.js')
-rw-r--r--uploader/static/js/datatables.js60
1 files changed, 59 insertions, 1 deletions
diff --git a/uploader/static/js/datatables.js b/uploader/static/js/datatables.js
index 8d54a84..9782a60 100644
--- a/uploader/static/js/datatables.js
+++ b/uploader/static/js/datatables.js
@@ -54,4 +54,62 @@ var dtAddRowClickHandler = (tableId) => {
 var dtAddCommonHandlers = (tableId) => {
     dtAddRowSelectionHandler(tableId);
     dtAddRowClickHandler(tableId);
-}
+};
+
+var addTableLength = (menuList, lengthToAdd, dataLength) => {
+    if(dataLength >= lengthToAdd) {
+        newList = structuredClone(menuList);//menuList.slice(0, menuList.length); // shallow copy
+        newList.push(lengthToAdd);
+        return newList;
+    }
+    return menuList;
+};
+
+var defaultLengthMenu = (data) => {
+    menuList = []
+    var lengths = [10, 25, 50, 100, 1000, data.length];
+    lengths.forEach((len) => {
+        menuList = addTableLength(menuList, len, data.length);
+    });
+    return menuList;
+};
+
+var buildDataTable = (tableId, data = [], columns = [], userSettings = {}) => {
+    var defaultSettings = {
+        responsive: true,
+        /* == Scroller settings == */
+        scroller: true,
+        paging: true, // MUST be true for scroller to work
+        sDom: "iti",
+        scrollY: "100vh",
+        scrollCollapse: true,
+        /* == END: Scroller settings == */
+        lengthMenu: defaultLengthMenu(data),
+        language: {
+            processing: "Processing… Please wait.",
+            loadingRecords: "Loading population — Please wait.",
+            lengthMenu: "Show _MENU_ populations",
+            info: "Showing _START_ to _END_ of _TOTAL_ populations"
+        },
+        data: data,
+        columns: columns,
+        drawCallback: (settings) => {
+            $(this[0]).find("tbody tr").each((idx, row) => {
+                var arow = $(row);
+                var checkboxOrRadio = arow.find(".chk-row-select");
+                if (checkboxOrRadio) {
+                    if (arow.hasClass("selected")) {
+                        checkboxOrRadio.prop("checked", true);
+                    } else {
+                        checkboxOrRadio.prop("checked", false);
+                    }
+                }
+            });
+        }
+    }
+    var theDataTable = $(tableId).DataTable({
+        ...defaultSettings,
+        ...userSettings
+    });
+    return theDataTable;
+};