I want to create a dynamic select2 dropdown based on the array value and disable the options in dropdown. When i select the option in current select box the option will be disabled in other select box drop down. Here is my code
var selectArray = [{id: 0, value: 'test'}, {id: 1, value: 'test1'},{id: 2, value: 'test2'}, {id: 3, value: 'test3'},{id: 4, value: 'test4'}, {id: 5, value: 'test5'}];
var data = [{id:1, text: 'value1'},{id:2, text: 'value2'},{id:3, text: 'value3'},{id:4, text: 'value4'},{id:5, text: 'value5'}];
$.each(selectArray, function( index, value ) {
var html = '<li class="m-b-20"><select id="select_id_'+ value.id +'" class="select2-option"></select></li>';
$('#selectbox').append(html);
});
$(".select2-option").select2({
minimumResultsForSearch: -1,
data: data
});
ul li {
list-style: none;
}
select {
width: 200px;
}
.m-b-20 {
margin-bottom: 20px;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>
</head>
<body>
<div>
<ul id="selectbox">
</ul>
</div>
</body>
</html>
Can Any one please help me?