In a laravel blade page I have a controller sendingplanTypes
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 thechange
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! +' '
Try something like this:
<script>
const planTypes = {!! json_encode($planTypes) !!};
// use this HTML however you want
let html = `
<select id="multiselect" name="planTypes[0][]" class="selectpicker form-control type">'
<option value="">--</option>
${planTypes.map(planType => `<option value="${planType.id}">${planType.name}</option>`)}
</select>
`;
<script>
var planTypes = {!! json_encode($planTypes) !!};
var filter_planTypes = planTypes;
$(document).on('change', '#multiselect', function () {
var plan_id = $(this).val();
filter_planTypes = filter_planTypes.filter(function(val) {
return val.id != plan_id;
});
var options = '<option value="">Select</option>';
$.each(filter_planTypes, function (key, value) {
options += '<option value="'+ value.id +'">'+ value.name +'</option>';
});
$('#multiselect').html(options);
$('#multiselect').trigger("change");
});
</script>
OR if you want to use multiselect the better option is to user select2 js library
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<select id="multiselect" name="planTypes[]" class="selectpicker form-control type" multiple="multiple">
<option value="">--</option>
@foreach($planTypes as $planType)
<option value="{{ $planType->id }}">{{ $planType->name }}</option>
@endforeach
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<script>
$(document).ready(function() {
// Select2 Multiple
$('#multiselect').select2({
placeholder: "Select",
allowClear: true
});
});
</script>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.