GUI - Perspective - Confirm / Cancel popup

I am working in perspective and wondering if there is a way to mimic this in perspective. I have a delete button and want to have user confirmation before deleting.

# If a row is selected, ask for confirmation before deleting the rows.
    if system.gui.confirm("Are you sure you want to delete " + str(len(selRows)) + " row(s)?", "Are You Sure?", 0):

There are multiple ways to implement this, depending on the structure of the rest of your project. The easiest is probably a popup. When the button is clicked, display a popup, on the popup, when the confirmation button is clicked use system.perspective.sendMessage() to send a message to a message handler on the view which can then run the delete operation.

I would recommend writing all of your CRUD operations in a script module as pure functions which can then be called from multiple places in the project.

1 Like

I have the Delete as a named query.

Sure, doesn't really change much. You still need to call the named query, and the view is going to have all of that information readily available. Better not to pass the values around needlessly, just ask for confirmation and then execute only with given that confirmation.

In case it isn't clear, there's no way to do this in a single script.

1 Like

So just do a popup in the delete button script and if confirm is chosen in the popup, send a message to message handler on the delete button component and run the named query ?

2 Likes

Yes, just like that. If you want to make a generic popup button for this kind of duty, parameterize not just the content to display, but also the message handler name, scope, and perhaps a unique key to include in the payload. This makes the message targeting and reception handling very robust.

4 Likes

When the message handler says, "This method will be called when a message with the matching type code arrives at this component". Is 'type code' the value entered in for 'Message Type'.

Yes. I like to think of this as the handler's name.

Which listen scope would make most sense if I am opening a popup when the delete button is hit? Session, Page, or View?

What about using the One-Shot button? I've been using them in a lot of places in place of normal buttons because it gives you confirmation options and the script you define only runs if the user hits the "Yes" or "Confirm" button. I know I'm derailing the conversation but wanted to bring up another option that I love to use.

1 Like

I would expect you to need page scope.

1 Like

Hmm. I see there is a require confirm one shot button.

I have deleteProductHandler Message Handler on my delete button. My delete button onActionPerformed opens popup with a confirm button with code

def runAction(self, event):
	messageType = 'deleteProductHandler'
	system.perspective.sendMessage(messageType) 

Nothing is happening. The code inside the messageHandler is not running.

How does it know where to send the message. The handler is on a component in a different view.

Page scope is used by default if the scope parameter isn't supplied.

You must provide a payload, even if it is just an empty dictionary.

def runAction(self, event):
	messageType = 'deleteProductHandler'
	system.perspective.sendMessage(messageType,{})

https://docs.inductiveautomation.com/display/DOC81/system.perspective.sendMessage

That did not fix the issue, but ok that's good to know.

I think its because the message handler is configured on a different view

I need to add a pageid i believe

Is the message handler configured to listen on page scope?