Мой созданный URL-адрес для запроса AJAX при прокрутке выглядит следующим образом: страница не объединена в конце
Код: Выделить всё
console.log("Constructed URL: " + ENDPOINT + "?page=" + currentPage);
// returns => Constructed URL: http://127.0.0.1:8000/search?interestedin=Sugar+Mommy&minAge=22&maxAge=40&minHeight=150&maxHeight=180&minWeight=55&maxWeight=85&minChildren=1&maxChildren=3&distance=10&address=F9VW%2B3WW%2C+Main+Main+Qazi+Rd%2C+Nishat+Colony+Lahore%2C+Punjab%2C+Pakistan&city=Lahore&state=Punjab&country=Pakistan&latitude=31.4927025&longitude=74.397301?page=2Мой пользовательский код выглядит следующим образом:
Код: Выделить всё
var ENDPOINT = "{{ route('search') }}" + window.location.search;
var isLoading = false;
var currentPage = 1;
var totalPages = "{{$users -> lastPage()}}"; // Assuming you pass the total pages to your Blade view
$(window).scroll(function() {
var container = $("#user-cards-container");
var containerHeight = container.height();
var scrollTop = $(window).scrollTop();
var windowHeight = $(window).height();
if (!isLoading && currentPage < totalPages && scrollTop + windowHeight >= container.offset().top + containerHeight - 20) {
currentPage++;
infinteLoadMore(currentPage);
}
});
function infinteLoadMore(page) {
console.log("Constructed URL: " + ENDPOINT + "?page=" + currentPage);
isLoading = true;
$.ajax({
url: ENDPOINT + "?page=" + page,
datatype: "html",
type: "get",
beforeSend: function() {
$('.auto-load').show();
}
})
.done(function(response) {
if (response.html == '') {
$('.auto-load').html("No more data to display.");
isLoading = false;
return;
}
$('.auto-load').hide();
$("#user-cards-container").append(response.html);
console.log("Response is: " + response);
isLoading = false;
})
.fail(function(jqXHR, ajaxOptions, thrownError) {
console.log('Server error occurred');
isLoading = false;
});
}
Код: Выделить всё
@include('user.member_card')
и мой код контроллера Laravel выглядит следующим образом:
Код: Выделить всё
public function search(Request $request)
{
$page = $request->input('page', 1);
$query = User::query()
->where('id', '!=', Auth::id()) // Excluding the authenticated user
->where('status', '!=', 0); // Excluding blocked users
// Filtering based on interestedin
if ($request->interestedin) {
$query->where('iam', $request->interestedin);
}
// Filtering based on bodyType
$bodyTypeMap = [
"Small" => 0,
"Average" => 1,
"Aethletic" => 2,
"Large" => 3,
];
$selectedBodyTypes = [];
foreach ($bodyTypeMap as $bodyTypeName => $bodyTypeValue) {
if ($request->has($bodyTypeName)) {
$selectedBodyTypes[] = $bodyTypeValue;
}
}
if (!empty($selectedBodyTypes)) {
$query->whereIn('body_type', $selectedBodyTypes);
}
// Filtering based on age
if ($request->age) {
$query->whereBetween('age', [$request->minAge, $request->maxAge]);
}
// Filtering based on height
if ($request->height) {
$query->whereBetween('height', [$request->minHeight, $request->maxHeight]);
}
// Filtering based on weight
if ($request->weight) {
$query->whereBetween('weight', [$request->minWeight, $request->maxWeight]);
}
// Filtering based on children
if ($request->children) {
$query->whereBetween('child', [$request->minChildren, $request->maxChildren]);
}
// Filtering based on location
if ($request->searchByLocation) {
// making bounding box using helper function
$locationBorders = distanceCalculator($request->latitude, $request->longitude, $request->distance);
$query->whereBetween('latitude', [$locationBorders['lat_min'], $locationBorders['lat_max']])
->whereBetween('longitude', [$locationBorders['lng_min'], $locationBorders['lng_max']]);
}
$users = $query->paginate(9, ['*'], 'page', $page);
if ($request->ajax()) {
$view = view('user.member_card', compact('users'))->render();
$lastPage = $users->lastPage(); // Get the total number of pages
return response()->json(['html' => $view, 'lastPage' => $lastPage]);
}
return view('user.search', ['users' => $users]);
}Если вам нужен другой фрагмент кода или что-то еще, просто дайте мне знать.< /п>
Подробнее здесь: https://stackoverflow.com/questions/769 ... nding-ajax
Мобильная версия