Show a sorted list of only unique options/values in a select field

Show a sorted list of only unique options/values in a select field

medarobmedarob Posts: 20Questions: 5Answers: 0

For my use case I used the basic example and changed it.

In HTML I added a select field and the options should only be unique values of a certain column. Some entries have multiple values, so the value list has to be split.

https://live.datatables.net/fexiqeno/2/edit

The output of the nameArr is
["Edinburgh"]
["Edinburgh", "Oslo"]
["Edinburgh", "San Francisco"]
["Edinburgh", "Seattle"]
["Edinburgh", "Tokyo"]
["London"]
["New York"]
["New York", "Tokyo"]
["San Francisco"]
["San Francisco", "New York"]
["San Francisco", "Washington"]

Currently, the values in the dropdown are unique but not sorted. Is there a way to sort the options in the select dropdown? It might be possible to sort the option now, after the select element was created but maybe it's possible in datatables to get sorted list with split unique values and without dublicates and use this to create the option-list in the select element?

Replies

  • kthorngrenkthorngren Posts: 22,162Questions: 26Answers: 5,102
    edited July 18

    This is all standard Javascript code not specific to Datatables. Looks like you will need to build the array of offices. Once built get the unique values using Set() then sort the array. The resulting unique sorted array cn be used to build the select list. Something like this:
    https://live.datatables.net/fexiqeno/3/edit

    Kevin

Sign In or Register to comment.