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

clearOptions should not clear items

Open ghost opened this issue 11 years ago • 10 comments

The clearOptions() method results in the clear() method also being called. Given the logical distinction between "options" and "items" this seems incorrect. The behavior also prevents (unless I'm missing another approach) the use case of providing a new set of options for each item, as follows: $('#select-state').selectize({ load: function(query, callback){ this.clearOptions(); //fetch the new options } });

ghost avatar Oct 11 '14 00:10 ghost

But couldn't your fix clear the input-field if the query was really fast or really slow? I use the onType to clear the query cache. And more or less the same code for onClear and onItemRemove. This code is for an older version of selectize, so you might not need all the lines anymore and could probably be optimized even more.

$('#select-state').selectize({
    onType: function(){
        ajaxquery && ajaxquery.abort();
        this.$input[0].selectize.renderCache = {};
        this.$input[0].selectize.clearOptions();
        this.$input[0].selectize.refreshOptions(true);
    },
    load: function(query, callback) {
        if (!query.length) return callback();
        ajaxquery && ajaxquery.abort();
        ajaxquery = $.ajax({
            url: '/?q=' + encodeURIComponent(query),
            type: 'GET',
            dataType: 'json',
            error: function() {
                callback();
            },
            success: function(res) {
                callback(res);
            }
        });
    }
});

flamber avatar Nov 03 '14 12:11 flamber

This is nonsense why it's still open. You should just remove .clear() at the end of the function and it just works as expected.

dresende avatar Sep 25 '18 11:09 dresende

Has this issue been fixed? if not then how can we implement clearOptions with a hacky way so that our options will be cleared without being cleared the selected input option? If Somebody can help then it would be really appreciated.

dipbazz avatar Aug 01 '20 09:08 dipbazz

According to this changelog: Fixed bug making clearOptions function. Now it doesn't remove already selected options.

(thanks @caseymct - #1079)

But it doesn't seem to work. Also, silent is implemented and doesn't see to work too.

jicihome avatar Dec 30 '20 19:12 jicihome

I can confirm for silent. Not working for add item

jicihome avatar Mar 30 '21 17:03 jicihome

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

github-actions[bot] avatar Jun 29 '21 01:06 github-actions[bot]

This is wrong proposition. clearOptions must call clear otherwise data will be inconsistent.

For example you have list 1,2, user selected 7. Is this expected? No, because there is no 7 on the list.
Same with clearOptions. If options are cleared, you can choose nothing, thus selected list also must be cleared.

KES777 avatar Jun 21 '23 16:06 KES777