我有一张桌子,上面满是赞助孩子,我希望用户按位置,性别等进行过滤。 目前仅从这2个开始。稍后将添加更多过滤器。
在我的过滤器表单上
<form action="{{ route('child-sponsorship-filter') }}" method="get">
@csrf
Location
<select name="location">
<option value="">No Preference</option>
@foreach ($country as $c)
<option value="{{$c->country_name}}">{{$c->country_name}}</option>
@endforeach
</select>
Gender
<select name="gender">
<option value="">No Preference</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<button type="submit">Filter Results</button>
</form>
/// Begin Displays All and Filtered sponsor children
@foreach ($sponsorKid as $sk)
...
@endforeach
在我的KidController上
public function index(){
$country = DB::table('countries')->orderBy('country_name')->get();
$sponsorKid = Kid::orderBy('first_name')->get();
return view('child-sponsorship.index',compact('sponsorKid','country'));
}
public function filter(Request $request){
$location = $request->location;
$gender = $request->gender;
// $filter = [$location->location, $gender->gender];
//dd($location);
$country = DB::table('countries')->orderBy('country_name')->get();
if(empty($gender) && empty($location)){
$sponsorKid = Kid::orderBy('id')->get();
}elseif(!empty($gender) && !empty($location)){
$sponsorKid = DB::table('kids')->where('current_country', $location)->where('gender', $gender)->orderby('id')->get();
}elseif($gender != true){
$sponsorKid = DB::table('kids')->where('current_country', $location)->orderby('id')->get();
}elseif($location != true){
$sponsorKid = DB::table('kids')->where('gender', $gender)->orderby('id')->get();
}else{
$sponsorKid = Kid::orderBy('id')->get();
}
return view('child-sponsorship.index',compact('sponsorKid','country'));
}
儿童表
+----+------------+--------+-----------------+
| id | first_name | gender | current_country |
+----+------------+--------+-----------------+
| 1 | Emma | Female | Haiti |
| 2 | Lea | Female | Nepal |
| 3 | Matthew | Male | India |
| 4 | Andrew | Male | Nepal |
| 5 | CHRIS | Male | Haiti |
+----+------------+--------+-----------------+
国家/地区表
+----+--------------+
| id | country_name |
+----+--------------+
| 1 | Haiti |
| 2 | India |
| 3 | Nepal |
+----+--------------+
正在使用的路由
Route::get('/child-sponsorship','KidController@index')->name('child-sponsorship');
Route::get('/child-sponsorship/filter','KidController@filter')->name('child-sponsorship-filter');
以我在URL中获得令牌的形式对@csrf
进行操作。怎么样
我可以删除吗?
http://xxxxxx.oo/child-sponsorship/filter?_token=jqFSC7tTmHuZ1tL8Dzz1LzNQNv5F2pOUrAa5uzs2&location=Haiti&gender=Male
选择过滤器后,例如:Location, Haiti
。页面刷新后,我似乎无法使当前过滤器保持在选择框中。页面刷新后,它将返回到其默认状态。 (我需要第三条路线吗?)
有人可以告诉我如何在不刷新页面的情况下通过ajax更新过滤器。
是否有比使用大量的if语句更好的过滤查询的方法?
任何帮助将不胜感激!
好吧,我想我越来越近了。
路线:
Route::get('/child-sponsorship','KidController@index')->name('child-sponsorship');
Route::get('/child-sponsorship/filter','KidController@filter')->name('child-sponsorship-filter');
索引视图 (显示过滤器表单和赞助者子级)
<form id="myForm">
<label>Location</label>
<select id="location" name="location">
<option value="">No Preference</option>
@foreach ($country as $c)
<option value="{{$c->country_name}}">{{$c->country_name}}</option>
@endforeach
</select>
<label>Gender</label>
<select id="gender" name="gender">
<option value="">No Preference</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<button id="ajaxSubmit">Filter Results</button>
</form>
@foreach ($sponsorKid as $sk)
////.......
@endforeach
-----------------------------------------------------
// JS
@section('filter_js')
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
jQuery(document).ready(function(){
jQuery('#ajaxSubmit').click(function(e){
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
jQuery.ajax({
url: "{{ route('child-sponsorship-filter') }}",
method: "get",
data: {
location: jQuery('#location').val(),
gender: jQuery('#gender').val(),
},
success: function(result){
console.log(result);
}});
});
});
</script>
@endsection
根据我的调试栏工具,我得到了正确的查询:
select * from `kids` where `current_country` = 'Nepal' and `gender` = 'Female' order by `id` asc
但是页面上没有正确显示...。 我也在控制台中获得了正确的路径:
jquery.min.js:19 XHR finished loading: GET "http://xxxxxx.oo/child-sponsorship/filter?location=Nepal&gender=Female".
我在这里做什么错了?
这是我在KidController
中的2个函数 (索引和过滤器)
public function index(){
$country = DB::table('countries')->distinct('country_name')->orderBy('country_name')->get();
$sponsorKid = Kid::orderBy('first_name')->get();
return view('child-sponsorship.index',compact('sponsorKid','country'));
}
public function filter(Request $request){
$location = $request->location;
$gender = $request->gender;
$country = DB::table('countries')->orderBy('country_name')->get();
if(empty($gender) && empty($location)){
$sponsorKid = Kid::orderBy('id')->get();
}elseif(!empty($gender) && !empty($location)){
$sponsorKid = DB::table('kids')->where('current_country', $location)->where('gender', $gender)->orderby('id')->get();
}elseif($gender != true){
$sponsorKid = DB::table('kids')->where('current_country', $location)->orderby('id')->get();
}elseif($location != true){
$sponsorKid = DB::table('kids')->where('gender', $gender)->orderby('id')->get();
}else{
$sponsorKid = Kid::orderBy('id')->get();
}
return view('child-sponsorship.index',compact('sponsorKid','country'));
}
我返回的视图与我不希望刷新到新页面相同。