gatanaso/multiselect-combo-box-flow

Sort selectedItems

lipisak opened this issue ยท 13 comments

Hi,
I use your component as selector of Days of week. In selection dialog it's ok - values are sorted as on input. When I select item these values are not correctly sorted. Guess it's because of Set (not sorted). I am attaching an image where incorrect sorting is visible. setOrdered(BOOLEAN) doesn't take any effect.

I am using version for Vaadin 14 - 2.5.0

Enum is created from English values (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY) and caption generator is used for translations

SORTING_ISSUE

Hi @lipisak,

It could be related to the caption generator, but I can't say for certain. The "ordered" example in the demo https://multiselect-combo-box-flow.herokuapp.com seems to be working okay.

Any chance you can provide a minimal reproducible project with the issue?

Thank you,
Goran

Hi @gatanaso,
I am attaching a Java file - actually it's kind of wrapper - because we need values of the filed to be in list not in set. Whenever we use it during selection it changes the order of selected items (should be in order of initial list). When we set initial selected items it is also set incorrectly. The strange thing is that it is no alphabetically neither in Czech nor in English. It is not sorted according to provided Items. I would guess it's caused by internal HashSet representation, but I haven't dig too deep into it.

MultiComboBoxField.java.zip

Hi @lipisak and thanks for sharing the file. Unfortunately I can't really run or use that to see how it behaves, as it's missing other internal classes from your project.

Are you able to reproduce the issue with the vanilla MultiselectComboBox and not in the custom wrapper field that you have shared?

Hi @gatanaso,
I did some research and I found following. There is private static <T> Set<T> presentationToModel method in MultiselectComboBox. HashSet implementation which is used doesn't keep the order of items returned from client side. As it maps the values correctly to model values and then add it to set. But it doesn't keep the order returned by client. Wouldn't it be better to use LinkedHashSet instead to keep the order from presentation to model layer?

L.

Hi @lipisak,

Yeah, though the ordered demo from https://multiselect-combo-box-flow.herokuapp.com works perfectly fine without any issues, even with the current presentation to model behavior and the sorting being done on the client-side.

Hi @gatanaso

this is the most simplified version of the usage I can create :) The MultiComboBoxField must be preserved as it behaves as wrapper Set<->List.

https://github.com/lipisak/multiselectcombo-vaadin14-minimalRunnable

This project uses only vaadin and your component - no 3rd parties.

Thank you
J.

Oh, I think I've understood the issue wrongly from the beginning ๐Ÿ˜„ Yes, you are right. If you use set ordered, the selected items will be sorted alphabetically on the client. If you omit it, then the value order is lost due to the internal data representation.

Guess it doesn't matter in my example if you set ordered true or not (on my wrapper which passes the value to your component). The items are incorrectly set anyway :) If I understand ordered feature correctly - it is used on client side. The server only gets the values from client side, maps it to server representation and returns them in the Set. Which unfortunatelly can be ordered differently - HashSet vs LinkedHashSet :) I don't even know how it's possible that your examples work.

Just to clarify one more time. Your use case is such that the selected items (tokens/chips) always need to be sorted based on how they appear in the drop-down list, irregardless of the order in which those were selected?

For example, if the list of drop-down items is: "Mon, Tue, Wed, Thu, Fri, Sat and Sun". And nothing is selected. You open the drop-down and select: "Wed, Sat and Mon", then the expectation is that the selected items (tokens/chips) will be in the following order: "Mon, Wed and Sat" (they will appear as such in the input field)?

If this assumption is correct, unfortunately, this is not currently supported and would need to be implemented separately.

This is exactly the example I should provide you :-D It would be very helpful for us, if this feature would be possible.
We use it for months, days, etc. For users it's very confusing when they select days that they are not in "logical" order.

Hi @gatanaso,
If you guide me some way we can create pull request for this, but I am not sure, what should be changed. J

Thanks, I will have to think about it as well. Not sure what would be the best approach. I am open to suggestions in case you have any :)

Hi @lipisak,

One way you can temporarily work around this limitation, is by overriding the client side sorting:

        MultiselectComboBox<String> multiselectComboBox = new MultiselectComboBox<>();
        multiselectComboBox.setItems("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
        multiselectComboBox.setOrdered(true);
        multiselectComboBox.getElement().executeJs(""
            + "const _sorter = {'monday': 1, 'tuesday': 2, 'wednesday': 3, 'thursday': 4, 'friday': 5, 'saturday': 6, 'sunday': 7};"
            + "$0._sortSelectedItems = (selectedItems) => {"
            + "    selectedItems.sort((item1, item2) => {"
            + "        const item1Str = String(item1['label']).toLowerCase();"
            + "        const item2Str = String(item2['label']).toLowerCase();"
            + "        return _sorter[item1Str] - _sorter[item2Str];"
            + "    });"
            + "};"
        );

The above code will ensure that the list of selected items is sorted according to the given sorter object values and fits your use case ๐Ÿ™‚

Hope this helps,
Goran