Edit database table cell with text field

Hello!

I would like to edit a DB from a text field in Perspective.
image

I already have a named query binded to the text field to show that value on Perspective.

I would like to edit this text field to upload the value on the DB.

The text field PROPS are:

And I add an onBlur event scriopt on the textfield:

		
	new_kwlim_str = self.props.text
	codigo = self.getSibling("DNF-Code").props.text
	
	
	try:
	    new_kwlim = float(new_kwlim_str)
	    
	    system.db.runPrepUpdate(
	        "UPDATE T_SEDE SET KWLIM = ? WHERE CODIGO = ?",
	        [new_kwlim, codigo],
	        "DB-Name"  
	    )
	    
	    system.perspective.print("Updated KWLIM to " + str(new_kwlim) + " for CODE: " + str(codigo))
	    
	 
	
	      system.perspective.openPopup(
	        id="aprobar-popup",
	        view="Page/Aprobar",
	        params=popup_params,
	        title="Updated Successfully!",
	        modal=True,
	        width=400,
	        height=40
	    )
	
	
	except Exception as e:
	    system.perspective.print("Error updating KWLIM: " + str(e))
	    system.perspective.showNotification("Failed to update kW Lim.", "error")

I´m using the right DB-Name.

It doesn´t seem to work, I don´t see changes on the dB neither the notification

Maybe that kind of event is not the most suitable one for this case?

Any Idea what could be wrong with this aproach.

Thanks!

What is this, system.perspective.showNotification?

1 Like

Sorry about that, I updated the code with:

  system.perspective.openPopup(
	        id="aprobar-popup",
	        view="Page/Aprobar",
	        params=popup_params,
	        title="Updated Successfully!",
	        modal=True,
	        width=400,
	        height=40
	    )

Why not use the NumericEntryField?

1 Like

I adding kW into the textfield , but yes, in case I need it I can use a Numeric Entry Field,

do you think the script with event onBlur makes sense?

Cheers.

It doesn't make sense to use a text field for a float. With the numeric entry, you can verify that the number entered is within certain bounds, that it is in fact a number, and it has an onActionPerformed event that can be triggered from direct, protected, or button modes.

The onBlur event does not guarantee that the user is finished editing the field.

1 Like

Thanks @dkhayes117 , I will try with the numeric field.
image

1 Like

How are you going to compare a kW setting or reading with a database string? Keep the value and units separate in the database and in the user interface.

2 Likes

The print in the except should have told you what went wrong. It should be printing something along the lines of "ValueError: Can't convert string to float".

Also, why an onBlur event ? Just put a button to validate the value. I would hate to have a value sent to a database just because I clicked outside of a field. Particularly if there's a confirmation popup !

1 Like

Ok, thanks everibody, finally I used a Numeric Field with an onActionPerformed as @dkhayes117 suggested with this script:

def runAction(self, event):
	import sys
	import traceback
	
	tr
		new_kwlim_raw = self.props.value
		codigo = self.getSibling("codigo").props.text.strip()
	
		
		new_kwlim = float(new_kwlim_raw) if new_kwlim_raw is not None else 0
	
		system.perspective.print("Actualizando KWLIM a {} para {}".format(new_kwlim, codigo))
	
	
		system.db.runPrepUpdate(
		    "UPDATE T_SEDE SET KWLIM = ? WHERE CODIGO = ?",
		    [new_kwlim, codigo],
		    "PSS"
		)
	
		system.perspective.openPopup(
		    id="success-popup",
		    view="Page/Aprobar",
		    params={"codigo": codigo, "kwlim": new_kwlim},
		    title="Updated Successfully!",
		    modal=True,
		    width=400,
		    height=40
		)
	
	except Exception:
		exc_type, exc_value, exc_tb = sys.exc_info()
		error_details = traceback.format_exception(exc_type, exc_value, exc_tb)
		
		for line in error_details:
		    system.perspective.print(line)
		
		system.perspective.openPopup(
		    id="error-popup",
		    view="Page/ErrorPopup",
		    params={"message": "Failed to update. See console for details."},
		    title="Error",
		    modal=True,
		    width=400,
		    height=100
		)
		

and it´s working fine.
@Transistor I will show the units apart on a label, they don´t come from the DB I just added them and
@pascal.fragnoud I will work on the interface so it´s clear the user is changing some value on the database and doesn´t cause trouble.