Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How to persist button selections when changing pages with pagination (But not on refresh)

Hello,

I have a table with pagination and a column that has 2 buttons you can toggle between for each row.

The buttons are pre-selected according to data from the database:

    @foreach($rows as $row)
      <tr>
        <td> ... </td>
        <td> ... </td>
        <td>
          <div class="buttons">
            <button {{$row->type === "option1" ? "selected" : ""}}>Option 1</button>
            <button {{$row->type === "option2" ? "selected" : ""}}>Option 2</button>
          </div>
        </td>
      </tr>
    @endforeach

The user can select choices accross the table, then he has a "save" button that saves all the updated selections.

The problem is that when the pagination link is clicked, the selections aren't saved.

How can I do that? To persist the selections but also make a distinction between the pagination action and a refresh (or tab/browser closed), so that when the user refresh or closes the browser/tab, it will not save the selections?

Thanks

0 likes
7 replies
LaryAI's avatar
Level 58

To persist the button selections when changing pages with pagination, you can use JavaScript to store the selected options in the browser's local storage. When the user navigates to a new page, you can retrieve the stored selections and update the buttons accordingly. To distinguish between a pagination action and a refresh, you can add a query parameter to the pagination links and check for its presence in your server-side code. Here's an example implementation:

  1. Add a query parameter to the pagination links:
{{ $rows->appends(['page_action' => 'paginate'])->links() }}
  1. Use JavaScript to store the selected options in local storage:
$(document).on('click', '.buttons button', function() {
    var rowId = $(this).closest('tr').data('id');
    var option = $(this).text();
    localStorage.setItem('row_' + rowId, option);
});
  1. Use JavaScript to retrieve the stored selections and update the buttons:
$(document).ready(function() {
    $('.buttons button').each(function() {
        var rowId = $(this).closest('tr').data('id');
        var option = localStorage.getItem('row_' + rowId);
        if (option) {
            $(this).removeClass('selected');
            $(this).siblings().addClass('selected');
        }
    });
});

$(document).on('click', '.pagination a', function() {
    localStorage.clear();
});
  1. Check for the presence of the query parameter in your server-side code:
if (request()->query('page_action') !== 'paginate') {
    // Save the selections
}
Tray2's avatar

There are a few different ways, but the one I would use is handling the DOM updates with JavaScript, so when you push the button for the next page you only update the listed rows, nothing else.

1 like
Ligonsker's avatar

Has anyone understood what was the bot attempting to do? Because even if I add a parameter to the pagination, when I refresh the page the parameter remains, so it doesn't help if it remains when I refresh the page

@tray2 I currently save the selections in a JS object. I can even save it to LocalStorage. I am sending the request via AJAX btw.

But how can I make the distinction between a pagination and another type of refresh? The bot answer doesn't help here. I think I should've written in the first place that I use AJAX so that the bot would make a better answer. I Can give it a try and make a new post with this extra piece of information and see how he does

Snapey's avatar

i would send ajax request when the button is pressed so the server state is always in sync with the view.

Otherwise, you should have the entire table in a form and then submit the form when a pagination button is pressed

1 like
Ligonsker's avatar

@Snapey In my case there are thousands of rows, it would then make more sense to do the AJAX method instead?

And what should I do is that on every click on a pagination link, I will send the currently selected data, then save it in a session, then merge it with the selections from the db, then send the merged selections array back to the frontend?

Ligonsker's avatar

@Snapey Oh sorry you meant only the paginated page. So not thousands. But in this case it's not how the page is designed to work - i.e. to save on pagination link click. It has to be saved only when the Save button is clicked. In the option where table is a form, it will save every time.

In the ajax option, how can I do it in a way that will stop syncing when I refresh the page? Let's say that on every pagination link click I send the current selections, when do I clean this session in the backend? How will I know that the page was refreshed or browser closed?

Please or to participate in this conversation.