Answers for "select2 ajax selected value"

4

select2 server side

$('#payment_method').select2({
	placeholder: 'Select a option',
	ajax: {
			url: 'ajax_get_url',
            dataType: 'json',
            type: 'GET',
            processResults({ data }) {
				return {
					results: $.map(data, function (item) {
						return {
							text: item.name,
                            id: item.id,
                            }
						})
					}
				}
			}
		});
Posted by: Guest on July-16-2020
0

select2 ajax selected value

//YOUR AJAX SELECT2
$("#myselect2").select2({
    dropdownParent: $('#test_modal'),
    width: '100%',
    placeholder: 'Seach',
    ajax: {
        url: "<?=base_url('dashboard/api/v1/dropdown')?>",
        dataType: 'json',
        delay: 250,
        processResults: function(data) {
            return {
                results: data
            };
        },
        cache: true
    }
});


// TRIGGER EDIT AND SET THE VALUE FOR SPECIFIC MODAL / FORM
$(document).on('click', '.edit_button', function() {
    var something_id = $('#something_id').val();
    $.ajax({
        url: "<?php echo base_url('dashboard/api/v1/page/edit');?>" + "/" + something_id,
        method: 'POST',
        dataType: 'json',
        success: function(data) {
            if (data.id) {
                // Set selected   
                var $newOption = $("<option selected='selected'></option>").val(data.id).text(data.text);
                $("#myselect2").append($newOption).trigger('change');
            } else {
                $('#myselect2').val(null).trigger('change');
            }
        }
    });
});
Posted by: Guest on October-01-2021

Code answers related to "select2 ajax selected value"

Code answers related to "Javascript"

Browse Popular Code Answers by Language