Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2: default selection in multiple select with AJAX

Tags:

I am using the awesome select2 to create a multiple selection combo.

I would like to programmatically select default values (as in selections the user has made previously), but I don't know how. I read that using initSelection does the trick but this is called when creating the combo and I don't want this default selection to be always done.

like image 651
Gabriel Sanmartin Avatar asked Apr 22 '13 08:04

Gabriel Sanmartin


2 Answers

right solution in version 4.x by example:

$('#element').val(['val1','val2']).trigger('change'); 

see: Creator description

like image 79
Ramin Firooz Avatar answered Sep 23 '22 21:09

Ramin Firooz


I found some solutions, maybe that will help you:

$('#el').select2({...}) // init select2  var defaultData = [{id:1, text:'Item1'},{id:2,text:'Item2'},{id:3,text:'Item3'}];  $('#el').data().select2.updateSelection(defaultData); 
like image 37
sanj Avatar answered Sep 22 '22 21:09

sanj