Skip to content

Commit

Permalink
Added SortBy and SortOrder options to Znuny.Form.Input.Set to sort se…
Browse files Browse the repository at this point in the history
…lect field options by key or value.
  • Loading branch information
dennykorsukewitz committed Feb 6, 2025
1 parent 301c938 commit 8a54c9f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 6.5.12 2024-??-??
- 2025-02-06 Added SortBy and SortOrder options to Znuny.Form.Input.Set to sort select field options by key or value.
- 2025-01-22 Fixed uninitialized value bug. TicketZoom/Agent/MIMEBase.pm - Added special handling for 'undef' values in the arrayref $Event->{$Property}.
- 2025-01-21 Fixed Bug - Problem when handling errors in the owner field in the activity dialog. Thanks to Daylton Rodrigues (@dayltonr) for reporting. [#627](https://github.com/znuny/Znuny/issues/627).
- 2025-01-16 Increased size of columns profile_key and profile_value of database table search_profile.
Expand Down
68 changes: 61 additions & 7 deletions var/httpd/htdocs/js/Znuny.Form.Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,20 @@ Znuny.Form.Input = (function (TargetNS) {
var Success = Znuny.Form.Input.Set('Queue',
'Postmaster',
{
KeyOrValue: 'Value',
TriggerChange: 'false',
KeyOrValue: 'Value', # Key, Value
TriggerChange: 'false',
Modernize: true, # true, false
SelectOption: true, # true, false - set options of select field
AddEmptyOption: true, # true, false - add empty option as first option for single-selects/dropdowns
SortBy: 'Key', # Key, Value - Key is default
SortOrder: 'ASC', # ASC, DESC - ASC is default
}
);
Returns:
var Success = true; # true, false
var Success = true; # true, false
*/
TargetNS.Set = function (Attribute, Content, Options) {
Expand Down Expand Up @@ -858,6 +864,7 @@ Znuny.Form.Input = (function (TargetNS) {
$('#'+ FieldID +' option').remove();

function AppendOptions() {
var ContentArray;

// Add empty option as first option for single-selects/dropdowns
// because otherwise somehow the first element will be selected
Expand All @@ -873,11 +880,58 @@ Znuny.Form.Input = (function (TargetNS) {
) {
$('#'+ FieldID).append($('<option>', { value: '', selected: true }).text('-'));
}
$.each(Content, function(Key, Value) {
if (Value !== '') {
$('#'+ FieldID).append($('<option>', { value: Key }).text(Value));

// create array from object
if (Options.SortBy || Options.SortOrder) {

ContentArray = Object.entries(Content).map(([key, value]) => ({ key: parseInt(key), value }));

if (
typeof Options.SortBy === 'undefined'
|| (Options.SortBy !== 'Key' && Options.SortBy !== 'Value')
) {
Options.SortBy = 'Key';
}
});

if (
typeof Options.SortOrder === 'undefined'
|| (Options.SortOrder !== 'DESC' && Options.SortOrder !== 'ASC')
) {
Options.SortOrder = 'DESC';
}
// sort by id
if (Options.SortBy == 'Key') {
ContentArray.sort((a, b) => a.key - b.key);
}

// sort by name
else if (Options.SortBy == 'Value') {
ContentArray.sort((a, b) => a.value.localeCompare(b.value));
}

// sort order
if (Options.SortOrder == 'DESC') {
ContentArray.reverse();
}
// add options
ContentArray.forEach(function(item) {
var Key = item.key;
var Value = item.value;

if (Value !== '') {
$('#'+ FieldID).append($('<option>', { value: Key }).text(Value));
}
});
}

// add options without sorting
else {
$.each(Content, function(Key, Value) {
if (Value !== '') {
$('#'+ FieldID).append($('<option>', { value: Key }).text(Value));
}
});
}
}

function RedrawInputField() {
Expand Down

0 comments on commit 8a54c9f

Please sign in to comment.