Keep selected rows persistent in lightning datatable pagination

I have got this request many times from my blog visitor to create a datatable which can have persistent selected rows.

Use Case
  • When a user selects some rows on a page and navigates to previous/next page, currently selected rows should be stored.
  • When the user selects new rows in the next page, it should these newly selected rows with previous ones which were selected on the last page.
  • When the user navigates back to the last page, the user should see the selected rows he selected last time.
  • If the user deselects any row, it should delete that row from the stored rows as well.

This blog will cover these requirements and will help you to make your selected rows persistent.

Check out the code below:

Component – LightningPaginationWithPersistentSelectedRows.cmp

Controller – LightningPaginationWithPersistentSelectedRowsController.js

Helper – LightningPaginationWithPersistentSelectedRowsHelper.js

Apex – AccountController.apxc

Code Highlights
  • The above code uses Lightning Pagination – Using Server Side Controller And Client Side Cache post’s code as the base and we are modifying a few things in this code.
  • We are modifying the row Id and appending with the page number. This helps to identify the rows which belong to a particular page. So in case you want to update these rows or doing any DML operation, you need to make sure you extract the real Ids from the row again, meaning remove the page number.
  • Attribute “Selection” is storing all the rows selected from all the pages.
  • Attribute “hasPageChanged” is helping us to identify if the next page or previous page is clicked.
  • Method “onRowSelection” is the one where all the magic happening.

I am using and modifying the code from previous posts that I have written on the data table. Check out these posts for further solutions on the data table:

Check out below video from “Lightning Component Development Series” as well for lightning data table explanation:

The above code example is only a workaround and not a permanent solution to keep selected rows persistent in the lightning data table as this feature doesn’t come out of the box yet. Refer the code and modify as per your use case.

(Visited 3010 Times)

7 thoughts on “Keep selected rows persistent in lightning datatable pagination

  1. Raja

    It is really helpful for so many people. Thank You! :).

    One Suggestion. If you use client side, instead of server side actions it will help more. Is there any possibility. I am also not sure, have to check it.

  2. Jafe Who

    It is really helpful for me. Thank You! :).
    I have a little question.
    I write so many console.log() in controller’s onRowSelection() function,then I find out that if i go straight to the second page and click one checkbox,The Id of that line wouldn’t be save into v.selection.But if i click twice again,that Id will be save into v.selection.
    After I check a little while,the problem is program go to
    component.set(“v.hasPageChanged”, false);
    It cause the problem happened.
    I write a button and it’s disabled attribute is related with v.selection value,if value is empty,disabled=true,I can’t click that button.
    because it seems to click the checkbox ,actually it’s not in v.selection
    So how to deal with this problem?


Leave a Reply

Your email address will not be published. Required fields are marked *