Silvano González
Silvano González

Reputation: 391

What's the proper way to disable ajax cache on select2?

I'm using select2 to load remote data to an html select

The issue with it is that if user selects an option -> clears the input -> data changes in server -> user search for the data again. If I ask for data selected it returns the old (previusly selected) values.

Example:

//User search for an option and selects it 
$('#someSelect').select2('data').Limit 
//Here the limit is 0 
//Server data changes on server. Now limit is 10 
//User searchs again for the same option and selects it 
$('#someSelect').select2('data').Limit 
//The value returned in the line above is 0 again 
//Somehow the data previously selected is cached 
//and returned instead of the refreshed data 

Setting cache:false doesn't do anything.

Upvotes: 1

Views: 2622

Answers (2)

DotNetLover
DotNetLover

Reputation: 229

You can also reset the select2 dropdown to clear the options..

$('#someSelect').select2("val", "All");

Upvotes: 2

Silvano González
Silvano González

Reputation: 391

After hours I figured out the solution.

On the event select2:unselect I had to clear all the options of the select because the plugin has added the selected option to the DOM.

$('#someSelect').on('select2:unselect',function(){
    $('#someSelect').html(null);
});

Upvotes: 2

Related Questions