Laravel 5.6:从下拉列表中获取选定的值以在另一个中使用它 [英] Laravel 5.6: Get selected value from a dropdown to use it in another

查看:51
本文介绍了Laravel 5.6:从下拉列表中获取选定的值以在另一个中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

6,我正在使用一个下拉列表从数据库表中读取一列.我想要的只是在该下拉列表上获取选定的值,然后使用它创建一个新查询,并将其填充到新的下拉菜单中.

6, I'm using a dropdown to read a column from a database table. All I want is to get the selected value on that dropdown and use it to create a new query that will be populated in a new drop-down.

阅读并看了几个示例后,我看到人们使用ajax,而其他人使用laravel HTTP请求(如$request->get()),所以我不知道该采取哪种方式,因为我对其中的任何一个都不熟悉,甚至在尝试多个时代使它无法正常工作.

After reading and see several examples I see people using ajax and other people using laravel HTTP request like $request->get() so i don't know which way to take since I'm not familiar with any of those and even when trying several times can't get it to work.

任何人都可以让我深入了解最佳/高效的方法吗?是否可以仅使用php或我缺少的laravel中的某些功能来做到这一点?

Can anyone give me an insight into the best/efficient way to do it? Is it possible to do it only using php or some feature in laravel that I'm missing?

这是我的控制人:

public function selectsector() //this is the dropdown #1 that works fine
{ 
 $sectors = DB::table('Sectors')->whereBetween('SectorID', [1, 10])->value('SectorID');
 return view('besttradesview', ['sectors10' => $sectors]);
} 

public function selectsubsector() //dropdown #2 not working
{
$subsectors = DB::table('Sectors')->where('parentid', $sectors)->get(); 
//this line is not working it does not recognize $sector variable
return view('besttradesview', ['subsectors42' => $subsectors]);
}

具有下拉菜单#1:扇区和#2:子扇区的视图

View with dropdow #1: sector and #2: subsector

<form method="GET">
<div class="selectsector">
<Select class="selectsector" name = "sector">
@foreach($sectors10 as $sector) 
<option>{{ $sector->SectorName }}</option>
@endforeach
</select>

<Select class="selectsubsector" name = "subsector">
@foreach($subsectors42 as $subsector) 
<option>{{ $subsector->SectorName }}</option>
@endforeach
</select>
</form>

路线:

Route::get('kitysoftware/besttrades', 'SectorsController@selectsector');
Route::get('kitysoftware/besttrades', 'SectorsController@selectsubsector');

获取错误:未定义变量:扇区

Getting error: Undefined variable: sectors

推荐答案

好,我设法使用具有json数据类型的javascript和ajax函数来做到这一点.我是JavaScript的新手,花了我一段时间,所以我花时间为新手发布详细信息.我们开始:

Ok, i managed to do it using javascript and ajax function with json datatype. I'm new at JavaScript and it took me a while so i'm taking the time to publish the details for the newcomers. Here we go:

查看文件: 诀窍是使用html隐藏对象,该对象会在下拉列表之前捕获此行的路由+前缀:

The View file: The trick is to use a html hidden object that captures a route + prefix like in this line before the dropdowns:

 <input type="hidden" name="application_url" id="application_url" value="{{URL::to(Request::route()->getPrefix()) }}"/>

此对象的名称为"application_url",我们稍后将在javascript代码中使用该对象来完成路由所需的url.
名称为"sectorSelect"的DropDown#1:

The name of this object is "application_url" which we'll be using later in the javascript code to complete the url that the routes needs.
DropDown#1 with name "sectorSelect":

<label class="selectsector">Sector:</label>
<Select class="selectsector" id="sectorSelect" name="sectorSelect" >
    <option value=""> -- Please select sector --</option>
    @foreach ($sectors10 as $sector)
        <option value="{{ $sector->SectorID }}">{{ $sector->SectorName }}</option>
    @endforeach
</select>

DropDown#2,名称为:"SubsectorSelect"

DropDown #2 with name: "SubsectorSelect"

 <label class="selectsector">SubSector:</label>
 <Select class="selectsector" id="subSectorSelect" name="subSectorSelect">
    <option value=""> -- Select an option --</option> // you don't have to do nothing here since this will be populated it from a query depending on the dropdown#1 selected value
 </select>

现在在web.php路由文件中:

Route::get('kitysoftware/sectors/subsectors/{id}', 'SectorsController@selectsubsector');

我们正在创建一个带有{id}参数的路由.这将是下拉菜单#1中的选定值.然后我们在Sectorscontroller中调用"selectsubsector"方法.

We are creating a route with an {id} parameter. This will be the selected value in dropdown #1. Then we call the "selectsubsector" method in the Sectorscontroller.

控制器: 第一个下拉查询:

public function selectsector()
{
$sectors = DB::table('Sectors')->select('SectorName', 'SectorID')- 
 >whereBetween('SectorID', [1, 10])->get();
    return view('besttradesview', ['sectors10' => $sectors]);

第二个下拉查询(selectsubsector方法):

Second Dropdown query (selectsubsector method):

   public function selectsubsector($sectorId)
    {

    $subsectors = DB::table('Sectors')->select('SectorName', 'SectorID')->where('parentid', $sectorId)->get();
    return response()->json($subsectors); //this line it's important since we are sending a json data variable that we are gonna use again in the last part of the view.
    }

视图文件的最后部分 javaScript + ajax函数

Final part of the view file The javaScript + ajax function

<script type="text/javascript">
    $('#sectorSelect').change(function () { //we watch and execute the next lines when any value from the dropdown#1 is selected
        var id = $(this).val(); //we get the selected value on dropdown#1 and store it on id variable
        var url = $('#application_url').val(); //we get the url from our hidden element that we used in first line of our view file, and store it on url variable
            //here comes the ajax function part
            $.ajax({
            url: url + "/kitysoftware/sectors/subsectors/" + id, //we use the same url we used in our route file and we are adding the id variable which have the selected value in dropdown#1
            dataType: "json", //we specify that we are going to use json type of data. That's where we sent our query result (from our controller)
            success: function (data) { //*on my understanding using json datatype means that the variable "data" gets the value and that's why we use it to tell what to do since here.*
                //and this final part is where we use the dropdown#1 value and we set the values for the dropdown#2 just adding the variables that we got from our query (in controllert) through "data" variable.
                $('#subSectorSelect').empty();
                $.each(data, function (key, value) {
                    $('#subSectorSelect').append('<option value="' + key.SectorID + '">' + value.SectorName + '</option>');
                });
            }
        });
    });
</script>

希望它对解决方案和解释有所帮助.我也很高兴获得一些反馈.

Hope it helps the solution and the explanations. I'm happy to get some feedback as well.

这篇关于Laravel 5.6:从下拉列表中获取选定的值以在另一个中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆