Common Datagrid Column Properties

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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Common Datagrid Column Properties

Post by phaworth » Wed Feb 03, 2010 11:44 pm

Many of the columns I use in different datagrids share common formatting properties, eg currency, date, etc. So far I have created a column behaviour every time I use a column that needs formatting in some way, with the appropriate code to display a currency value, date, etc but I'd really like to find a way to not have to keep repeating the code.

I already have code that translates between my database storage formats and display formats when the data goes into fields or buttons or any other objects. I guess I'm looking for a way to be to have that code be invoked every time data is loaded into a datagrid without having to keep repeating the code.

Any ideas?

Thanks,
Pete

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

Re: Common Datagrid Column Properties

Post by trevordevore » Fri Feb 05, 2010 3:43 pm

One approach would be to define a getProp handler in your Data Grid group script that returned the type for any column in the Data Grid.

Code: Select all

getProp uColumnType pColumn
    switch pColumn
        case "start date"
            return "date"
        ...
        default
            return "text"
    end switch
end uColumnType
Then you could create a generic "default column behavior" button script that used the uColumnType custom property. The FillInData handler might look something like this:

Code: Select all

on FillInData pData
    set the text of me to FormatData(pData, the uColumnType[the dgColumn of me] of the dgControl of me
end FillInData
FormatData is just a generic function defined somewhere in the message hierarchy that formats the data based on the type.
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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Sat Feb 06, 2010 7:11 pm

Thanks Trevor. That's actually very close to how I deal with this situation when handling fields, buttons, etc on the form so I have handlers that do all that stuff already but wasn't sure how to integrate them with a datagrid.

The absolute ideal solution would be that the code you gave for the FillInData handler would be the default code you create when any datagrid is placed on a card so I wouldn't have to define a custom behaviour for every column in every datagrid. I have naming conventions for objects that determine what type of db data they refer to and the same principle could be used to name columns in a datagrid.

Thanks,
Pete

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

Re: Common Datagrid Column Properties

Post by trevordevore » Mon Feb 08, 2010 9:11 pm

Pete,

You could update your revDataGridLibrary to have the code you need as the default. The default script for Data Grid forms is stored in a custom property:

Code: Select all

the uDefaultScript of button "Default Script" of stack "revDataGridLibrary"
The default script for Data Grid table columns is here:

Code: Select all

the uDefaultScript of button "Default Column Script" of stack "revDataGridLibrary"
Just update these custom props with the scripts you want to be the defaults. You would need to update the scripts whenever you download a new version of Rev.

If you don't want to go the above route you still don't need to set a custom behavior for each column. Just set the "default column behavior" property of the Data Grid to the button with the behavior script you want to use. The script will be used by default for all columns in your Data Grid.
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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Mon Feb 08, 2010 9:28 pm

Thanks Trevor, sounds like either of these solutions would work.

I'm inclined to use the second one, setting the "default column behavior" and I just found how to do that in the Datagrid manual.

Thanks,
Pete

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Tue Feb 09, 2010 12:30 am

Hi Trevor,
Got this working great for any new datagrids. Now I want to convert my existing datagrids to use my default column behaviour but the column behaviors still show my original code in there, not the default code. I assume that's because the default code is copied over when the column behavior is created?

I could go through and delete all my existing datagrids then recreate them from the default grid I've set up but there's several of them so I'm wondering if there's another way to do this?

Thanks,
Pete

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

Re: Common Datagrid Column Properties

Post by trevordevore » Tue Feb 09, 2010 4:39 pm

In the current version of the Data Grid library a custom column will not have the "default column behavior" assigned to it. Basically the Data Grid assumes that if you created a custom column you are using a custom behavior. I'm going to modify the library so that custom column controls inherit the standard column behavior or the "default column behavior" (if set) if the custom column control has no behavior assigned to it.

Right now you have two options -

1) Assign the behavior property of your custom column control to the same button as the "default column behavior" property.

2) Update the Data Grid behavior script with the new code.

Code: Select all

edit script of btn "Data Grid" of stack "revDataGridLibrary"
Search for string:

Code: Select all

copy control theColumn of theTemplateGroup to group theColumn of group "dgList" of me
Replace with this code:

Code: Select all

copy control theColumn of theTemplateGroup to group theColumn of group "dgList" of me
 ## Use default behavior if control doesn't have one assigned.
if the behavior of it is empty then
    if the dgProps["default column behavior"] of me is empty then
        set the behavior of it to the long ID of button "Default Column" of theResourceStack
    else
        set the behavior of it to the dgProps["default column behavior"] of me
    end if
end if
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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Wed Feb 17, 2010 6:45 am

Thanks Trevor. I used the first of the two methods you suggested and it works just fine. But it would be nice if custom columns inherit the default column behavior as you are planning. How can I find out when that modification has been done?
Thanks,
Pete

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Wed Feb 17, 2010 10:22 am

Hi Trevor,
I'm now finding that I need logic that is specific to certain custom controls in the datagrid in addition to what I use in the default column behaviour.

For example, one column in one datagrid contains a checkbox button. The common logic deals with showing it as checkd or unchecked but in addition to that, I need to change the contents of other objects on the card depending on the hilite of the checkbox.

What's the best way to achieve that without compromising the common logic that is in my custom behaviour?

Thanks,
Pete

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

Re: Common Datagrid Column Properties

Post by trevordevore » Wed Feb 17, 2010 3:45 pm

The modification has already been made and submitted for the next release of Revolution.

Regarding notifying controls when the hilite in the Data Grid changes - A simple way of doing this is dispatching a message when a certain event happens in your Data Grid. For example, if you want to notify your program when a control is clicked on you might dispatch "HiliteButtonClicked" whenever the user clicks on the button.

Code: Select all

dispatch "HiliteButtonClicked"
You can pass any parameters when dispatching the message. The nice thing about dispatch is no errors occur if the message isn't handled so you can add it to your custom behavior and not worry about handling the message everywhere.

Another approach is to use a broadcasting system that broadcasts specific messages to controls whenever the checkbox is clicked on or your underlying data structure is changed. That is a little more involved though.
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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Wed Feb 17, 2010 6:05 pm

Thanks Trevor, the dispatch solution sounds like the way to go.
Pete

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Wed Feb 17, 2010 9:30 pm

Hi Trevor,
Well I guess I'm not quite there yet. I tried the dispatch command with no target and nothing happened. I'm sure that's because the dispatch command is coming from the datagrid template card and the object I want to send the dispatched event to is on the card that contains the datagrid so wouldn't be in the message path.

It sounds like I have include a target object with the dispatch command but I want to preserve the ability of my default column script to be used for any datagrids. I'm thinking of defining a custom property of the button in the datagrid column that will define the target object for the dispatch command. Does that sound like a reasonable solution?

Thanks,
Pete

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

Re: Common Datagrid Column Properties

Post by trevordevore » Wed Feb 17, 2010 9:39 pm

If you are using "dispatch" in the behavior script then the command will come from inside your Data Grid when it is sent, not the template card. You don't need to add a target. Just try handling the message that is being dispatched in the Data Grid group script and see if it gets triggered.
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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Wed Feb 17, 2010 9:50 pm

That works perfectly Trevor, thank you.
Pete

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Common Datagrid Column Properties

Post by phaworth » Thu Feb 18, 2010 6:19 am

Well, The fun just keeps coming! The way I've set up my shared datagrid is that I have it on a card which holds a library of various objects that I use in my app. My datagrids have a number of custom properties so I've defined them on the library copy and just change their contents in the application version of the datagrid. The library datagrid is also set up to use my custom default behaviour script in a button on the library card so I don;t have to set that up for every new datagrid.

When I want to use a datagrid, I copy and paste it from the library card onto my app card. I just noticed that results in every datagrid pointing to the same template. All is working fine but that feels like it could cause problems. Is there a way I can force each pasted datagrid to create a new template? Or is there a better way to do this? For example, maybe I should set up a new datagrid from the Tools palette then have a simple script that sets up it's default behaviors and my custom properties

Thanks,

Pete

Post Reply