John Davidson

php - i go back to first page everytime i change my pagination leghth

0 comments
Message:


im working on a dynamic paginator that allows user to determine number of member that show per page
.but the problem is, whenever i change the paginator lenght, it goes back to page 1


ex : im on page 3 and show 10 members per page( which is showing member 21-30 ), when i change to show 30 members per page it goes back to first page and show member 1 - 30, but i want it to stay to pages 3 and show member 30-60


public function list(Request $request)
{
$items = $request->items ?? 10;
Paginator::useBootstrap();
$members = Member::paginate($items)->withQueryString();
return view('members', [
'members' => $members,
'page_title' => 'Member List',
'items' => $items
]);
}

heres my member list controller
{{ $members->appends('items')->links() }}


        <form>
<select id="pagination">
<option value="10" @if($items == 10) selected @endif >10</option>
<option value="30" @if($items == 30) selected @endif >30</option>
<option value="50" @if($items == 50) selected @endif >50</option>
<option value="100" @if($items == 100) selected @endif >100</option>
</select>
</form>
</div>
</div>
<script>
document.getElementById('pagination').onchange = function() {
window.location = "{!! $members->url(1)!!}&items=" + this.value;
};
</script>

this is my pagination lenght limit



My Answer:
>paginate(10); // default pagination length

if ($request->has('length')) {
$items = $request->paginate($request->length);
}

return view('list', ['items' => $items]);
}

In your view file, you can add a form to allow the user to change the pagination length:

html





This way, when the user changes the pagination length, the form will submit a GET request with the new length value. In your controller, you can check if the request has the 'length' parameter and update the pagination accordingly. This should allow the user to change the pagination length without resetting the page number.

Rate this post

4 of 5 based on 3076 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us