Understanding DataGrids

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Understanding DataGrids

Post by Gage » Sat May 11, 2013 12:00 am

Hi all,

So I am trying to fully wrap my head around how a DataGrid operates. Specifically, for now, I am in need of understanding the "Form" style.

I understand that the behavior (namely populating the objects in the row template with data and laying out the objects in the row template) is specified by clicking on the "Row Behavior" button. This creates an invisible button on my Row Template card, which has a bunch of default script.

What I don't understand is as follows:
-If I write "of me" in the script of button "Behavior Script", does it refer to the DataGrid or the button?
-If the DG, does that mean that the DataGrid has a "behave as button "Behavior Script" " property built in?
-What is the best way to call the script on the button to activate, so that the DG will be populated and laid out --> does this happen in the DG script on MouseUp on the original card it is placed on of the mainstack?

Thanks in advance for the help! I have been poring over the available material and information, but can't seem to get the general picture well enough to make the DG Form work for my needs, even though my tasks are right up its alley.

Phil E.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Understanding DataGrids

Post by sturgis » Sat May 11, 2013 12:47 am

"of me" is the object executing the script. A behavior is assigned to an object and when the object is interacted with the context of the behavior script os the object itself not the button where the behavior resides.

So in this case the "of me" refers to the datagrid group.

SO button "mybutton" of me is button mybutton of group "mydatagrid"


When dealing with a datagrid, forget the button exists. You're interacting directly with the datagrid by setting properties and executing handlers and functions. The fact that the handlers and functions are referenced in the behavior button doesn't really matter.

Have you looked at http://lessons.runrev.com yet? Theres quite a bit of stuff there re: the datagrid. (near the bottom, a few links that then lead you down the rabbit hole) Make sure you look at the api and properties sections, and also the "what sort of things should I not do to avoid needless suffering"

Lots of good stuff to be found at that site.

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Understanding DataGrids

Post by Gage » Sat May 11, 2013 3:18 am

Wow, that makes perfect sense. Thank you for stating that so clearly, Idk what I had wrong in my head!

I have checked out as many of the examples as I could find. There are bits and pieces that help, but the problems I end up having often don't seem to align in my head with what they are explaining.

Thank you for your help.

Phil E.

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Understanding DataGrids

Post by Gage » Sat May 11, 2013 3:32 am

Follow-up to clarify:

So does this mean that if I have a field "My Field" in the "Row Templates" stack on card Row Template for xxxx, that I can write

put myVar into the text of field "My Field" of me

or

put myVar into the text of field "My Field" of card "Row Template for xxxx" of stack "Row Templates"


?

If I am understanding correctly, the DataGrid control in the mainstack contains all of the objects within the Row Template substack on the DataGrid's corresponding card.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Understanding DataGrids

Post by sturgis » Sat May 11, 2013 4:12 am

Well first, it would be either: put myVar into field "myField" of me
or: set the text of field "myField" of me to myVar Can't "put" directly into a property (in this case the text property of the field)

Next, your question depends on where the script is. In this case: field "myField" of me indicates a parent/child relationship. If the script is in the parent (owner) of the child, then yes you can write that script, but the script has to be in the parent (or a behavior assigned to the parent) so that the context is correct. So if "me" is a group named "fred" and the script is in the group, and the group contains a field "george" then yes you could write: put "whatevervalue" into field "george" of me

Same with cards and stacks. You could put a script in the card or stack and have a field on that stack and reference it with "me" where me would be the object that "owns" the field. Since the field is owned by both the card and the stack it should work fine.

Objects that can't own other objects can still use "me" but only in reference to themselves. So if you have a button and put a mouseup script in it with: set the label of me to ("fred" & random(300)) -- every time you click the button the label should change to a different random number (fred234)

This is handy with behaviors because you can write a script once, assign it to multiple objects and they can refer to themselves as "me" so certain things become easier. (you might also look at "this" in the dictionary)

As for things with the datagrid? It is SO complex that I try to only use the behavior scripts. However, you are correct in that if you are adjusting the script for your form datagrid, you can add a line like you indicated and place the value of a variable into a field. The way the datagrid redraws though, this would place the SAME value into that field in every line of the grid. This is where the pData comes in to play. When you assign data to your grid it is placed into an array that is numerically indexed. As the data is filled in it passes the data for the line in question to fillndata. Just tweak that handler to get the parts (pData is still an array, but only a truncated non numerically indexed array) so you can tell the parts where to go and what to do. So if you have data in the array, line 1, with sub keys under the 1 "fredfield" and "tonyField" each with an assigned value, then inside the fillndata (i'm probably typing the name of the handler wrong but you get the idea) To assign the values inside that handler you would: set the text of field "fredfield" of me to pData["fredfield"] (the name could be anything you assigned) and: set the text of field "tonyfield" of me to pData["tonyfield"] . The handlers in the behavior script are used during redraws of the grid, and when other events happen (like a double click to edit a value) and is run multiple times for each visible row. So if 3 rows are showing and you're at the top of the grid, the first time the grid is populated, fillindata is hit 3 times as is layoutcontrol, any clean up as values are scrolled off the screen are taken care of, and as new rows are scrolled in, fillindata, layoutcontrol etc handle the job of filling in the row.

Having said all that, my knowledge of the datagrid is limited. SO accuracy and mileage may vary!
Last edited by sturgis on Sat May 11, 2013 4:44 am, edited 1 time in total.

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm

Re: Understanding DataGrids

Post by Gage » Sat May 11, 2013 4:40 am

Alright! I am really beginning to see it come together.

Thanks for the stellar answers!

Phil E.

Post Reply