User input in datagrid

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
Fjord
Posts: 145
Joined: Sat Dec 06, 2008 6:18 pm

User input in datagrid

Post by Fjord » Fri Nov 27, 2009 2:38 pm

Hi,
I'm building an app with datagrid. Colum 1 is "projectName", column 2 is "initialBudget", columns 3 is "expensesAsOfToday", column 4 is "amountRemaining", the latter is computed. Therefore I want the user to be able to modify or add data in columns 1 to 3 but not 4.
The question is "how do I let the user access the datagrid"? I'm thinking of changing dynamically the "Allow Text Editing" property but how can I know which cell the user is trying to edit? and BTW how do I access that property?
Or should I use another strategy?
--
François

Fjord
Posts: 145
Joined: Sat Dec 06, 2008 6:18 pm

Re: User input in datagrid

Post by Fjord » Fri Nov 27, 2009 8:44 pm

I'm adding another constraint: I want the datagrid lines to catch double clicks to go to another card (that shows the detailed data of the line). What's the best practice for this case where I want the grid to be both edited and catch double clicks?
--
François

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Re: User input in datagrid

Post by trevordevore » Tue Dec 01, 2009 3:30 pm

With a Data Grid you can define event handlers (i.e. mouseDoubleUp, mouseUp, etc.) just like you would for other controls. The difference is that there are two primary locations you can define those handlers:

1) The data grid script.

2) The behavior script (for a row or column).

mouseDoubleUp

I would put your mouseDoubleUp handler in the data grid script. Take a look at this lesson that talks about getting data for a row.

How Do I Get Data Associated With a Row or Column?

A mousedoubleUp might look something like this if placed in the data grid script:

Code: Select all

on mouseDoubleUp pBtnNum
    if pBtnNum is 1 then
        put GetDataOfIndex(the dgHilitedIndex of me, YOUR_UNIQUE_VALUE) into theUniqueValue 
        -- Load card with record...
    end if
end mouseDoubleUp
Editing

To see how editing is currently handled for a table you can look at the default script for rendering data to a cell. The mouseDoubleUp handler shows how editing is handled.

How Do I Override the Default Behavior For Rendering Data to a Cell?

You could modify the default behavior so that editing was only allowed for the columns you are interested in. The default code is:

Code: Select all

on mouseDoubleUp pMouseBtnNum
    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
Just update the if statement so that it checks to see if the dgColumn of me is not "amountRemaining".

Allow editing property

The API info in the docs shows how to access properties. You are looking for the "allow editing" property.

Data Grid Properties
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Fjord
Posts: 145
Joined: Sat Dec 06, 2008 6:18 pm

Re: User input in datagrid

Post by Fjord » Sun Dec 13, 2009 11:43 pm

Thanks Trevor. I did my homework (read and experiment) :wink: but am still stuck with the following. I have this array myData which I map to the grid myGrid:

Code: Select all

set the dgData of group "myGrid" to myData
I can reflect changes to index 2 column 2 of myData to myGrid with something like

Code: Select all

   dispatch "SetDataOfindex" to group "myGrid" with 2,"Col2" , 0
   dispatch "RefreshList" to group "myGrid"
But what I really need is to let the user change the grid then reflect the change to myData. To add a line, I have a "+" btn that inserts a new line and lets the user type data in it

Code: Select all

   get the keys of myData
   sort it numeric descending
   put 1 + line 1 of it into newIndex
   put "type something in this line" into myData[newIndex]["col1"]
   put "type something in this line" into myData[newIndex]["col2"]
   dispatch "AddData" to group "myGrid" with myData[newIndex] , 0
   dispatch "EditCellOfIndex" to group "myGrid" with "Col1" ,newIndex
Then the user can type anything in the new line, or in other lines. This is standard behaviour. My problem now is to catch a user modification of a cell so that I can modify my data accordingly. Do I have to reprogram the entire behaviour of the grid? or should I do

Code: Select all

put the dgData of group "myGrid" into myData
each time? this seems time-consuming, and when should I do that?
--
François

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Re: User input in datagrid

Post by trevordevore » Mon Dec 14, 2009 2:25 pm

Here is a lesson from the manual that shows how to save data to an external data source.

How Do I Save Changes The User Makes In An Editor Field To An External Data Source?
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Post Reply