Popup Confirmation for Table, before Row Selection Changes?

We are currently working on a mechanism that allows users to click on any row, which then opens a new view based on the row description. This functionality is working as expected. However, we've encountered an issue where users can lose their entered information if they change the row selection. To address this problem, we've implemented a warning popup to hold users in their current view.

Here's how the script works, which runs on the 'onSelectionChange' event of the table (even though I acknowledge that it might not be the optimal event, as it only triggers when there's a row selection change, similar to 'onRowClick'):

#onSelectionChange event script on Table
def runAction(self, event):
	logger = system.util.getLogger("myLogger")

	# Get the currently selected row index
	selected_row_index = self.props.selection.selectedRow
	
	# Get the previously saved row index
	previous_row_index = self.custom.preSelectedRow
	
	# Log the selected and previous row indices for debugging
	logger.info("Selected Row Index: %s, Previous Row Index: %s" % (selected_row_index, previous_row_index))
	
	# Check if the current and previous row indices are different
	if selected_row_index != previous_row_index:
	    # The selected row index has changed, open the warning popup
	    popup_params = {
	        "id": "01",
	        "view": "TestYP/Template/WarningPopup"
	    }
	    
	    # Open the warning popup and store the result in a variable
	    popup_result = system.perspective.openPopup(**popup_params)
	    
	    # Check the result of the popup (assuming it returns True or False)
	    if popup_result == True:
	        # User selected True, so do not change the row selection
	        logger.info("User chose to stay on the current row.")
	        
	        # Restore the previous row selection
	        self.props.selection.selectedRow = previous_row_index
	    else:
	        # User selected False or closed the popup, allow the row to change
	        logger.info("Row Index Changed!")
	
	# Save the current selected row index to the custom property for the next run
	self.custom.preSelectedRow = selected_row_index
  1. It retrieves the currently selected row index in the table
  2. Retrieves the previously saved row index, for comparison
  3. Logs both the selected and previous row indices for debugging purposes
  4. It checks if the current and previous row indices are different
    * If they are different, it attempts to open a warning popup, expecting a True/False response
    * If the user selects 'True' in the popup, it prevents the row selection from changing and keep the previous row selection - where I'm struggling
    * If the user selects 'False' or closes the popup, it allows the row selection to change
  5. Finally, it saves the current selected row index as the previous row index for future reference

Now, my question to achieve the desired behavior is:

How can we modify this script or choose the correct event to ensure that the table row selection does not change until we receive a 'True' or 'False' input from the popup?
I'm also open to explore better solutions if this approach might not work.

You can't. Not like this.
Opening popups is not blocking, so there's nothing to stop the rest of the script from executing while the user clicks on the popup's buttons.

I'd change the way things work.
Now, without actually trying it because I really don't have that kind of time on my hands, I think the path of least effort would be to prevent the bit of code that opens the view on row selection to execute if one is already opened.
But I can think of other solutions, depending on how that view is actually opened and the behavior you actually want.
Is it a popup ? make it modal, so the user can't interact with the table while it's opened.
Is it an embedded view on the side ? disable selection on the table while it's displaying something.

Tell us more about your setup and what the goals are, so we can help you find a proper solution.

3 Likes

Thanks for your response, @pascal.fragnoud I've successfully implemented a popup. But...

Now, I'm looking for the best way to detect if any data has been modified in the current view and trigger a popup if changes are made by the user. Any suggestions? How to achieve this task?

That depends on your data and how it can be modified. You'll need to tell us more about your use case.