I have a page with pagination and a JavaScript toggle that switches between table and boxes view. The toggle works fine, but when I navigate to another page (page 2, 3, etc.), it always resets back to the default boxes view instead of maintaining the current view state which is a table.
How can I preserve the view state (table vs boxes) across pagination?
Current behavior:
- User switches to table view
- User clicks “next page”
- Page loads but resets to boxes view (not desired)
Desired behavior: Stay in table view across all pages.
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
</span>
</div>
<!-- The toggle that breaks on pagination -->
{% for application in page_obj %}
<!-- both table and boxes loop through page_obj -->
{% endfor %}
// This always resets to boxes on page load
boxes.style.display = 'grid';
tables.style.display = 'none';
let isBoxes = true;
toggleTable.addEventListener('click', function(){
if(isBoxes){
boxes.style.display = 'none';
tables.style.display = 'block';
isBoxes = false;
} else {
boxes.style.display = 'grid';
tables.style.display = 'none';
isBoxes = true;
}
});
def track_applications(request):
job_list = applications.objects.filter(user=request.user)
paginator = Paginator(job_list, 3)
page_obj = paginator.get_page(request.GET.get("page"))
return render(request, "template.html", {'page_obj': page_obj})