保持下拉菜单处于选中状态laravelcollective / html

时间:2019-03-22 08:52:02

标签: php laravel laravelcollective


我有一个像这样的下拉菜单:

{!! Form::open(['method'=>'get']) !!}
        <div class="row">
            <div class="col-sm-4 form-group">
                {!! Form::select('sort',[''=>'Choose Sort','asc'=>'Ascending','desc'=>'Descending'],null,['class'=>'form-control','onChange'=>'form.submit()']) !!}
            </div>
            <div class="col-sm-5 form-group">
                <div class="input-group">
                    <input class="form-control" id="search"
                           value="{{ request('search') }}"
                           placeholder="Search name" name="search"
                           type="text" id="search"/>
                    <div class="input-group-btn">
                        <button type="submit" class="btn btn-warning">Search</button>
                    </div>
                </div>
            </div>
        </div>
        {!! Form::close() !!}

当我选择“升序”之类的项目时,页面会重新加载,并且下拉菜单会返回“选择排序”。我想将选择的值保留在下拉菜单中。
我怎样才能做到这一点?非常感谢你!

1 个答案:

答案 0 :(得分:0)

它很简单,只需传递变量

根据您的要求更改路线

  

您认为

{!! Form::open(['method'=>'get','route' => 'document.sort']) !!}
        <div class="row">
            <div class="col-sm-4 form-group">
                {!! Form::select('sort',[''=>'Choose Sort','asc'=>'Ascending','desc'=>'Descending'],isset($sortvalues) ? $sortvalues : '',['class'=>'form-control','onChange'=>'form.submit()']) !!}
            </div>
            <div class="col-sm-5 form-group">
                <div class="input-group">
                    <input class="form-control" id="search"
                           value="{{ request('search') }}"
                           placeholder="Search name" name="search" value="{{ isset($searchvalues) ? $searchvalues : ''}}" 
                           type="text" id="search"/>
                    <div class="input-group-btn">
                        <button type="submit" class="btn btn-warning">Search</button>
                    </div>
                </div>
            </div>
        </div>
        {!! Form::close() !!}
  

我的路线

Route::any('/sort', 'DocDocumentController@sorDocument')->name('document.sort');
  

最后在Controller中

public function sorDocument(Request $request)
    {
        $docDocuments = DocDocument::latest()->paginate(20,['*'],'documentPage');
        $searchvalues = $request->search;
        $sortvalues = $request->sort;
        $viewShare = array_keys(get_defined_vars());
        return view('docdocuments.index', compact($viewShare));
    }