Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to sort the listbox items by given sort funcs on update of listbox #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/dual-listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class DualListbox {
this.sortable = false;
this.upButtonText = "up";
this.downButtonText = "down";

this.sortAvailableListItems = false;
this.sortSelectedListItems = false;
}

/**
Expand Down Expand Up @@ -164,14 +167,14 @@ class DualListbox {
* Update the elements in the available listbox;
*/
updateAvailableListbox() {
this._updateListbox(this.availableList, this.available);
this._updateListbox(this.availableList, this.available, this.sortAvailableListItems);
}

/**
* Update the elements in the selected listbox;
*/
updateSelectedListbox() {
this._updateListbox(this.selectedList, this.selected);
this._updateListbox(this.selectedList, this.selected, this.sortSelectedListItems);
}

//
Expand All @@ -195,11 +198,18 @@ class DualListbox {
/**
* Update the elements in the listbox;
*/
_updateListbox(list, elements) {
_updateListbox(list, elements, sortListItems) {
while (list.firstChild) {
list.removeChild(list.firstChild);
}

if (sortListItems) {
elements.sort(function(a, b) {
if (a.textContent < b.textContent) return -1;
if (a.textContent == b.textContent) return 0;
return 1;
});
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This compare function doesn't necessarily do what it expected:

> a = ['Éclair', 'Strasse', 'Dachshund','Straße', 'sträflich', 'strafe', 'Strafe']
> a.sort(function(a, b) {
...               if (a < b) return -1;
...               if (a == b) return 0;
...               return 1;
...           });
[
  'Dachshund',
  'Strafe',
  'Strasse',
  'Straße',
  'strafe',
  'sträflich',
  'Éclair'
]

Better would be

> a.sort((a, b) => a.localeCompare(b))
[
  'Dachshund',
  'Éclair',
  'strafe',
  'Strafe',
  'sträflich',
  'Strasse',
  'Straße'
]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right. What I did was a quick hack to fit my needs for a project.

What do you think about having the sort funcs as options? I committed such a change as well as another one implementing your idea with the DocumentFragment.

for (let i = 0; i < elements.length; i++) {
let listItem = elements[i];
list.appendChild(listItem);
Expand Down