【发布时间】:2021-04-12 13:31:14
【问题描述】:
我想通过实时搜索实现分页。我的实时搜索是 工作,但是当我点击第 2 页时,我总是看到第 1 页 数据。我不明白如何正确实现分页,以便 点击其他页面后,我会发现不同的数据。
这是我的index.blade.php,我想在其中实现分页和实时搜索。
@extends('layout.master')
@section('css')
<meta name="csrf-token" content="{{ csrf_token() }}">
@endsection
@section('content')
<br>
<div class="br-pagebody">
<div class="br-section-wrapper">
@include('include._message')
<div class="row">
<h6 class="br-section-label">Doctors List</h6>
</div>
<div class="row">
<div class="col-lg-4 ">
<div class="form-group">
<input type="text" name="search" id="search" class="form-control" placeholder="Search doctor Data" />
</div>
</div>
<div class="col-lg-3 offset-lg-5">
<a href="{{route('doctors.create')}}" class="btn btn-primary" style="margin-left: 35px">Add New Doctor</a>
</div>
</div>
</br>
<div class="table-responsive">
<table class="table table-bordered table-colored table-dark">
<thead class="thead-colored thead-dark">
<tr>
<th>Image</th>
<th>Doctor Id</th>
<th>Name</th>
<th>Department</th>
<th>Email</th>
<th>mobile</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<input type="hidden" name="hidden_page" id="hidden_page" value="1" />
</div><!-- table-wrapper -->
<div class="d-flex justify-content-left">
{!! $doctors->links() !!}
</div>
</div>
</div>
@endsection
@section('js')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
fetch_doctor_data();
function fetch_doctor_data(query = '')
{
$.ajax({
url:"{{route('search.doctor')}}",
method:"GET",
data:{'query':query},
dataType:'json',
success:function(data)
{
var output = '';
if(data.data.length > 0)
{
for(var count = 0; count < data.data.length; count++)
{
output += '<tr>';
output += '<td class="center"><img src="'+data.data[count].image+'" style="width:40px;height:40px;"/></td>';
output += '<td>'+data.data[count].id+'</td>';
output += '<td>'+data.data[count].name+'</td>';
output += '<td>'+data.data[count].department.name+'</td>';
output += '<td>'+data.data[count].email+'</td>';
output += '<td>'+data.data[count].mobile+'</td>';
output += '<td>'+data.data[count].status+'</td>';
output += '<td><a class="btn btn-primary" href="doctors/'+data.data[count].id+'/edit"><i class="fas fa-edit">Edit</i></a><button style="margin-left:2px;" class="btn btn-danger delete" value="'+data.data[count].id+'" data-id="'+data.data[count].id+'"><i class="fas fa-trash">Delete</i></button></td>';
output += '</tr>';
}
}
else
{
output += '<tr>';
output += '<td colspan="8" style="text-align:center">No Data Found</td>';
output += '</tr>';
}
$('tbody').html(output);
}
})
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_doctor_data(query);
});
});
</script>
<script>
$(document).ready(function(){
$(document).on("click", ".delete", function() {
if(!confirm("Do you really want to do this?")) {
return false;
}
var $element = $(this).parent().parent();
var id= $(this).val();
var token = $("meta[name='csrf-token']").attr("content");
console.log(id)
$.ajax(
{
url: "doctors/"+id,
type: 'delete',
data: {
"id": id ,
"_token": token,
},
success: function (response)
{
console.log(response)
$element.fadeOut().remove();
}
});
});
});
</script>
@endsection
这是我在控制器中用于实时搜索的功能:
function liveSearchDoctor(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data =Doctor::
where('name', 'like', '%'.$query.'%')
->orWhere('mobile', 'like', '%'.$query.'%')
->orWhere('email', 'like', '%'.$query.'%')
->orderBy('id', 'desc')
->with('department')
->paginate(2);
}
else
{
$data =Doctor::with('department')
->paginate(2);
}
}
【问题讨论】:
-
使用livewire就不用手动使用ajax了