Paginate Doesn't Work As Expected Uisng Laravel And Jq
I am working on a filtering data. As there is <input>
and if someone start typing I am calling jq
function to send a request to my controller. It is working fine. Even I am getting filtered data as well. But if I click on page 2 then it affects a design. So then after I got to know it is change my url
.
As an example - After filtering data (This works fine)
code.test/?page=1
But if you click on page 2, it is redirecting to
code.test/filter?page=2
Here is my code for main.blade
-
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-12">
<div class="form-group">
<input type="text" class="form-control" id="search" placeholder="Enter email" name="email">
</div>
</div>
<div class="col-md-3 col-sm-12">
<div class="form-group">
<select id="department">
<option value="0">All Departments</option>
@foreach($department as $d)
<option value="{{$d->id}}">{{$d->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
<div id="filter">
<div class="row">
<?php
$count = count($data);//dd($data[0]->fname);
?>
@if($count > 0)
@foreach($data as $d)
<div class="col-md-12">
{{$d->fname}}, {{$d->lname}}<br>
{{$d->profile}}<br>
<b>{{$d->departments->name}}</b>
</div>
@endforeach
@else
No data found
@endif
</div>
</div>
{{ $data->appends($data)->links() }}
</div>
This is my jq function -
function filter(){
var str = $("#search").val();
var dep = $('#department option:selected').val();
// /alert(dep);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "GET",
url: '/filter',
data: {
str: str,
dep: dep,
},
success: function(data){
console.log(data);
$('#filter').html(data);
},
});
}
$(document).ready(function(){
$("#search").on('input', function(){
filter();
});
$("#department").change(function () {
filter();
});
});
Now in controller
I am returning a view
after filter-
public function filter(Request $request){
$str = $request->str;
$dep = $request->dep;//dd($dep);
$s = new Staff;
$d = new Department;
//If input and dropdown values are available
if($str != null && $dep != 0){
$data = $s::with('departments')
->where('department', $dep)
->where(function($q) use ($str) {
$q->where('fname', 'like', '%'.$str.'%')
->orWhere('lname', 'like', '%'.$str.'%');
})
->paginate(10)
->appends(['dep'=> $dep, 'str'=> $str]);
$data_count = count($data);
return view('search', compact('data', 'data_count'));
} 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.'%');
})
->paginate(10)
->appends(['dep'=> $dep, 'str'=> $str]);
$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)
->paginate(10)
->appends(['dep'=> $dep, 'str'=> $str]);
$data_count = count($data);
return view('search', compact('data', 'data_count'));
} else if($str == null && $dep == 0){ //If dropdown value is null and input is null
$data = $s::with('departments')->paginate(10)->appends(['dep'=> $dep, 'str'=> $str]);
$data_count = count($data);
return view('filter', compact('data', 'data_count'));
}
}
Please help me out with this.
Thank you in advance.
Answer
It's bit a late to answer but this may help you -
- In your
main.blade
instead of loading data directly you can pass that data to otherview
and then@include
that newblade
file tomain.blade
. Like this -
<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>
Create new blade as an example
search.blade
and paste whatever you have in<div id="filter"> /* ---- This lines ---*/
insearch.blade
.In you
js
function change url ofmain.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);
},
});
- Make changes in
controller
. Just check whether request is fromajax
or not and then process that data.
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'));
}
Hope this will work for you. Thank you.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?