I am using yajra datatable in laravel.I have implemented the table.It is working fine except search. I have a custom filter too. Below code is showing the Datatable initialization and ajax request. User Type Filter is working fine.
<script>
$(document).ready(function() {
var table = $('#dataList').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{ route('users.user.index') }}",
data: function (d) {
d.user_type = $('#user_type_filter').val();
}
},
columns: [
{data: 'DT_RowIndex', name: 'DT_RowIndex'},
{data: 'name', name: 'name'},
{data: 'email', name: 'email'},
{data: 'phone', name: 'phone'},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
$('#search-form').on('submit', function(e) {
table.draw();
e.preventDefault();
});
} );
</script>
Backend index page to filter the user type
public function index(Request $request)
{
/**
* Ajax call by datatable for listing of the users.
*/
if ($request->ajax()) {
$data = User::with('userType')->get();
$datatable = DataTables::of($data)
->filter(function ($instance) use ($request) {
if ($request->has('user_type') && $request->get('user_type')) {
$instance->collection = $instance->collection->filter(function ($row) use ($request) {
//return Str::contains($row['phone'], $request->get('phone')) ? true : false;
return $row['user_type_id'] == $request->get('user_type');
});
}
if ($request->input('search.value') != "") {
$instance->collection = $instance->collection->filter(function ($row) use ($request) {
return Str::contains($row['name'], $request->input('search.value')) ? true : false;
});
}
})
->addIndexColumn()
->addColumn('action', function ($user) {
return view('users.datatable', compact('user'));
})
->rawColumns(['action'])
->make(true);
return $datatable;
}
$user_type = UserType::pluck('user_type','id')->all();
$users = User::with('userType')->paginate(25);
return view('users.index', compact('users','user_type'));
}
This code is generating the search only on current page.
you should try like this
refer this link https://datatables.yajrabox.com/fluent/custom-filter