Skip to content

Commit

Permalink
docs + sorting #983
Browse files Browse the repository at this point in the history
  • Loading branch information
jokob-sk committed Feb 1, 2025
1 parent 9d8b147 commit c855d50
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/PLUGINS_DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ Each element may also have associated events (e.g., running a scan or triggering

##### Supported settings `function` values

You can have any `"function": "my_custom_name"` custom name, however, the ones listed below have a specific functionality attached to them. If you use a custom name, then the setting is mostly used as an input parameter for the `params` section.
You can have any `"function": "my_custom_name"` custom name, however, the ones listed below have a specific functionality attached to them.

| Setting | Description |
| ------- | ----------- |
Expand Down
51 changes: 39 additions & 12 deletions front/js/ui_components.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,38 +231,65 @@ function copyToClipboard(buttonElement) {
// Simple Sortable Table columns
// -----------------------------------------------------------------------------

// Function to handle column sorting when a user clicks on a table header
function sortColumn(element) {
var th = $(element).closest('th');
var table = th.closest('table');
var columnIndex = th.index();
var ascending = !th.data('asc');
var th = $(element).closest('th'); // Get the clicked table header
var table = th.closest('table'); // Find the closest table
var columnIndex = th.index(); // Get the index of the column
var ascending = !th.data('asc'); // Toggle sorting order
sortTable(table, columnIndex, ascending);
th.data('asc', ascending);
th.data('asc', ascending); // Store sorting order
}

// Function to sort the table based on the selected column
function sortTable(table, columnIndex, ascending) {
var tbody = table.find('tbody');
var rows = tbody.find('tr').toArray().sort(comparer(columnIndex));
var tbody = table.find('tbody'); // Get the table body
var rows = tbody.find('tr').toArray().sort(comparer(columnIndex)); // Convert rows to an array and sort
if (!ascending) {
rows = rows.reverse();
rows = rows.reverse(); // Reverse order if descending
}
for (var i = 0; i < rows.length; i++) {
tbody.append(rows[i]);
tbody.append(rows[i]); // Append sorted rows back to the table
}
}

// Function to compare values in the selected column
function comparer(index) {
return function(a, b) {
return function (a, b) {
var valA = getCellValue(a, index);
var valB = getCellValue(b, index);
return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB);

// Check if both values are valid IP addresses, and sort numerically if so
if (isIPAddress(valA) && isIPAddress(valB)) {
return ipToNum(valA) - ipToNum(valB);
}

// If both values are numbers, sort numerically
if ($.isNumeric(valA) && $.isNumeric(valB)) {
return valA - valB;
}

// Otherwise, sort as text
return valA.localeCompare(valB);
};
}

// Function to get the text value from a table cell
function getCellValue(row, index) {
return $(row).children('td').eq(index).text();
return $(row).children('td').eq(index).text().trim(); // Get text from the specified column and trim spaces
}

// Function to check if a string is a valid IPv4 address
function isIPAddress(value) {
return /^\d{1,3}(\.\d{1,3}){3}$/.test(value); // Regular expression to match IPv4 format
}

// Function to convert an IP address to a numeric value for sorting
function ipToNum(ip) {
return ip.split('.').reduce((acc, octet) => (acc << 8) + parseInt(octet, 10), 0);
}


// -----------------------------------------------------------------------------
// handling events
// -----------------------------------------------------------------------------
Expand Down
Empty file modified front/php/templates/language/ca_ca.json
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion front/php/templates/language/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"BackDevices_DBTools_ImportCSVError": "The CSV file could not be imported. Make sure the format is correct.",
"BackDevices_DBTools_ImportCSVMissing": "The CSV file could not be found under <b>/config/devices.csv.</b>",
"BackDevices_DBTools_Purge": "The oldest backups were deleted",
"BackDevices_DBTools_UpdDev": "Device updated successfully",
"BackDevices_DBTools_UpdDev": "Device updated successfully. Main devices list may need some time to reload if a scan is in progress.",
"BackDevices_DBTools_UpdDevError": "Error updating device",
"BackDevices_DBTools_Upgrade": "Database upgraded successfully",
"BackDevices_DBTools_UpgradeError": "Database upgrade failed",
Expand Down

0 comments on commit c855d50

Please sign in to comment.