How do I auto-select newly added row in Table widget?

This question was asked on Discord by a user kraftathlete, following is the use-case:

How can I auto-selected the latest added row in a table?


For this, we can use the Appsmith store to save the last selected row in the table with a click of the button and on the default selected row property of the table widget you can bind {{appsmith.store.saveLastSelectedRow + 1}}


{{ 
 storeValue("saveLastSelectedRow", Table1.selectedRowIndex);
  UpdateQuery.run(
    () => SelectQuery.run(),
    () => showAlert("Did NOT Update!", "error")
  );
}}

I feel like this is close, but doesn’t quite solve the issue (at least for new records; it does seem the best option for updated records). Table1.selectedRowIndex only pulls the selected row number based of the table (not the primary key). Plus, it assumes you have a row selected, so if you’re inserting a new record then this won’t work.

I looked through a bunch of options, but honestly the easiest way to get the latest selected row is to just setup your query to order the data by Date Modified descending (assuming you have this column). Then you can just set the default row to 0 to always select the top row, which will always by default be the newest record on that table when the data is queried again.

Hope this helps someone!

1 Like