From cfa33d211372d40edcd35d2c0d74daf261fe6bf3 Mon Sep 17 00:00:00 2001
From: Frederick Muriuki Muriithi
Date: Tue, 11 Mar 2025 16:50:04 -0500
Subject: Extract common DataTables into a reusable function.
---
uploader/static/js/datatables.js | 60 ++++++++++++++++++++++++++++++++++++++-
uploader/static/js/populations.js | 33 ++++++---------------
uploader/static/js/species.js | 32 ++++++---------------
3 files changed, 75 insertions(+), 50 deletions(-)
(limited to 'uploader/static/js')
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;
+};
diff --git a/uploader/static/js/populations.js b/uploader/static/js/populations.js
index 5c1f848..73e298a 100644
--- a/uploader/static/js/populations.js
+++ b/uploader/static/js/populations.js
@@ -1,19 +1,10 @@
-var populationDataTable = (populationdata) => {
- var lengthMenu = [10, 25, 50, 100, 1000];
- if(populationdata.length > 1000) {
- lengthMenu.push(populationdata.length)
- }
- $("#tbl-select-population").DataTable({
- responsive: true,
- lengthMenu: lengthMenu,
- language: {
- processing: "Processing… Please wait.",
- loadingRecords: "Loading population — Please wait.",
- lengthMenu: "Show _MENU_ populations",
- info: "Showing _START_ to _END_ of _TOTAL_ populations"
- },
- data: populationdata,
- columns: [
+$(() => {
+ dtAddCommonHandlers("#tbl-select-population");
+ var populationsDataTable = buildDataTable(
+ "#tbl-select-population",
+ JSON.parse(
+ $("#tbl-select-population").attr("data-populations-list")),
+ [
{
data: (apopulation) => {
return ` {
return `${apopulation.FullName} (${apopulation.InbredSetName})`;
}
}
- ]
- });
-};
-
-
-$(() => {
- dtAddCommonHandlers("#tbl-select-population");
- populationDataTable(JSON.parse(
- $("#tbl-select-population").attr("data-populations-list")));
+ ]);
});
diff --git a/uploader/static/js/species.js b/uploader/static/js/species.js
index 1f2aa3b..c1374c6 100644
--- a/uploader/static/js/species.js
+++ b/uploader/static/js/species.js
@@ -1,19 +1,10 @@
-var speciesDataTable = (speciesdata) => {
- var lengthMenu = [10, 25, 50, 100, 1000];
- if(speciesdata.length > 1000) {
- lengthMenu.push(speciesdata.length)
- }
- $("#tbl-select-species").DataTable({
- responsive: true,
- lengthMenu: lengthMenu,
- language: {
- processing: "Processing… Please wait.",
- loadingRecords: "Loading species — Please wait.",
- lengthMenu: "Show _MENU_ species",
- info: "Showing _START_ to _END_ of _TOTAL_ species"
- },
- data: speciesdata,
- columns: [
+$(() => {
+ dtAddCommonHandlers("#tbl-select-species");
+ var speciesDataTable = buildDataTable(
+ "#tbl-select-species",
+ JSON.parse(
+ $("#tbl-select-species").attr("data-species-list")),
+ [
{
data: (aspecies) => {
return ` {
return `${aspecies.FullName} (${aspecies.SpeciesName})`;
}
}
- ]
- });
-};
-
-$(() => {
- dtAddCommonHandlers("#tbl-select-species");
- speciesDataTable(JSON.parse(
- $("#tbl-select-species").attr("data-species-list")));
+ ]);
});
--
cgit v1.2.3