You can't use blade templating in JS files.
Now you have 2 things you can do:
- Move your snippet of javascript from js file to the bottom of the "view.blade.php"
- You can declare in the "view.blade.php" the variables that contain options, and then get it from javascript.
For example:
Your "view.blade.php" would be:
<table width="100%">
<tbody class="input_fields_Pesanan">
<tr>
<td style="width: 15%;" align="center"><b>Kode BMN</b></td>
<td style="width: 15%;" align="center"><b>Nama Barang</b></td>
<td style="width: 15%;" align="center"><b>Kuantitas</b></td>
<td style="width: 15%;" align="center"><b>Satuan Ukuran</b></td>
<td style="width: 2%;"></td>
<tr>
<td>
<select type="text" style="width: 100%;" class="form-control select2" id="kode_bmn" name="kode_bmn" data-placeholder="--Pilih Kode BMN--">
<option></option>
@foreach(
b)
<option>{{$b->kode_bmn}}-{{$b->nama_bmn}}</option>
@endforeach
</select>
</td>
<td><input type="text" class="form-control " id="jenis_barang" name="jenis_barang" ></td>
<td><input type="number" min="1" class="form-control" id="kuantitas" name="kuantitas"></td>
<td>
<select type="text" style="width: 100%;" class="form-control select2" id="satuan_ukuran" name="satuan_ukuran" data-placeholder="--Pilih Satuan Ukuran--">
<option></option>
<option>m</option>
<option>unit</option>
<option>kg</option>
</select>
</td>
<td> <button class="add_field_pesanan" title="Tambah Field Pesanan"><i class="fas fa-plus"></i></button></td>
</tr>
</tbody>
</table>
<script>
var myOptions = '@foreach(
b) <option>{{$b->kode_bmn}}-{{$b->nama_bmn}}</option> @endforeach';
</script>
And then your Javascript file would be:
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper_pesanan = $(".input_fields_Pesanan"); //Fields wrapper
var add_pesanan = $(".add_field_pesanan"); //Add button ID
var x = 1; //initlal text box count
$(add_pesanan).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper_pesanan).append('<tr><td><select type="text" style="width: 100%;" class="form-control select-new-' + x + '" id="kode_bmn" name="kode_bmn" data-placeholder="--Pilih Kode BMN--"><option></option>' + myOptions + '</select></td><td><input type="text" class="form-control " id="jenis_barang" name="jenis_barang" ></td><td><input type="number" min="1" class="form-control" id="kuantitas" name="kuantitas"></td><td><select type="text" style="width: 100%;" class="form-control select-new-' + x + '" id="satuan_ukuran" name="satuan_ukuran" data-placeholder="--Pilih Satuan Ukuran--"><option></option><option>m</option><option>unit</option><option>kg</option></select></td><td> <a href="#" class="remove_pesanan" title="Hapus"><i class="fas fa-trash-alt"></i></a></td></tr>'); // add input boxes.
//instantiate the new select as select2
$('select.select-new-' + x).select2();
}
});
$(wrapper_pesanan).on("click",".remove_pesanan", function(e){
e.preventDefault(); $(this).parent('td').parent('tr').remove(); x--;
});
});
Obviously make sure the snippet in view.blade.php comes before of including your javascript, otherwise you get a undefined from javascript.
Answer from Bacca001 on Stack Overflowphp - Use foreach loop in javascript in laravel - Stack Overflow
php - How to create javascript function in laravel foreach loop - Stack Overflow
javascript - How to do foreach in JS using data from laravel views? - Stack Overflow
php - Using javascript function on foreach loop in laravel blade - Stack Overflow
Solution from @Aless55 works perfectly fine
<script>
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
theme: "light2",
title:{
text: "Simple Line Chart"
},
data: [{
type: "line",
indexLabelFontSize: 16,
dataPoints: [
@foreach($records as $prc)
{ y: {{$prc->price}} },
@endforeach
]
}]
});
chart.render();
}
</script>
Try this piece :
<script>
window.onload = function () {
var records = "<?php echo $records ?>"
var data = records.map((function (record) {
return {y: record.price}
}))
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
theme: "light2",
title:{
text: "Simple Line Chart"
},
data: [{
type: "line",
indexLabelFontSize: 16,
dataPoints: data
}]
});
chart.render();
}
</script>
you must get the all data and make it into Api i make my Javascript like this
async function getAllMarker() {
map.setZoom(8);
const res = await fetch("/api");
const data = await res.json();
data.forEach((item) => {
const position = {
lat: Number(item.latitude),
lng: Number(item.longitude),
};
addMarker(position, map, item);
});
}
for Add Marker my Function Look Like This
function addMarker(position, map, item){
L.marker(position).addTo(map)
.bindPopup(item.marker)
.openPopup();
}
First, modify your JS function to something similar to this:
function addMarker(lat, lng, place){
L.marker([lat, lng]).addTo(map)
.bindPopup(place)
.openPopup();
}
For old versions of laravel:
<script>
var coordinates = <?php echo json_encode($coordinates); ?>;
</script>
For latest versions of laravel:
<script>
var coordinates = {{ Js::from($coordinates) }};
</script>
Then Loop through the array :
<script>
coordinates.foreEach(function(item){
addMarker(item.lat, item.lng, item.place);
});
</script>
In this case, as the HTML you want to append is governed by the content coming from your Laravel back end, it would make far more sense to de-couple the HTML and the JS. As such, create the HTML in the DOM when the page loads and hide it. Then in the JS you can clone that HTML and append it where it is required to be displayed. Something like this:
$('.add-new-product').on('click', function() {
var $clone = $('#new-product-content > div').clone();
$clone.appendTo('#products-div');
});
#new-product-content {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="add-new-product">Add New<i class="fa fa-plus"></i></span>
<div id="products-div"></div>
<div id="new-product-content">
<div class="col-xs-12">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon input-group-addon-small"><i class="sicon-tag-special"></i></span>
<select class="form-control" name="category_id" style="width: 100%">
<option value="">Select Category...</option>
<!--
output your categories here, eg.
@foreach($categories as $category)
<option value="{{ $$category->id }}">{{ $category->name }}</option>
@endforeach
Below is just for demo purposes... -->
<option value="lorem">Lorem</option>
<option value="ipsum">Ipsum</option>
<option value="dolor">Dolor</option>
<option value="sit">Sit</option>
</select>
</div>
</div>
</div>
</div>
Also note the use of unobtrusive event handlers here. Using inline event handlers in your HTML, such as onclick etc., is bad practice and should be avoided where possible.
You could do something like this:
function createProduct() {
// Echo the JSON for the options in your JS
var options = @json($categories);
// Map options to their HTML version, and join them
var optionsHtml = options.map(function (opt) {
return '<option value="' + opt.id + '">' + opt.name + '</option>';
}).join('');
var html = ' <div class="col-xs-12">'
+ ' <div class="form-group">'
+ ' <div class="input-group">'
+ ' <span class="input-group-addon input-group-addon-small"><i class="sicon-tag-special"></i></span>'
+ ' <select class="form-control" name="category_id" style="width: 100%">'
+ ' <option value="">Select Category.. </option>'
+ optionsHtml // Add them here
+ ' </select>'
+ ' </div>'
+ ' </div>'
+ ' </div>';
$('#products_div').prepend(html);
}