Add fixed item and non-removable item to select2?

Viewed 67

I have a mail module and I want to add the mentioned (in the photo) email or hr@example.com to be fixed selected and non-removable option in cc in a multi selection select2. How may I achieve this? any idea?

Here is my Blade syntax for selecting cc :

<div class="form-group">
    <label>CC <span class="astirered">*</span></label>
    {!! Form::select('cc[]', $employee_list, $mail_cc_user_id, ['class'=>'form-control select2','multiple']) !!}
    <span class="astirered">{!! $errors->first('cc') !!}</span>
</div>

and this is the script of select 2 :

$('.select2').select2({
    placeholder: "Please select one"
});
1 Answers

I dont think select 2 has a built in way for that, but with some additional JQuery it is possible to hide it. If you look the select box with inspector you will find something like this

<li class="select2-selection__choice" title="mni@email.com" data-select2-id="id">
    <span class="select2-selection__choice__remove" role="presentation">×</span>
    mni@email.com
</li>

You could remove that x by jQuery like this:

$('li.select2-selection__choice[title="mni@email.com"]').find('span.select2-selection__choice__remove').remove();

And then for exmaple you could mark that specific email default selected in the form, then run that jQuery command when document loads.

Related