Laravel 5.1 Ajax 从 Blade View 获取 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33247140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:32:51  来源:igfitidea点击:

Laravel 5.1 Ajax to get html from Blade View

ajaxlaravel

提问by karmendra

I have a myitems($filter) method in a ItemsController that loads the myitems.blade view.

我在加载 myitems.blade 视图的 ItemsController 中有一个 myitems($filter) 方法。

The view has list of items as label and text box to get number of items ($items is passed from myitems method to myitems view and there is a model form binding on items model as well)

该视图具有项目列表作为标签和文本框以获取项目数量($items 从 myitems 方法传递到 myitems 视图,并且项目模型上也有模型表单绑定)

I have a select box that has options 1. All, 2. Expired and 3.New

我有一个选择框,其中包含选项 1. All、2. Expired 和 3.New

When I change the selection I want to have get new $items from the controller and recreate the form with new items list.

当我更改选择时,我希望从控制器获取新的 $items 并使用新项目列表重新创建表单。

I am using using jquery to alert that the selection is changed. but I am not able to figure out how will I be able to call myitems($filter) from ajax and redirect to create the items.blade page.

我正在使用 jquery 来提醒选择已更改。但我无法弄清楚如何从 ajax 调用 myitems($filter) 并重定向以创建 items.blade 页面。

回答by karmendra

Route

路线

//Note the ? in parameter, it says parameter is optional
Route::get('/myitems/{filter?}', 
    ['as' => 'myitems.list', 'uses' => 'ItemController@myitems']
);

Controller

控制器

...
public function myitems(Request $request, $filter = null)
{
    if (empty($filter)) $filter=1;

    $items = Item::where('item_type', $filter)->get();

    // if ajax call just return the partial that displays the list
    if ($request->ajax()) {
        return view('_itemlist', compact('items'));
    }

    // passed as options to input select
    $filters= [
           'All' => '0',
           'New' => '1',
           'Expired' => '2'
    ];
    return view('itempagewith_defaultitemlist', compact('items','filters'));
}
...

View

看法

view itempagewith_defaultitemlist.blade.php

查看itempagewith_defaultitemlist.blade.php

<div class="container">
{!! Form::model($myitems, ['route' => 'myitems.list', 'class'=>'form-horizontal', 'files' => true]) !!}

<div id="filterselectbox">
    <div class="form-group">
            {!!Form::label('filterselectbox','*Filter:',['class'=>'control-label']) !!}            
            {!!Form::select('filterselectbox', $filters, null, ['class'=>'form-control ']) !!}
     </div>
</div>
{!! Form::close() !!}

<div id="item-container">
    @include('_itemlist')
</div>

<script>
$('#filterselectbox').change(function() {
    var id = $('#filterselectbox').val();
    var ajaxurl = '{{route('myitems', ':id')}}';
    ajaxurl = ajaxurl.replace(':id', id);
    $.ajax({
        url: ajaxurl,
        type: "GET",
        success: function(data){
            $data = $(data); // the HTML content that controller has produced
            $('#item-container').hide().html($data).fadeIn();
        }
    });
});
</script>                   

</div>

partial view _itemlist.blade.php

部分视图_itemlist.blade.php

 <ul>
     @foreach ($items as $item)
    <li>{{ $item->name }}</li>
     @endforeach
 </ul>