selectize.js icon indicating copy to clipboard operation
selectize.js copied to clipboard

Avoid automatic sort options

Open olegsobchuk opened this issue 8 years ago • 3 comments

I try to use selectize dynamically via AJAX So I fetch the needed data and then push them to selectize. But my data have been ordered already and I don't change order. So I use next code

         data # array with payload
         new_opts = []
          $.each data.recipients, (_, value) ->
            new_opts.push({text: value[1], value: value[0]})

          selectize = $('#user_ids')[0].selectize
          selectize.clear()
          selectize.clearOptions()
          selectize.renderCache['option'] = {}
          selectize.renderCache['item'] = {}
          selectize.addOption(new_opts)
          selectize.refreshOptions

How can I avoid sorting process inside selectize ?

Thx

olegsobchuk avatar Aug 06 '18 17:08 olegsobchuk

There is a sortField attribute in the selectize settings object which can be removed to disable sorting of options.

gsimore avatar Oct 16 '18 23:10 gsimore

As far as I understand the sorting, the sort order of each option is determined by the score() function every time the search term is changed (i.e. while typing) and mostly is a measure of how well the option matches the search term.

The only way I found to get the options sorted by a pre-existing sort field was to override the score() callback like seen in the GitHub example

https://github.com/selectize/selectize.js/blob/5b06be3d5726c89ed1dbf2ff22eb94b9f2bc60ff/examples/github.html#L129-L134 Here you see that while the result of the original score() function is being used, the resulting score relies more heavily on the "watchers" field of each option ("item" in the example).

Note that the example doesn't even use the sortField option.

h0lg avatar Nov 18 '19 16:11 h0lg

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Jan 01 '21 02:01 github-actions[bot]