Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Update docs.md #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pages/12.programmatic-control/01.add-select-clear-items/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ $.ajax({

Notice that we manually trigger the `select2:select` event and pass along the entire `data` object. This allows other handlers to [access additional properties of the selected item](/programmatic-control/events#triggering-events).

### Preselecting options in an remotely-sourced (AJAX) Select2 with multiple values
```
$('.studentSelect').select2({
ajax: {
url: '/api/students',
},
});
// Fetch the preselected item, and add to the control
var studentSelect = $('.studentSelect');
$.ajax({
type: 'GET',
url: '/api/students/s/' + student_id
}).then(function (data) {
console.log(data);
// create the option and append to Select2
data.results.forEach(function(d) {
var option = new Option(d.full_name, d.id, true, true);
studentSelect.append(option).trigger('change');
});

// manually trigger the `select2:select` event
studentSelect.trigger({
type: 'select2:select',
params: {
data: data
}
});
});
```

## Clearing selections

You may clear all current selections in a Select2 control by setting the value of the control to `null`:
Expand Down