Saving updated data from a datagrid table

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Saving updated data from a datagrid table

Post by Simon Knight » Thu May 26, 2011 11:25 am

Hi,
I have just spent some time going around in circles so I hope that someone can help me out. I have a custom stack property that holds tab delimited data loaded from a file. I wish to view and edit the data so have copied it into a datagrid table. The default datagrid settings allow me to edit the data and I can save the data back to the custom property by using the the dgtext property. So far so good.

My problems started when I decided that it would be a good idea if edits to the datagrid data were saved automatically once the user exited the cell. I have tried intercepting the closefieldeditor message but I don't seem to be able to pass the message on so that the edit completes. I have also tried creating a custom behavior as described in the documentation (page 147) but this just disables any editing.

What am I doing wrong and what is the simplest method of saving the data?

many thanks

Simon
best wishes
Skids

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving updated data from a datagrid table

Post by Klaus » Thu May 26, 2011 12:08 pm

Hi Simon,

we can only guess without looking at your script(s)!
Maybe you just left a "pass XYZ" in your custom behavior script?


Best

Klaus

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Saving updated data from a datagrid table

Post by Simon Knight » Thu May 26, 2011 12:26 pm

Using the following code snips disabled default field editing in my datagrid.
from the DG group:

Code: Select all

on preOpenFieldEditor pFieldEditor
   set the behavior of pFieldEditor to the long ID of btn "CustomFieldEditorBehavior"
end preOpenFieldEditor

On StoreICDDataGrid
   put "StoreICDDataGrid has fired"
   // Store the datagrid to the custom function
   set c138Fields of stack "ICDdataDescription"  to dgtext of group "dglists"
   put "data stored"
end StoreICDDataGrid
The code in the button referenced above:

Code: Select all

--> all handlers


on escapeKey
    send "DeleteFieldEditor false" to the dgControl of me in 0 seconds
end escapeKey


on closeField
    ## Don't delete editor within same message
    send "DeleteFieldEditor" to the dgControl of me in 0 seconds
end closeField


on exitField
   Call StoreICDDataGrid
    send "DeleteFieldEditor" to the dgControl of me in 0 seconds
end exitField


on returnInField
    if the autotab of me then
        send "DeleteFieldEditor" to the dgControl of me in 0 seconds
    else
        pass returnInField
    end if
end returnInField


on enterInField
    if the autotab of me then
        send "DeleteFieldEditor" to the dgControl of me in 0 seconds
    else
        pass enterInField
    end if
end enterInField


on tabKey
    if the autotab of me then
        send "DeleteFieldEditorAndOpenNext" to the dgControl of me in 0 seconds
    else
        pass tabkey
    end if
end tabKey


on selectionChanged
    ## don't pass as selectionChanged is reserved for group
    ## developer can override behavior to process selectionChanged
end selectionChanged
best wishes
Skids

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving updated data from a datagrid table

Post by Klaus » Thu May 26, 2011 6:35 pm

Hi Simon,

looks OK, but "call" is probably the troublemaker!
And some missing "the"s :-)

Put this handler into the stack or card script:

Code: Select all

command StoreICDDataGrid
   put "StoreICDDataGrid has fired"
   set THE c138Fields of stack "ICDdataDescription" to THE dgtext of group "dglists"
   put "data stored"
end StoreICDDataGrid
And change this:

Code: Select all

on exitField

  ## "call" this handler by just its name ;-) ## Check "call" in the dictionary!
  StoreICDDataGrid
  send "DeleteFieldEditor" to the dgControl of me in 0 seconds
end exitField
Best

Klaus

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Saving updated data from a datagrid table

Post by Simon Knight » Fri May 27, 2011 10:47 am

Hi Klaus,

I have managed to get it working and the main problem was the location of my behavior button. Basically the only way to get it to work was to place the button on the same stack as the datagrid.

I seem to be having similar problems with setting the default column behavior; I have followed the guide and proved that my datagrid is set to use my behavior button (by using code to retrieve the default column behavior) but I am unable to get the grid to fire any of the routines. I have littered the copy of the default code with indicators to try and prove it is working, it is not :(

Code: Select all

--> all handlers


on FillInData pData
    -- This message is sent when the Data Grid needs to populate
    -- this template with the column data. pData is the value to be displayed.
   set the text of the long ID of me to pData ## temp workaround for
   TruncateTail the short id of me, "..."
end FillInData


on LayoutControl pControlRect
   -- A default column is just a field. Nothing to change here.
   TruncateTail the short id of me, "..."
end LayoutControl


on ResetData
    -- Sent when column data is being emptied because the control is no longer being used to display data
    set the text me to empty
end ResetData


on PreFillInData
   -- Sent right before new data is going to replace existing data in the column
   beep
   put "prefill in data fires" & cr after fld"debug"
end PreFillInData


setprop dgHilite pBoolean
    -- This custom property is set when the highlight of your column template has
   -- changed. You only add script here if you want to customize the highlight.
   beep
   put "dghilite has fired" & cr after fld"debug"
   answer "dghilite firse"
    if pBoolean then
        set the foregroundColor of me to the dgProp["hilited text color"] of the dgControl of me
    else
        set the foregroundColor of me to empty
    end if
end dgHilite


getprop dgDataControl
    -- Required by library so that it can locate the control.
    return the long ID of me
end dgDataControl


-- Data grid will call this if a user action asks to edit cell content.
command EditValue
   put "Edit Value has fired " & cr after fld"debug"
    EditFieldText the long ID of me, the dgIndex of me, the dgColumn of me
end EditValue


on mouseDoubleUp pMouseBtnNum
   put "mouse double up has fired" & cr after fld"debug"
    if pMouseBtnNum is 1 then
        if the dgProps["allow editing"] of the dgControl of me \
                and the dgColumnIsEditable[the dgColumn of me] of the dgControl of me then
            -- Edit field contents if the user double-clicks
            EditCellOfIndex the dgColumn of me, the dgIndex of me
            exit mouseDoubleUp
        end if
    end if
    pass mouseDoubleUp
end mouseDoubleUp
I'm wondering if I should cut my losses and revert to a "Basic Table Field"

best wishes
Simon
best wishes
Skids

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Saving updated data from a datagrid table

Post by Simon Knight » Fri May 27, 2011 11:52 am

Hi,

Grasping at straws I re-installed Livecode (4.6) and the datagrid started using my behavior script.

Simon
best wishes
Skids

Post Reply