javascript - 如何使用 Blade 在 Laravel 中显示选择框所选选项的详细信息?

标签 javascript php jquery laravel laravel-4

我是 Laravel 新手 我如何设法将数据库内容显示到选择框中。我想要的是,我需要根据选择框中选择的 ID 从另一个表中获取数据。我的代码如下

Blade

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

用于填充选择框的 Controller

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

这显示了所有列表和订阅者。 由于我需要显示选择框中所选列表的订阅者,因此我在 Blade 底部添加了一个脚本,并在路由中添加了其他 Controller 功能,以根据从脚本传递的 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);
});

但是没有发生任何变化,我错在哪里?

最佳答案

我已经编辑了我的代码,并且成功了。

Blade

<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'
    ]);

Controller

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

我现在有一个单独的 Blade (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()}}

关于javascript - 如何使用 Blade 在 Laravel 中显示选择框所选选项的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28295123/

相关文章:

javascript - jQuery UI 对话框 - 停止关闭事件

php/symfony2 从 URL 中隐藏 app.php

javascript - 在加载 jQuery 之前阻止链接

javascript - 如何让这个jQuery函数更加优雅呢?

javascript - Django AJAX 防止模板之间重新加载元素

javascript - 停止 angular/css 中的图像自动旋转

javascript - 小图像悬停查看大图像(虽然不是单独的窗口)

php - CakePHP : How to Create OptGroup Drop down list from DataBase?

javascript - Cassandra 错误: Undefined column name apparently due to case sensitivity despite consistent case in JavaScript

php - Docker连接到本地主机