-
Notifications
You must be signed in to change notification settings - Fork 0
/
select2ajaxexample.js
49 lines (41 loc) · 1.18 KB
/
select2ajaxexample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
$(document).ready(function () {
initView();
function initView() {
addSelect2();
bindUpdateListener();
}
function addSelect2() {
$.fn.select2.defaults.set( "theme", "bootstrap" );
$('#select2-dropdown').select2({
ajax : {
url: 'https://opendata.rijksoverheid.nl/v1/sources/rijksoverheid/infotypes/news?output=json',
dataType: 'json',
contentType: 'json',
processResults: function(data) {
return {
results: data
}
}
},
placeholder: 'Selecteer een optie...',
allowClear: true,
language: "nl",
width: null,
templateResult: formatResult, // tell Select2 how results should be displayed in the dropdown
templateSelection: formatSelection // tell Select2 how selected results should be displayed in dropdown
});
}
function formatResult (result) {
return result.title;
}
function formatSelection (result) {
return result.title;
}
function bindUpdateListener() {
$('#select2-dropdown').on("select2:select", function (e) {
JSON.stringify(e.params, function(key, value) {
$('#article').html(value['data'].introduction);
})
});
}
});