LiveCode LessonsData GridLiveCode Data GridUsing The Built-In Field EditorHow Do I Save Changes The User Makes In An Editor Field To An External Data Source?

How Do I Save Changes The User Makes In An Editor Field To An External Data Source?

When Calling EditFieldText with 3 Parameters (Simpler)

When calling EditFieldText with all three parameters (which is what a data grid column does by default) the data grid will automatically save the text that the user enters in the dgData array. That means that all you need to do is save the text of the editor field to your external data source in the CloseFieldEditor message. Note that if you save anything other than the text of pFieldEditor then your data source will not match the dgData value.

Here is an example script that goes in the Data Grid group script.

 

## An example that saves 
on CloseFieldEditor pFieldEditor
  local tRowID, tTable, tColumnBeingEdited, tNewText
	put the dgColumn of the target into tColumnBeingEdited
	put the text of pFieldEditor into tNewText
		
	## Save data to database using command I defined
	put "Person" into theTable
	## Get the unique id of the row in the database we want to edit
	put GetDataOfIndex(the dgIndex of the target, "id") into tRowID
	SaveDataToDatabase tTable, tRowID, tColumnBeingEdited, tNewText
end CloseFieldEditor

When Calling EditFieldText with 1 Parameter (More Flexible)

If you are working with data grid forms or decide to override the default behavior for data grid columns then you will make the call to EditFieldText yourself. For complete control over what gets saved you only pass in 1 parameter to EditFieldText. By passing in 1 parameter you are responsible for saving any changes to dgData.

Assume you made the following call in a data grid form:

EditFieldText the long id of field "Name" of me

CloseFieldEditor would still be sent when the user changes the contents of the field but would need to contain code for saving the changes to the dgData array.

Here is an example script that goes in the Data Grid group script.

## CloseFieldEditor placed in data grid script
on CloseFieldEditor pFieldEditor
	local tColumnBeingEdited, tNextText, tRowID, tTable, tDataA 
	put the dgColumn of the target into tColumnBeingEdited
	## Store UTF8 text
	put unidecode(the unicodetext of pFieldEditor, "UTF8") into tNewText
	
	## Save data to database using command I defined
	put "Person" into tTable
	## Get the unique id of the row in the database we want to edit
	put GetDataOfIndex(the dgIndex of the target, "id") into tRowID
	SaveDataToDatabase theTable, theRowID, theColumnBeingEdited, tNewText
	
	## Update dgData.
	## Setting dgDataOfIndex will refresh the data grid display as well as update dgData
	put the dgDataOfIndex[ the dgIndex of the target] of me into tDataA
	put tNewText into tDataA[theColumnBeingEdited]
	set the dgDataOfIndex[the dgIndex of the target] of me to tDataA
end CloseFieldEditor

6 Comments

JKrische

Point of clarification:

in the examples, your step that actually does the DB save is told the table, the column and the new value... but not the row ID? Would this not update EVERY row, putting the new value in the selected column for ALL rows? Since I can't imagine that was the intent here, how indeed are you telling the DB to update just the one row you wanted to change?

Trevor DeVore

@jkrische: yes, the id was unintentionally left out in the example. I've updated the example so that it includes code that gets the unique row id to use when updating the database.

Sean Cole

What is the SaveDataToDatabase command and what does it do?

Trevor DeVore

@Sean - The command isn't defined in the example script. It is an example of a command you would call to save the changes to your database. This is something you would have to implement yourself as it would be specific to your project.

Sean Cole

Ah, ok. It was 'how to save to an external source' that I was trying to understand (which is probably the bit in that command). I figured this was the best place to come. I'm trying to output a data grid to a data file directly (not as text file in csv but as something I can reload quickly as is as persistent storage as the custom properties are not storing them as I expected and return to they're original values at stack load.

Trevor DeVore

A sqlite database is one option. There is a lesson on using one at http://lessons.runrev.com/s/lessons/m/4071/l/30516-how-to-create-and-use-an-sqlite-database.

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.