Set Some Specific Options in Multiselect as Disabled and Selected

Disable a specific option boostrap-multiselect

Just a quick note to clarify: disabling the corresponding checkbox and adding the .disabled class to the appropriate li is not sufficient. You also have to disable the underlying option. Example (can also be found in the documentation):

<script type="text/javascript">
$(document).ready(function() {
$('#example-disable-javascript').multiselect({
includeSelectAllOption: true
});

$('#example-disable-javascript-disable').on('click', function() {
var input = $('#example-disable-javascript-container input[value="3"]');
var option = $('#example-disable-javascript-container option[value="3"]');

input.prop('disabled', true);
option.prop('disabled', true);

input.parent('label').parent('a').parent('li').addClass('disabled');
});

$('#example-disable-javascript-check').on('click', function() {
var options = '';
$('#example-disable-javascript option:selected').each(function() {
options += $(this).val() + ', ';
});

alert(options.substr(0, options.length - 2));
});
});
</script>
<div class="btn-group" id="example-disable-javascript-container">
<select id="example-disable-javascript" multiple="multiple">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
</select>
<button id="example-disable-javascript-disable" class="btn btn-primary">Disable an option!</button>
<button id="example-disable-javascript-check" class="btn btn-primary">Check</button>
</div>

JQuery : Disable multiple <option> on multiple <select> based on array of selected values

When looping all the options, if the option is not the selected one, you need to re-enable it.

const $selects = $(".products-combo");$selects.on('change', function(evt) {    // create empty array to store the selected values    const selectedValue = [];    // get all selected options and filter them to get only options with value attr (to skip the default options). After that push the values to the array.    $selects.find(':selected').filter(function(idx, el) {        return $(el).attr('value');    }).each(function(idx, el) {        selectedValue.push($(el).attr('value'));    });    // loop all the options    $selects.find('option').each(function(idx, option) {         // if the array contains the current option value otherwise we re-enable it.        if (selectedValue.indexOf($(option).attr('value')) > -1) {            // if the current option is the selected option, we skip it otherwise we disable it.            if ($(option).is(':checked')) {                return;            } else {                $(this).attr('disabled', true);            }        } else {            $(this).attr('disabled', false);        }    });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><select class="products-combo" name="product[]">  <option>Select 1</option>  <option value="1">Product 1</option>  <option value="2">Product 2</option>  <option value="3">Product 3</option>  <option value="4">Product 4</option>  <option value="5">Product 5</option></select><select class="products-combo" name="product[]">  <option>Select 2</option>  <option value="1">Product 1</option>  <option value="2">Product 2</option>  <option value="3">Product 3</option>  <option value="4">Product 4</option>  <option value="5">Product 5</option></select><select class="products-combo" name="product[]">  <option>Select 3</option>  <option value="1">Product 1</option>  <option value="2">Product 2</option>  <option value="3">Product 3</option>  <option value="4">Product 4</option>  <option value="5">Product 5</option></select>

How to disable 2 options in multiselect dropdown and grayout that option

I would recommend instead of editing the UI with jQuery to modify the user[] that is visualized in the *ngFor and add a field called disabled. Then in your template you can do the following

 <select name="user" id="multiselectUser" multiple="multiple" (change)="selecteduser($event)" [(ngModel)]="selectedUsers" >
<option *ngFor="let field of user" [disabled]="field.disabled" [value]="field.id" >
{{field.label}}</option>
</select>

And your typescript should be changed like so

// From

   setTimeout(() => {
$('#assignedRoles option[value="124"]').prop("disabled", true);
$('#assignedRoles option[value="123"]').prop("disabled", true);
});

// To

   setTimeout(() => {
this.user = this.user.map(x => {
return {
...x,
disabled: [123, 124].includes(x.id)
};
});

Here is also a demo on stackBlitz (im using your example as base)

// Answer for the comments
You can add a custom class like so and apply whatever styles you need

    <option *ngFor="let field of user" [ngClass]="{'disabled-option': field.disabled}"  [disabled]="field.disabled" [value]="field.id" >
{{field.label}}</option>

And in order to enable the options, you just have to iterate over the fields again and change the disabled state

** Important

Because you are using third party linrary for the select you must add you styles in the root .css files for them to take effect.

Also because of the library you are using you should re-initialized the select component in order for it to re-render the options with their new state.

Look again at the stackblitz i had provided

How to disable a specific option in select tag using chosen jquery

You can detect the change in the value, and iterate on all other options in case red is selected to disable them using .attr("disabled","disabled"); as follows:

$(document).ready(function(){     $('select').on('change', function(){          if(this.value == 'red'){               $('select option').map(function() {                    if($(this).val()!='red')                         $(this).attr("disabled","disabled");               });          }     })})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select>     <option>red</option>     <option>blue</option>     <option>yellow</option>     <option>green</option>     <option>whi te</option></select>

Disable an option of multiple select options

Have a go with this. It was not trivial

Remember JS numbers arrays and collections from 0, but I store the values in the selected array

const pairs = [, 5, 6, , 1]; // 1=5, 2=6 5:1$(function() {  $(".selectpicker").selectpicker()  $(".selectpicker").on("changed.bs.select", function(e, clickedIndex, newValue, oldValue) {    // const selected = $("option:selected", this).map((i, ele) => ele.value).get();    $("option", this).prop("disabled", false); // enable all    $("option:selected", this).each(function() { // disable selected pair      const paired = pairs[$(this).val()];      if (paired) {        // if value i is among the selected, disable value pairs[i]        console.log("value", this.value, "disable", paired)        $(".selectpicker").find('option[value=' + paired + ']')          .prop("selected", false)          .prop("disabled", true);      }    });    // refresh the select    $('.selectpicker').selectpicker('refresh');  });});
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script><script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script><script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<select id="sel1" class="selectpicker" multiple> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="4">Four</option> <option value="5">Five</option> <option value="6">Six</option> <option value="7">Seven</option></select>

jQuery <select> option disabled if selected in multiple <select> boxes

This should do it. Basically does what your comments ask - ensures that all 3 selects have unique selections. Once a selection is made in 1 selext box that option is no longer available in the others.

$('select[name*="homepage_select"]').change(function(){


// start by setting everything to enabled
$('select[name*="homepage_select"] option').attr('disabled',false);

// loop each select and set the selected value to disabled in all other selects
$('select[name*="homepage_select"]').each(function(){
var $this = $(this);
$('select[name*="homepage_select"]').not($this).find('option').each(function(){
if($(this).attr('value') == $this.val())
$(this).attr('disabled',true);
});
});

});

Live example: http://jsfiddle.net/QKy4Y/



Related Topics



Leave a reply



Submit