我有这个选择选项,我正在尝试填充子菜单,这是laravel
中其他选择主菜单的另一个选择选项,具体取决于MainMenu的选择:
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Main Menu</label>
<select id="inputState" name="mainmenu" class="form-control">
<option selected>Choose...</option>
@foreach ($mainmenus as $mainmenu)
<option>{{$mainmenu->Title}}</option>
@endforeach
</select>
</div>
我从查询中获取了这个mainmenu选项的值:
$mainmenus = DB::connection("mysql2")->table("MainMenu")->get();
我通过以下查询获得子菜单:
$submenu = DB::connection("mysql2")->table('applicationsubmenu')
->join('applicationmenu', 'applicationmenu.Id', '=', 'applicationsubmenu.ApplicationMenuId')
->select('applicationsubmenu.*')
->where('applicationmenu.MainMenuId', '=', $main_menu_id)
->get();
有人能帮助我吗?我怎么能动态地做到这一点?提前谢谢,我真的很感激!
答案 0 :(得分:1)
在页面底部,我将实现一个Js函数,如下所示
<script>
$(document).ready(function(){
$('#inputState').change(function(){
var main_menu_id = $('#inputState').val();
axios.post('/submenus', {
main_menu_id: main_menu_id
}).then((r)=>{
var submenus = r.data.submenus;
for(var i=0; i<submenus.length; i++){
$('#submenu').append('<option>'+submenus[i].title+'</option>');
}
});
});
});
</script>
在web.php文件中定义链接
<?php
...
Route::get('/submenus','SubmenuController@getsubmenus');
?>
现在,在控制器上,
<?php
namespace ...;
use Illuminate\Http\Request;
use Response;
use DB;
public function getsubmenus(Request $request){
return Response([
'submenu' => DB::connection("mysql2")->table('applicationsubmenu')
->join('applicationmenu', 'applicationmenu.Id', '=',
'applicationsubmenu.ApplicationMenuId')
->select('applicationsubmenu.*')
->where('applicationmenu.MainMenuId', '=', $request->main_menu_id)
->get(),
]);
}
?>