最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Use Foreach Loop in Javascript with Laravel 5.3 - Stack Overflow

programmeradmin1浏览0评论

I want to make a button to add new item row :

This is the HTML in create.blade.php

            <div class="createOrderForm" id="orderMenuItems">
            
                <div class="orderItem">
                    
                    <label> Item : </label>
                    <select name="item[]" required>

                        <option value="">Menu Items</option>

                        @foreach ($menuItems as $item)
                        <option value="{{$item->id}}">{{$item->name}}</option>
                        @endforeach

                    </select>
                    
                    <label>Quantity :</label>
                    
                    <input  type="text" name="quantity[]" value="" required>
                
                </div>
            
            </div>

and this is create.js that's included :

$(document).ready(function () {
console.log("Wele To create order Page");


var addItem = $('#addItem');
var orderMenuItems = $('#orderMenuItems');



$(addItem).click(function(e){
   
    var newItem = '<div class="orderItem">';
                    
    newItem += '<label> Item : </label>';

    newItem += '<select name="orderItem[]">';

    newItem += '<option value="">Menu Items</option>';

    newItem += "@foreach ($menuItems as $item)";

    var itemID = "{!! $item->id !!}";
    
    newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';

    newItem += "@endforeach";

    newItem += '</select>';
    
    newItem += '<label>Quantity :</label>';
    
    newItem += '<input  type="text" name="quantity[]" value="" required>';
                
    newItem += '</div>';
    
    $(orderMenuItems).append(newItem);
                
});
});

The button works and adds new row, but there is a problem with the menu items in the new row :

I think the problem is here :

newItem += "@foreach ($menuItems as $item)";

    var itemID = "{!! $item->id !!}";
    
    newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';

    newItem += "@endforeach";

I tried to use :

@foreach ($menuItems as $item)
....
@endforeach

instead of :

newItem += "@foreach ($menuItems as $item)";
....
newItem += "@endforeach";

but it doesn't work.

How Can I fix it ??

I want to make a button to add new item row :

This is the HTML in create.blade.php

            <div class="createOrderForm" id="orderMenuItems">
            
                <div class="orderItem">
                    
                    <label> Item : </label>
                    <select name="item[]" required>

                        <option value="">Menu Items</option>

                        @foreach ($menuItems as $item)
                        <option value="{{$item->id}}">{{$item->name}}</option>
                        @endforeach

                    </select>
                    
                    <label>Quantity :</label>
                    
                    <input  type="text" name="quantity[]" value="" required>
                
                </div>
            
            </div>

and this is create.js that's included :

$(document).ready(function () {
console.log("Wele To create order Page");


var addItem = $('#addItem');
var orderMenuItems = $('#orderMenuItems');



$(addItem).click(function(e){
   
    var newItem = '<div class="orderItem">';
                    
    newItem += '<label> Item : </label>';

    newItem += '<select name="orderItem[]">';

    newItem += '<option value="">Menu Items</option>';

    newItem += "@foreach ($menuItems as $item)";

    var itemID = "{!! $item->id !!}";
    
    newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';

    newItem += "@endforeach";

    newItem += '</select>';
    
    newItem += '<label>Quantity :</label>';
    
    newItem += '<input  type="text" name="quantity[]" value="" required>';
                
    newItem += '</div>';
    
    $(orderMenuItems).append(newItem);
                
});
});

The button works and adds new row, but there is a problem with the menu items in the new row :

I think the problem is here :

newItem += "@foreach ($menuItems as $item)";

    var itemID = "{!! $item->id !!}";
    
    newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';

    newItem += "@endforeach";

I tried to use :

@foreach ($menuItems as $item)
....
@endforeach

instead of :

newItem += "@foreach ($menuItems as $item)";
....
newItem += "@endforeach";

but it doesn't work.

How Can I fix it ??

Share Improve this question edited Dec 5, 2020 at 7:41 Rowayda Khayri asked Oct 13, 2017 at 12:28 Rowayda KhayriRowayda Khayri 4891 gold badge11 silver badges24 bronze badges 5
  • I think it's because you are mixing php and javascript so the {!! $item->id !!} isn't getting echoed by php, but is set as a string. – Mr. Greenwoodz Commented Oct 13, 2017 at 12:44
  • So, what should I do to make it work ? @Mr.Greenwoodz – Rowayda Khayri Commented Oct 13, 2017 at 12:47
  • Something like var itemID = '<?php echo $item->id ?>;'; – Mr. Greenwoodz Commented Oct 13, 2017 at 12:50
  • I tried it but still having the same problem .. @Mr.Greenwoodz – Rowayda Khayri Commented Oct 13, 2017 at 13:04
  • You need to generate the select in JS and append it with js. Don't use blade , it will be not piled. You will need ajax to get your value. – Dessauges Antoine Commented Oct 13, 2017 at 13:20
Add a ment  | 

1 Answer 1

Reset to default 2

I have no experience in laravel, but I'd guess that it simply doesn't feel responsible for js files.

I don't like this approach where you store the markup in two places. Once in the html, and once in a String in JS. And you have to keep these two places sync.

May I suggest a different approach:

in create.blade.php:

<div class="createOrderForm" id="orderMenuItems">
    <div class="orderItem">

        <label> Item : </label>
        <select name="item[]" required>

            <option value="">Menu Items</option>

            @foreach ($menuItems as $item)
            <option value="{{$item->id}}">{{$item->name}}</option>
            @endforeach

        </select>

        <label>Quantity :</label>

        <input  type="text" name="quantity[]" value="" required>

    </div>
</div>

the template can be anywhere in the page, so why not store it right next to where it's used

and in create.js

$(document).ready(function () {
    console.log("Wele To create order Page");

    var $addItem = $('#addItem');
    var $orderMenuItems = $('#orderMenuItems');

    $addItem.click(function(){
        var markup = $orderMenuItems.children('.orderItem')[0].outerHTML;
        $orderMenuItems.append(markup);
    });
});
发布评论

评论列表(0)

  1. 暂无评论