Select2 & Bootstrap modal issue setting selected option

Viewed 409

I have researched a lot of answers to this issue and still couldn't make it work.

I need to set a value into a select element as the unique option displayed and selected when the user clicks a button "Edit Record" for each table row.

The value is correctly set in the table row, but the problem is when the user clicks the "Edit Record" button.

This is the JS script for button click:

$(".edit-record").click(function () {
               //get data from table row
               var megaagent_rainmaker_id  = $(this).parent().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().text();
      
               //assign to value for select element inside modal
               $("#megaagent_rainmaker_select").val(megaagent_rainmaker_id);
               //open modal
               $("#kt_modal_megaagent").modal();
           });
           
<div class="col-lg-12">     
   <div class="form-group validated">
       <label class="form-control-label is-invalid">{{ __('pages/administration/structure.labels.administration.megaagents.index.table.header.rainmaker') }}</label>
           <select class="form-control kt-select2" id="megaagent_rainmaker_select" name="param" multiple="multiple" style="width: 100%"></select>
   </div>
</div>

This is the code for select2 initialization:

// Class definition
var rainmakerSelect = function () {
    // Private functions
    var demos = function () {
        // CSRF Token
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        // multi select
        $('#megaagent_rainmaker_select').select2({
            language: "es",
            placeholder: "Escriba y seleccione",
            allowClear: true,
            maximumSelectionLength: 1,
            ajax: {
                url: '/admin/megaagent/getrainmaker',
                type: "get",
                dataType: 'json',
                delay: 150,
                data: function (params) {
                  return {
                    _token: CSRF_TOKEN,
                    search: params.term // search term
                  };
                },
                processResults: function (response) {
                  return {
                    results: response
                  };
                },
                cache: true
              }
        });

    }

    // Public functions
    return {
        init: function () {
            demos();
        }
    };
}();

// Initialization

jQuery(document).ready(function () {
    rainmakerSelect.init();
});

This is the modal HTML code:

<!--begin::Modal-->
<div class="modal fade" id="kt_modal_megaagent" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">
                    {{ __('pages/administration/structure.labels.administration.megaagents.newmegaagent') }}
                </h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                </button>
            </div>
            <div class="modal-body">
                <form class="kt-form" id="megaagent-form">
                    @csrf
                    <div class="kt-portlet__body">
                        <div class="form-group row">
                            <div class="col-lg-12">     
                                <div class="form-group validated">
                                    <label class="form-control-label is-invalid">{{ __('pages/administration/structure.labels.administration.megaagents.index.table.header.rainmaker') }}</label>
                                        <select class="form-control kt-select2" id="megaagent_rainmaker_select" name="param" multiple="multiple" style="width: 100%"></select>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="kt-portlet__foot">
                        <div class="kt-form__actions text-center">
                            <button type="submit" class="btn btn-primary btn-submit">{{ __('general.buttons.send') }}</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">{{ __('general.buttons.close') }}</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!--end::Modal-->

Regards

1 Answers

You are changing the value of the select, but select needs to know that the value has changed. To do this you simply have to call .change() (or .trigger('change') if you prefer that).

This should do the trick:

$("#megaagent_rainmaker_select").val(megaagent_rainmaker_id).change();

I also recommend finding a better way to retrieve the value than

var megaagent_rainmaker_id  = $(this).parent().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().prev().text();

Any change to your DOM is going to blow that up. You might want to look at data attributes or selecting by nearest specified class or something along those lines.

Related