簡體   English   中英

如何使用Blade在Laravel中顯示有關選擇框的所選選項的詳細信息?

[英]How to display the details about the selected option of a select box in Laravel using Blade?

我是Laravel新手,有些我設法在選擇框中顯示數據庫內容。 我想要的是,我需要根據選擇框中選擇的ID從另一個表中獲取數據。 我的代碼如下

 <h2 class="comment-listings">Subscriber listings</h2><hr> <table> <thead> <tr> <th>Slelect a List:</th> <th><select class="form-control input" name="list1s" id="list1s" > <option selected disabled>Please select one option</option> @foreach($list1s as $list1) <option value="{{$list1->id}}">{{$list1->name}}</option> @endforeach </select> </th> </tr> </thead> </table> <table> <thead> <tr> <th>Device ID</th> <th>Status</th> <th>Delete</th> </tr> </thead> <tbody> @foreach($subscribers as $subscriber) <tr> <td>{{{$subscriber->device_id}}}</td> <td>{{$subscriber->list1->name}}</td> <td> {{Form::open(['route'=>['subscriber.update',$subscriber->id]])}} {{Form::select('status',['approved'=>'Approved','blocked'=>'Blocked'],$subscriber->status,['style'=>'margin-bottom:0','onchange'=>'submit()'])}} {{Form::close()}} </td> <td>{{HTML::linkRoute('subscriber.delete','Delete',$subscriber->id)}}</td> </tr> @endforeach </tbody> </table> <script> $('#list1s').on('change',function(e){ console.log(e); var list_id = e.target.value; $.get('/subscriber/get?list_id=' + list_id, function(data){ console.log(data); }) }) </script> {{$subscribers->links()}} 

用於填充選擇框的控制器

public function listList()
{
    $list1s = List1::all();
    $this->layout->title = 'Subscriber Listings';
    $this->layout->main = View::make('dash')->nest('content', 'subscribers.list', compact('list1s'));
    $subscribers = Subscriber::orderBy('id', 'desc')->paginate(20);
    View::share('subscribers', $subscribers);
}

這將顯示所有列表和訂戶。 由於我需要顯示在選擇框中選擇的列表的訂戶,因此我在刀片服務器的底部添加了一個腳本,並在路由中添加了其他控制器功能,以基於從腳本傳遞的list_id執行任務。

 Route::get('/subscriber/get', function(){
    $list_id = Input::get('list_id');
    $subscribers = Subscriber::where('list1_id','=', $list_id)->get();
    return Response::eloquent($subscribers);
});

但是沒有任何變化,我在哪里錯了?

我已經編輯了代碼,並且成功。

 <h2 class="comment-listings">Campaign listings</h2><hr> <script data-require="jquery@2.1.1" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th>Select a List:</th> <th> <select class="form-control input" name="list1s" id="list1s" onchange="displayVals(this.value)"> <option selected disabled>Please select a list</option> @foreach($list1s as $list1) <option value="{{$list1->id}}">{{$list1->name}}</option> @endforeach </select> </th> </tr> </thead> </table> <div id="campaign"> </div> <script> function displayVals(data) { var val = data; $.ajax({ type: "POST", url: "get", data: { id : val }, success:function(campaigns) { $("#campaign").html(campaigns); } }); } </script> 

路線

Route::any('/campaigns/get', [
    'as' => '/campaigns/get', 
    'uses' => 'CampaignController@getCampaigns'
    ]);

調節器

public function getCampaigns()
{
$list1 = Input::get('id');
$campaigns = Campaign::orderBy('id', 'desc')
->where('list1_id','=', $list1)
->where('user_id','=',Auth::user()->id)
->paginate(10);
return View::make('campaigns.ajaxShow')->with('campaigns', $campaigns);
}

當選擇框中的選項更改時,我現在有一個單獨的刀片(ajaxShow)要加載。

 <table> <thead> <tr> <th>Campaign title</th> <th>Status</th> <th>Delete</th> </tr> </thead> <tbody> @foreach($campaigns as $campaign) <tr> <td>{{{$campaign->template->title}}}</td> <td> {{Form::open(['route'=>['campaign.update',$campaign->id]])}} {{Form::select('status',['yes'=>'Running','no'=>'Stoped'],$campaign->running,['style'=>'margin-bottom:0','onchange'=>'submit()'])}} {{Form::close()}} </td> <td>{{HTML::linkRoute('campaign.delete','Delete',$campaign->id)}}</td> </tr> @endforeach </tbody> </table> {{$campaigns->links()}} 

Laravel 4及更高版本中的Illuminate\\Http\\Response中沒有實現eloquent方法(我相信該方法僅在Laravel 3中可用)。 但是您可以改為返回json響應:

Route::get('/subscriber/list', function(){
    $list_id = Input::get('list_id');
    $subscribers = Subscriber::where('list1_id','=', $list_id)->get();

    return Response::json($subscribers);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM