1 回答
TA贡献1995条经验 获得超2个赞
回答有点晚了,但这可能对您有所帮助-
在您main.blade而不是直接加载数据中,您可以将该数据传递给其他文件view,然后@include将该新blade文件传递给main.blade. 像这样 -
<div class="ui-block">
<div class="ui-block-content">
<div class="row">
<div class="col col-xl-9 col-lg-9 col-md-9 col-sm-12 col-12">
<div class="form-group">
<input type="text" class="form-control" id="search" placeholder="Start typing keywords.." name="search">
</div>
</div>
<div class="col col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12">
<div class="form-group">
<select id="department" class="form-control">
<option value="0">All Departments</option>
@foreach($department as $d)
<option value="{{$d->id}}">{{$d->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
</div>
</div>
<div id="filter">
@include('search')
</div>
以创建新刀片为例search.blade,并将您拥有的任何内容粘贴<div id="filter"> /* ---- This lines ---*/到search.blade.
在你js的函数中更改 url main.blade-
$.ajax({
type: "GET",
url: '/',//change this to main.blade's url
data: {
str: str,
dep: dep,
},
success: function(data){
console.log(data);
$('#filter').html(data);
},
});
在 中进行更改controller。只需检查请求是否来自ajax,然后处理该数据。
public function main(Request $request)
{
$data = Staff::with('departments')->orderBy('created_at', 'DESC')->paginate(10);
$department = Department::all();
//If request is from ajax, then processing to filter data
if($request->ajax()) {
$str = $request->str;
$dep = $request->dep;//dd($dep);
$s = new Staff;
$d = new Department;
if($str != null && $dep != 0){ //If input and dropdown values are available
$data = $s::with('departments')
->where('department', $dep)
->where(function($q) use ($str) {
$q->where('fname', 'like', '%'.$str.'%')
->orWhere('lname', 'like', '%'.$str.'%')
->orWhere('profile', 'like', '%'.$str.'%');
})
->orderBy('created_at', 'DESC')
->paginate(10);
} else if($str != null && $dep == 0){ //If input value is set and dropdown value set to all departments
$data = $s::with('departments')
->where(function($q) use ($str) {
$q->where('fname', 'like', '%'.$str.'%')
->orWhere('lname', 'like', '%'.$str.'%')
->orWhere('profile', 'like', '%'.$str.'%');
})
->orderBy('created_at', 'DESC')
->paginate(10);
$data_count = count($data);
return view('search', compact('data', 'data_count'));
} else if($str == null && $dep != 0){ //If dropdown value is not null and input is null
$data = $s::with('departments')
->where('department', $dep)
->orderBy('created_at', 'DESC')
->paginate(10);
} else if($str == null && $dep == 0){ //If dropdown value is null and input is null
$data = $s::with('departments')->orderBy('created_at', 'DESC')->paginate(10);
}
// returning data to view
return view('search', ['data' => $data])->render();
}
//returning data if request is not from ajax
return view('main', compact('data', 'department'));
}
希望这对你有用。谢谢你。
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报