Page 1 of 2

More datagrid help needed!

Posted: Fri Nov 16, 2012 9:03 am
by paulsr
I'm having one of those "you can't teach old dogs new tricks" days.

Despite having gone through (which means I haven't read every line) the 230 page Livecode_Data_Grid.pdf helper, I'm still utterly confused.

Okay, with Klaus' help, I have a working datagrid which loads quickly and looks pretty, but I need to customize it.

I'm hoping someone will be kind enough to explain to me, using simple words, the right way to do the following...

My datagrid is for info only. I don't want users to be able to tap the headers to change sort order. I found a forum message that said:

"The correct way to really lock the column header is to add this handler in the datagrid group:"

Code: Select all

on SortDataGridColumn pColumn
## Remain this handler empty for disabling the sort capability of a datagrid
end SortDataGridColumn 
Fine, but what/where is a "datagrid group"? What do I do with this tiny piece of code?

And now for something more complicated:

I'm reading data from a text file, building an array with the data I want in the grid, and then throwing the array to the datagrid with the set the dgData command. This, I believe is the normal way to do things.

In the text file, there are fields which are not transferred to the array/datagrid, but these fields I need to affect the formatting.

For instance, if a field in the file contains "A" I want to display some other field in the same line (which does go into the array) in red. Or set the background to some color, or maybe the whole line.

But by the time I've finished building the array I no longer have the data that determines the color. Not that I can find the way to set the color of a datagrid cell anyways!

So maybe I should start with a simpler question:

If, in column 2 of my datagrid, I have data which says "START" or "END" how do I color the STARTs in green and the ENDs in red?

Maybe I should understand that first!

Many thanks for teaching the old dog...

--paul

Re: More datagrid help needed!

Posted: Fri Nov 16, 2012 12:02 pm
by Klaus
Hi Paul,

1. the DataGrid itself is in fact a group, although with some tricks Livecode displays it as a single object!
So simply add the "sort preventing" script right to the DataGrid and it will work as exspected.

2. This is a quite tricky, since it requires that you add a custom behavior to your datagrid and datagrids
of type TABLE do not have one per default!

This is however well documented in the DataGrid PDF, get it here (top left):
http://lessons.runrev.com/m/datagrid

I am reading that PDF currently almost daily for the last couple of weeks since:
a. this is a REALLY complex beast and
b. I also need something like this in my current project :D

Drop a line if you get stuck, I managed this finally a couple of days before and hope
I did not forget how I finally managed it 8)


Best

Klaus

Re: More datagrid help needed!

Posted: Fri Nov 16, 2012 12:32 pm
by paulsr
Klaus wrote:Hi Paul,

1. the DataGrid itself is in fact a group, although with some tricks Livecode displays it as a single object!
So simply add the "sort preventing" script right to the DataGrid and it will work as exspected.
Okay, that was easy, thanks!
2. This is a quite tricky, since it requires that you add a custom behavior to your datagrid and datagrids
of type TABLE do not have one per default!

This is however well documented in the DataGrid PDF, get it here (top left):
http://lessons.runrev.com/m/datagrid

I am reading that PDF currently almost daily for the last couple of weeks since:
a. this is a REALLY complex beast and
b. I also need something like this in my current project :D

Drop a line if you get stuck, I managed this finally a couple of days before and hope
I did not forget how I finally managed it 8)
That's the PDF I've been trying to get my brain around for the past couple of days :shock:

You mean, even doing something simple like coloring text or setting the background color of a particular cell is complex?

Is there some other way of doing this, without a datagrid? Basic table maybe?

Many thanks...

--paul

Re: More datagrid help needed!

Posted: Fri Nov 16, 2012 12:52 pm
by Klaus
Hi Paul,
paulsr wrote:...
You mean, even doing something simple like coloring text or setting the background color of a particular cell is complex?
yep, doable, but surely advanced stuff!
And definitviely nothing I could explain in a few words here in the forum.
paulsr wrote:Is there some other way of doing this, without a datagrid? Basic table maybe?
Don't think so, a "Basic Table" field is just a single field with TAB delimited text in it.
I do not think you can modify this the way you need to.
On the other hand I never bothered to use a table field due to its natural limitations.


Best

Klaus

Re: More datagrid help needed!

Posted: Sat Nov 17, 2012 2:16 am
by paulsr
Klaus wrote:yep, doable, but surely advanced stuff!
And definitviely nothing I could explain in a few words here in the forum.
Okay. Thanks Klaus. I shall make a large pot of coffee and try to figure it out!

--paul

Re: More datagrid help needed!

Posted: Sat Nov 17, 2012 9:19 am
by paulsr
Okay, this is close to what I'm looking for...

http://lessons.runrev.com/s/lessons/m/d ... in-a-table

Towards the end, it has code like:

Code: Select all

set the foregroundcolor of me to the dgProp["hilited text color"] of the dgControl of me
and

Code: Select all

set the textcolor of me to red
...which at least gives me the confidence that this can be done.

But I can't figure out what "of me" does. What's in "me"? How do I make "me" become (for instance) "Line 3"?

Megaconfused!

--paul

Re: More datagrid help needed!

Posted: Sat Nov 17, 2012 1:02 pm
by Klaus
Hi Paul,

"of me" in that (quite confusing) example is the current column that receives the "fillInData" message.
I will take a deeper look at this example and see if this can be modified to your needs.


Best

Klaus

Re: More datagrid help needed!

Posted: Sat Nov 17, 2012 1:16 pm
by Klaus
Hi Paul,

OK, took a deeper look at this strange example, really confuisng and not very helpful.

anyway, to create the example you mentioned above:
...
if in column 2 of my datagrid, I have data which says "START" or "END" how do I color the STARTs in green and the ENDs in red?
---

We need to know the NAME of your column 2, lets presume if it named "tColumn2".
Then you only need to midify the "fillInData" hanlder like this

Code: Select all

## This will be sent to EVERY column in every row of your datagrid, so we only need to take action
## if the NAME of your column is "tColumn2"

## tData = the text that goes into each column!
on FillInData tData
  
  ## This is the "object" in question
  if the dgColumn of me <> "tColumns2" then
    ## No action neccessary!
    set the text of me to tData
    
    ## Done and goodbye:
    exit FillInData
  end if
  
  ## Correct column, now set forecolor accordingly:
  switch tData
    case "Start"
      set the forecolor of me to 0,255,0 ## Green
      break
    case "END"
      set the forecolor of me to 255,0,0 ## Red
      break
      ## No changes otherwise:
  end switch

   ## Forecolor set (or not), now put text in it:
  set the text of me to tData
end FillInData
Hope this helps 8)


Best

Klaus

Re: More datagrid help needed!

Posted: Sun Nov 18, 2012 11:25 am
by paulsr
Klaus wrote:Hope this helps 8)
Many thanks Klaus ... I think it will help enormously, once I understand a couple more things...

You said to modify the FillInData handler, by which I assume you mean the one in the datagrid's behavior script?

If so, there's a comment which says "This behavior only applies to 'forms' and not 'tables'." but I'm working with a table. Is this a problem, or am I trying to put your code in the wrong place?

And, if I have the line:

Code: Select all

set the dgData of grp "FldGrid" to tGrid
then my tGrid replaces your tData... is that correct?

Almost there :)

--paul

Re: More datagrid help needed!

Posted: Sun Nov 18, 2012 1:33 pm
by Klaus
Hi Paul,
paulsr wrote:You said to modify the FillInData handler, by which I assume you mean the one in the datagrid's behavior script?
Yes.
paulsr wrote:If so, there's a comment which says "This behavior only applies to 'forms' and not 'tables'." but I'm working with a table. Is this a problem, or am I trying to put your code in the wrong place?
Only datagrids of type "FORM" have a pre-made behavior script, TABLEs do not!
That's why the "Row behavior" button is greyed out in the inspector for these datagrids.

Check the example you mentoined on how to create and apply a "behavor" to your TABLE grid!
In THAT script you can modify the "on fillInData" handler.
paulsr wrote:And, if I have the line:

Code: Select all

set the dgData of grp "FldGrid" to tGrid
then my tGrid replaces your tData... is that correct?
Setting the "DGData" or "DGText" will alway replace all data in a datagrid.

But "tData" in my script above is of course also affected but not in the way you ight think!

When you "set the dgtext/dgdata...***" then the DataGfid Library will send the "fillInData" to
EACH field in EVERY ROW in your data, and "tData" is the content that will be filled into these fields.

*** It does not matter if you supply TAB delimited text (dgtext) or an ARRAY (dgData), internally the
DG library will will convert TAB delimited text (dgtext) to an array and use this to fill the grid with data.

To be able to distinguish WHICH fields is currently being filled, you will need to check
"the DGControl of me" as seen in my scriupt. THAT will hold the NAME of the current field
that is being filled.


Best

Klaus

Re: More datagrid help needed!

Posted: Mon Nov 19, 2012 10:37 am
by paulsr
Klaus wrote:Only datagrids of type "FORM" have a pre-made behavior script, TABLEs do not!
That's why the "Row behavior" button is greyed out in the inspector for these datagrids.
Hi Klaus,

With your help I'm starting to understand these mysterious thing called datagrids! But, just as I thought I'd understood your last message, I tried out a few things.

First; I switched the datagrid from a table to a form. Instantly my columns became unavailable. And when I tried your code (modified to suit my actual grid) I encountered problems with lines such as:

Code: Select all

if the dgColumn of me <> "tColumns2" then
because the column no longer exists.

One thing I hadn't realized is that if I leave the DG as a table, I can modify column behavior. I hadn't noticed that the grayed-out button could be activated by clicking the "+". That's not exactly intuitive! But maybe customizing some code in there is what I need?

BTW: If you switch a DG from table to form, it doesn't seem to be possible to switch back. I'm sure there's a really good, but wholly obscure, reason for this.

So, I'd appreciate yr thoughts. I feel I'm making progress, but oh so slowly.

Thanks...

--paul

Re: More datagrid help needed!

Posted: Mon Nov 19, 2012 10:45 am
by paulsr
More odd things...

For some of my columns, if I click "+" and then "Column Behavior" nothing seems to happen ... except that the next time I run the datagrid, the columns are empty and all the headings have disappeared! Whereas other columns, when I do the above, the editor displays a default behavior script. The latter is kind-of what I expected.

I can't see any difference between any of my columns. They are named "col01" thru "col07" and all have the same settings.

I wondering if I should scrap the DG and rebuild it?

--paul

Re: More datagrid help needed!

Posted: Mon Nov 19, 2012 1:55 pm
by Klaus
Hi Paul,
paulsr wrote:
Klaus wrote:Only datagrids of type "FORM" have a pre-made behavior script, TABLEs do not!
That's why the "Row behavior" button is greyed out in the inspector for these datagrids.
well, I only meant that you should add a (not yet present) behavior to your TABLE datagrid as described in the example
you mentioned above, and modify that one as I described, That's what we were talking about, right?!
paulsr wrote:...One thing I hadn't realized is that if I leave the DG as a table, I can modify column behavior. I hadn't noticed that the grayed-out button could be activated by clicking the "+". That's not exactly intuitive! But maybe customizing some code in there is what I need?
Sorry, no idea I never used this feature before.

I think scrapping your DG and start anew is a good idea 8)


Best

Klaus

Re: More datagrid help needed!

Posted: Wed Nov 21, 2012 9:14 am
by paulsr
Hi Klaus,

(and anyone else crazy enough to try customizing a datagrid!)

I've made progress, and uncovered some oddities, so this may be of interest to someone at some point. Here's what I've done and what I've learned...

First; I scrapped my DG, and created a new one, using a different name "GrpGrid" and accepting the default column names. In my case "Col 1" thru "Col 7".

Without doing any kind of customization, my data loads and displays fine. There are about 200 records for the 20 or so display rows, so I have a vertical scroll bar.

Next, I selected "Col 1" in the property inspector and clicked the '+' to add custom behavior. The "Column Behavior" button became un-grayed, so I clicked that too. Nothing happened.

So I did the same thing with "Col 2" and sure enough a "Col 2 Behavior" script popped-up in the editor window. The same happened in all the other columns, but I was still unable to get a script I could modify for Col 1.

Thinking I may at some point want to customize all the columns, I then created a dummy non-visible "Col 8".

It seems that whatever is the first field you try to modify, you can't!

So I clicked first on "Col 8" and then on the other columns. So now I have customizable "Column Behavior" scripts for all my visible columns, i.e. "Col 1" thru "Col 7".

Next, I started to modify the FillInData handler for Col 2. In my case that column is either blank, or contains the text "START" or "END". I want these to be green and red text respectively. I decided to start with just the green.

The default code is as follows:

Code: Select all

on FillInData pData    
    set the text of field 1 of me to pData
end FillInData
So I changed it to:

Code: Select all

on FillInData pData      
   set the text of field 1 of me to pData
   
   if pData = "START" then
      set the foregroundcolor of field 1 of me to 0,255,0         
   end if
     
end FillInData
I thought that was working, but I noticed when I scrolled up & down all the text became green. From this I figured I must be coloring the visible cells, rather than the actual text. So I added the "END" coding:

Code: Select all

on FillInData pData    
   set the text of field 1 of me to pData
   if pData = "START" then
      set the foregroundcolor of field 1 of me to 0,255,0  
   else
      if pData = "END" then         
         set the foregroundcolor of field 1 of me to 255,0,0  
      end if      
   end if
end FillInData
Bingo! Red & Green text, as needed.

It seems that if you choose to color any text, you must color all the text. In my case I didn't need to color the blank areas :D altho I did think about it :oops:

I have to say, this is actually a lot easier than any of the examples and tutorials I've found.

(At this point I was going to report a major problem. Having added the above code, I couldn't scroll the fields. I could move the scroll bar, but the data didn't change. Removing my custom code didn't help. But as I was typing this message LC decided it had encountered an unexpected error and crashed. When I restarted it, the scolling now works correctly. Hopefully this was a not-to-be-repeated anomoly.)

One minor problem was that any column for which I said I wanted customized behavior, the text was now left justified. In the property inspector, I selected "Center" for all columns. Deciding to have custom behavior overrides this.

One of the cards in the DG Template has a controls called "_ColumnData_" for each column. Displaying this in the Property Inspector allows you to set all the same column settings you can set for the original DG columns. (Does that make sense?)

Anyways, I am pretty-much at the point I want to be ... albeit a week later than I would have liked.

Many thanks Klaus for all your input. I wouldn't have reached this point without you.

One last thought... Does anyone know how to customize the background of the cells? I haven't yet found a way to do this.

--paul

Re: More datagrid help needed!

Posted: Wed Nov 21, 2012 11:21 am
by Klaus
Hi Paul,

glad you finally made it work!

I actually thought that you hve been following the instructions in the example that you mentioned above*
so I was referring to this fact and my proposals might have taken you to success a bit earlier 8)

* Especially the line at the top of the example:
...
Before you being this lesson you will need to read the lesson How Do I Override the Default Behavior for Rendering Data to a Cell?
http://lessons.runrev.com/s/lessons/m/d ... solve=true
...
Where it is explained how to add a custom behavior to your table grid!

Anyway:
One last thought... Does anyone know how to customize the background of the cells? I haven't yet found a way to do this.
Just set the backgroundcolor of the fields and their "opaque to true" before, if neccessary.


Best

Klaus