简体   繁体   English

如何将 Laravel foreach 放入 JS 文件中?

[英]How to put Laravel foreach inside JS file?

I have this select-box I just add it via jQuery :我有这个select-box我只是通过jQuery添加它:

<span onclick="createProduct()">Add New<i class="fa fa-plus"></i></span>

<script>

function createProduct() {
        var html = '';
        html += '   <div class="col-xs-12">';
        html += '      <div class="form-group">';
        html += '          <div class="input-group">';
        html += '               <span class="input-group-addon input-group-addon-small"><i class="sicon-tag-special"></i></span>';
        html += '               <select class="form-control" name="category_id" style="width: 100%">';
        html += '                  <option value="">Select Category.. </option>';
        html += '                  <option value="">Category1</option>';
        html += '                  <option value="">Category2</option>';
        html += '               </select>';
        html += '          </div>';
        html += '       </div>';
        html += '    </div>';

     $('#products_div').prepend(html);
 }

</script>

Here when the Admin clicks on the add new button, a select element is added in the page.在这里,当管理员单击add new按钮时,会在页面中添加一个选择元素。 My problem is with creating the the options in the select element.我的问题是在 select 元素中创建选项。 I have this foreach I need to put it into this js code!我有这个 foreach 我需要把它放到这个 js 代码中!

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

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.在这种情况下,由于您要附加的 HTML 由来自 Laravel 后端的内容控制,因此将 HTML 和 JS 分离会更有意义。 As such, create the HTML in the DOM when the page loads and hide it.因此,当页面加载并隐藏它时,在 DOM 中创建 HTML。 Then in the JS you can clone that HTML and append it where it is required to be displayed.然后在 JS 中,您可以克隆该 HTML 并将其附加到需要显示的位置。 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.在 HTML 中使用内联事件处理程序(例如onclick等)是不好的做法,应尽可能避免。

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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM