livecode.com
  • Home
  • Download
  • Developers
  • Academy
  • Community
  • Store
  • Support
  • About
LiveCode Lessons » LiveCode Data Grid » How Do I Save Changes The User Makes In An Editor Field To An External Data Source?

Topics

  • Introduction 5
    • What is the Data Grid?
    • Can You Show Me Some Examples?
    • How Do I Create My First Data Grid Table?
    • How Do I Create My First Data Grid Form?
    • Example: Creating a List of People
  • Data Grid Fundamentals 6
    • What Is This "Data Grid Templates" Stack That Appeared in My Project?
    • What is a Row Template?
    • What is a Column Template?
    • How Do I Populate a Data Grid With Data?
    • How Do I Customize A Form's Row Template?
    • How Do I Customize A Table's Columns?
  • Working With Data Grids (Forms & Tables) 14
    • How Do I Determine The Selected Line?
    • How Do I Get Data Associated With a Row or Column?
    • How Do I Add A Row Of Data To An Existing Data Grid?
    • How Do I Update Data In a Row?
    • How Do I Clear Data From a Data Grid?
    • How Do I Add a mouseDown Event To The Data Grid Without Breaking It?
    • How Can I Store An Option Menu Value When The User Makes a Selection?
    • How Do I Refresh a Data Grid After Making Changes to a Template Through Script?
    • How Do I Use A Template In Multiple Data Grids?
    • How Can I See What The Data Grid's Internal Array Currently Looks Like?
    • How Do I Get Aggregate Values for Columns?
    • How Do I Determine If the Data Grid Has Focus?
    • How Do I Export Data From A Data Grid?
    • How Do I Work with Checkboxes in a Data Grid?
  • Working With Data Grid Tables 13
    • How Do I Change Column Alignment?
    • How Do I Sort By A Column?
    • How Do I Resize Columns?
    • How Do I Override the Default Behavior For Rendering Data to a Cell?
    • How Do I Determine If a User Clicks In The Table Header?
    • How Do I Display a Contextual Menu When the User Clicks on a Column Header?
    • What If I Need to Work With htmlText, rtfText or unicodeText?
    • How Do I Display Line Numbers in a Table?
    • How Do I Customize Column Sorting?
    • How Do I Disable Column Sorting?
    • How Do I Perform An Action After the User Sorts a Data Grid?
    • How Do I Align Decimals in a Column?
    • How Can I Colorize Individual Lines in a Table?
  • Working With Data Grid Forms 4
    • How Do I Create a Form with Variable Line Heights?
    • How Do I Sort Records By A Specific Key's Values?
    • How Do I Create Rows That Can Expand/Contract?
    • How Can I Speed Up Drawing When "fixed row height" is False?
  • Using The Built-In Field Editor 6
    • How Do I Open a Table Cell For Editing?
    • How Can The User Edit Field Content in a Data Grid Form?
    • How Can I Edit The Text as UTF-8, UTF-16 or HTML?
    • How Can I Select The Text in the Edit Field When It Opens?
    • How Do I Save Changes The User Makes In An Editor Field To An External Data Source?
    • How Can I Customize The Field Editor Behavior?
  • Building Standalones With The Data Grid 1
    • What Do I Need to Do To Deploy a Standalone With A Data Grid?
  • Useful Things To Know 1
    • What Sorts of Things Should I Not Do In Order To Avoid Needless Suffering?
  • Advanced Options 3
    • Displaying Large Amounts of Data
    • Creating A Data Grid By Hand
    • How do I create a Datagrid dynamically from tab delimited data
  • API and Properties 3
    • Data Grid Properties
    • Data Grid API
    • Template Custom Properties & Messages

Last Updated

Mar 29, 2012

Download Manual PDF

Other Resources

  • Getting Started with LiveCode

  • Get Up and Running with LiveCode
  • Getting Started with LiveCode Development
  • LiveCode Concepts

  • Features, concepts and aspects of LiveCode
  • LiveCode Lessons

  • How To - Step-By-Step Guides To Tasks In LiveCode
  • How To - LiveCode Server Tasks
  • How To - LiveCode Mobile Tasks
  • How To - LiveCode Sample Scripts
  • How to - LiveCode Marketplace Products
  • How to Purchase and License LiveCode
  • Tutorials

  • Creating a Video Library Application
  • Data Grid

  • LiveCode Data Grid
  • Data Grid Tips & Tricks
  • Converting the Stock Program

Comments

6 for this lesson

  • Prev: How Can I Select The Text in the Edit Field When It Opens?
  • Next: How Can I Customize The Field Editor Behavior?

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
    put the dgColumn of the target into theColumnBeingEdited
    put the text of pFieldEditor into theNewText
        
    ## 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 theRowID

    SaveDataToDatabase theTable, theRowID, theColumnBeingEdited, theNewText
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
    put the dgColumn of the target into theColumnBeingEdited
    ## Store UTF8 text
    put unidecode(the unicodetext of pFieldEditor, "UTF8") into theNewText
    
    ## 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 theRowID

    SaveDataToDatabase theTable, theRowID, theColumnBeingEdited, theNewText
    
    ## 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 theDataA
    put theNewText into theDataA[theColumnBeingEdited]
    set the dgDataOfIndex[the dgIndex of the target] of me to theDataA
end CloseFieldEditor

  • Prev: How Can I Select The Text in the Edit Field When It Opens?
  • Next: How Can I Customize The Field Editor Behavior?

Comments (6)

JKrische Monday Aug 16 at 04:52 AM

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 Tuesday Aug 17 at 02:06 AM

@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 Wednesday Mar 28 at 12:54 PM

What is the SaveDataToDatabase command and what does it do?

Trevor DeVore Thursday Mar 29 at 12:40 AM

@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 Thursday Mar 29 at 01:25 AM

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 Thursday Mar 29 at 01:35 AM

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

E-Mail me when someone replies to this comment
Brought to you by RunRev Ltd, Registered in Scotland, No. SC200728