1$('#payment_method').select2({
2 placeholder: 'Select a option',
3 ajax: {
4 url: 'ajax_get_url',
5 dataType: 'json',
6 type: 'GET',
7 processResults({ data }) {
8 return {
9 results: $.map(data, function (item) {
10 return {
11 text: item.name,
12 id: item.id,
13 }
14 })
15 }
16 }
17 }
18 });
1//YOUR AJAX SELECT2
2$("#myselect2").select2({
3 dropdownParent: $('#test_modal'),
4 width: '100%',
5 placeholder: 'Seach',
6 ajax: {
7 url: "<?=base_url('dashboard/api/v1/dropdown')?>",
8 dataType: 'json',
9 delay: 250,
10 processResults: function(data) {
11 return {
12 results: data
13 };
14 },
15 cache: true
16 }
17});
18
19
20// TRIGGER EDIT AND SET THE VALUE FOR SPECIFIC MODAL / FORM
21$(document).on('click', '.edit_button', function() {
22 var something_id = $('#something_id').val();
23 $.ajax({
24 url: "<?php echo base_url('dashboard/api/v1/page/edit');?>" + "/" + something_id,
25 method: 'POST',
26 dataType: 'json',
27 success: function(data) {
28 if (data.id) {
29 // Set selected
30 var $newOption = $("<option selected='selected'></option>").val(data.id).text(data.text);
31 $("#myselect2").append($newOption).trigger('change');
32 } else {
33 $('#myselect2').val(null).trigger('change');
34 }
35 }
36 });
37});