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

Disable automatic sort

Open eliashdezr opened this issue 12 years ago • 43 comments

Hi, I'm receiving a pre-ordered list (JSON) but when I loaded into the selectize control, it re-orders the list by the Id and I don't want this. How I can let selectize use the order of my JSON?

This is the JSFiddle example: http://jsfiddle.net/LYYab/

Thanks.

eliashdezr avatar Dec 10 '13 22:12 eliashdezr

I would also like to be able to disable automatic sorting. It's really, really frustrating that Selectize automatically messes with the order of options that are added to it.

Is there no way to disable autosort?

erlingur avatar Feb 18 '14 16:02 erlingur

@eliashdez @erlingur Remove sortField: "text" from the selectize options to disable the auto sorting.

antsa avatar Feb 19 '14 07:02 antsa

@antsa I don't have that. I use the default configuration of $("#element").selectize() and it still does the auto sorting.

erlingur avatar Feb 19 '14 07:02 erlingur

And you are using 0.8.5? Without the sortField option it works as expected for me with just a selectelement with options inside of it.

The example of @eliashdez seemed to work differently with loading the data from JSON, where sorting happened. Here the trick seemed to be to set the sortField: "Id"

antsa avatar Feb 19 '14 08:02 antsa

@antsa Yes. Here is a jsfiddle demonstrating the problem: http://jsfiddle.net/8H62B/6/

When you first load the page with

On that jsfiddle, if you begin by looking at the options, then click the button and look at the options again you will see what I mean.

erlingur avatar Feb 19 '14 08:02 erlingur

@erlingur Apparently when adding options it does sorting and doesn't use the original order of the data—would it work for you to set the order with e.g. sortField: "text" in that case?

When calling selectize on a existing select with options leaving that option out will keep the original order of the option elements.

antsa avatar Feb 19 '14 08:02 antsa

@antsa sortField: 'text' will sort the options based on their text right? That wouldn't work for me since I have presorted options that are not really based on either the value or the name. I would ideally need addOptions() to respect the insert order. I'm looking at it now, trying to make it do that.

erlingur avatar Feb 19 '14 08:02 erlingur

So I created a pull request with a proposed fix. What do you think @antsa?

erlingur avatar Feb 19 '14 09:02 erlingur

I need it too

ai avatar Apr 17 '14 16:04 ai

Hey @ai, I created a pull request for this here: https://github.com/brianreavis/selectize.js/pull/295

You can use this branch if you want this functionality now: https://github.com/Reiknistofa/selectize.js/tree/respect_insert_order_in_addoption

erlingur avatar Apr 17 '14 17:04 erlingur

:+1:

dbpolito avatar Jun 03 '14 00:06 dbpolito

+1

ibtjw avatar Jun 11 '14 13:06 ibtjw

+1

dhiemstra avatar Jul 02 '14 13:07 dhiemstra

+1

chriserik avatar Jul 22 '14 13:07 chriserik

:+1: will this be merged?

xjunior avatar Jul 23 '14 18:07 xjunior

+1

raicheff avatar Aug 06 '14 12:08 raicheff

With the current most common model with API's serving multiple front ends, it's rather common to have dynamic elements. It's also common that the API will provide these elements in the correct sorting, making the current automatic sorting not just a deprecated feature, but seen as a bug for most of the users.

Thanks for fixing it.

xjunior avatar Aug 06 '14 13:08 xjunior

@xjunior No problem! Yeah, seems like something a lot of people would like.

This should still merge cleanly with the latest version. Hopefully @brianreavis will chime in here with his thoughts.

erlingur avatar Aug 06 '14 14:08 erlingur

I solved this problem by setting the following option when initializing the selectize object... Works with my pre-ordered JSON data!

score: function() { return function() { return 1 }; }

freefallfred avatar Aug 25 '14 14:08 freefallfred

@freefallfred Like this? http://jsfiddle.net/a9ttjt7v/ Doesn't fix it for me.

erlingur avatar Aug 25 '14 17:08 erlingur