I am attempting to have Select2 (4.0.0rc1) multi-value select box have preloaded data, but it is not working.
HTML
<select id="sj" class="js-example-basic-multiple form-control" multiple="multiple">
<option value="1">Tickets</option>
<option value="2">PArking</option>
<option value="3">Special Events</option>
<option value="4">Athletics</option>
</select>
JavaScript
$(document).ready(function() {
$("#sj").select2();
$("#example tbody").on("click", "tr",function(){
var defaultData = [{id:1, text:'Tickets'},{id:2,text:'Parking'},{id:3,text:'Athletics'}];
$("#sj").select2({data:defaultData});
});
I want this code to programmatically prepopulate the selected items on click.
EDIT what I'm after: The drop down has a total of N Multi- selectable items , i click on a row , and it passes in 3 options as initially selected, how do i do an initSelection as 3.5.2 did? Think of it this way: I am using the multi select drop down to show attributes associated, if its a sporting event, It may only come with tickets, but a week from now i may want to add parking for this event, but it needs to remember I already had tickets associated with this event
I accomplished this in 3.5.2 doing the following:
initSelection : function (element, callback) {
var id=$(element).val();
if (id!=="") {
$.ajax("index.cfm/tickets/select2get", {
dataType: 'json',
data: {
nd_event_id: $('#nd_event_id').val(),
passes: 'TK,PK,ATH',
},
}).done(function(data) { callback(data); });
}
},
But this will not work in 4.0.0 due to migration from inputs to selects