In a laravel blade page I have a controller sending planTypes
and stored like this:
arr = [];
var planTypes = @json($planTypes);
$('#multiselect').change(function (e) {
var plan_id = $(this).val();
arr.push(plan_id);
});
What it does? For any change in a dropdown listing all planTypes I capture the unique id and store it in a js array (arr
)
The user has the possibility to add multiple planTypes in the same page without the need to refresh and that is why the change
js function
Whenever I render the new dropdown, I would like to only display the remaining unused planTypes and I try to do it like this:
var item=
+' <select id="multiselect" name="planTypes[0][]" class="selectpicker form-control type">'
+' <option value="">--</option>'
@foreach($planTypes as $planType)
if(!arr[{{ $planType->id }}]) {
+' <option value="{{ $planType->id }}">{{ $planType->name }}</option>'
}
@endforeach
But for some strange reasons, I cannot make it work like that, combining blade @foreach and javascript if
Could someone point me to the correct approach?
Thank you!
+’ ‘
2
Answers
Try something like this:
OR
if you want to use multiselect the better option is to user select2 js library