How to hilite only a single data grid cell?

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: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
stam
Posts: 557
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

How to hilite only a single data grid cell?

Post by stam » Tue Jun 15, 2021 11:25 am

Hi all,

I want to hilite only 1 cell at at time in data grid table.
This means that when i click a different cell, the previously hilited one should stop being hilited and hilite the new cell.

It's easy enough to hilite a cell:

Code: Select all

on mouseUp
   set the opaque of the target to "true"
   set the backColor of the target to "green"
end mouseUp
but I can't see a way to un-hilite a previously hilited cell when clicking another one... and there seems to be no native method i can find.

Examining the structure of the table cell (examining the long name of 'the target'), this seems to be:

Code: Select all

group "<columnName> <rowNumber>" of 
  group "<columnName>" of 
    group "dgList" of 
      group "dgListMask" of 
        group "<data grid name>" of 
          card "<card name>" of 
            stack "<stack name>"
however the following doesn't work:

Code: Select all

repeat with x = 1 to the number of groups of group "<columnName>" of group "dgList" of  group "dgListMask" of group "<data grid name>"
   set the opaque of group x of group "<columnName>" of group "dgList" of  group "dgListMask" of group "<data grid name>" to false
end repeat
grateful for any suggestions...
Many thanks
Stam

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7766
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to hilite only a single data grid cell?

Post by dunbarx » Tue Jun 15, 2021 2:03 pm

Hi.

I don't know if something in the dataGrid API does this on its own, but know that all the editable fields in a DG are named something like "field Col 2 0005". This means you can:

Code: Select all

on mouseUp
   repeat with y = 1 to the number of flds
      if the name of fld y contains "col" then
         set the opaque of  fld y to "false"
         set the backColor of fld y to ""
      end if
   end repeat
   set the opaque of the target to "true"
   set the backColor of the target to "green"
end mouseUp
Craig

stam
Posts: 557
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to hilite only a single data grid cell?

Post by stam » Wed Jun 16, 2021 1:10 am

dunbarx wrote:
Tue Jun 15, 2021 2:03 pm
I don't know if something in the dataGrid API does this on its own, but know that all the editable fields in a DG are named something like "field Col 2 0005"
Hi Craig, sadly this does not work for me.

It works for simple cases where there is one field per cell, because by default that field is resized to the rect of of the cell - so the target is the field, not the cell.

I have a more complex table, where each cell has 3 fields.
This means the target could be any of these - and they don't cover the entire cell.
More worrying, because there are gaps between these fields, some of the cell is not covered by fields and using this script there it causes bits of the data grid to actually vanish, with no easy way to reverse it (i ended up rebuilding a new data grid in the end).

I couldn't see a function in the data grid API for doing this - the only other option i can think of is to have a semi-translucent rect on top of all fields set to the rect of the cell (eg with a blendLevel of 85) and with a color overlay - and then set the opaque of it when highlighting as that will then be the target.

But that does seem rather kludgy and was hoping for a more elegant solution... grateful for any suggestions!

stam
Posts: 557
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to hilite only a single data grid cell?

Post by stam » Wed Jun 16, 2021 12:32 pm

Further to this, the best solution i have found so far is a modification of Craig's method above, by adding an additional label field to each column's template group covering all other controls, giving it a preferred background colour and increasing the blendLevel to make it semi transparent. It's set to have no border, so toggling the opaque property in effect shows or hides this.

I named the extra label field 'highlighter' in each column's group to cover all other controls, and to ensure it fills the entire cell, i added a line to the on LayoutControl pControlRect handler:

Code: Select all

 set the rect of field "highlighter" of me to pControlRect

With the code below, clicking on a highlighted cell, it toggles the highlight and when clicking on another cell it removes the highlights from all other cells and highlights the newly clicked one thanks to Craig's code above - just added some extra safety to clarify that these actions will only affect the field "highlighter" and no other component of the data grid.

Code: Select all

on mouseUp
   lock screen  -- essential in LC 9.6.2 RC3 or greater, or massive speed penalty on MacOS Big Sur
   if the name of the target contains "highlighter" and the opaque of the target is true then 
      set the opaque of the target to false
      exit mouseUp
   else
      repeat with x = 1 to the number of fields
         if the name of field x contains "highlighter" then
            set the opaque of  field x to false
         end if
      end repeat
      if the name of the target contains "highlighter" then set the opaque of the target to true
   end if
end mouseUp
I appreciate this is a kludge, so would welcome a more elegant solution...
Stam

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7766
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to hilite only a single data grid cell?

Post by dunbarx » Wed Jun 16, 2021 2:18 pm

Stam.

Interesting and odd that you have three "fields" in a single "cell". Why, and how? What does that look like?

Craig

stam
Posts: 557
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to hilite only a single data grid cell?

Post by stam » Wed Jun 16, 2021 5:44 pm

dunbarx wrote:
Wed Jun 16, 2021 2:18 pm
Stam.

Interesting and odd that you have three "fields" in a single "cell". Why, and how? What does that look like?

Craig
Simple - there is an study identifier and a study status that take up most of the cell, and a UUID in much smaller font size at the bottom of the cell.

Easier to have them as separate fields because the formatting is very different (size/style), the separate fields allow me to position them accurately and dynamically with the data grid’s LayoutControl handler for that column and it’s simpler to pop up a menu for changing the status.

Yes I know it’s possible to use a single field but semantically there’s are distinct data and managing as one field would complicates the code and make it more difficult to maintain in the future.

Then the 3rd field is a UUID which I’m including for now but may hide down the line (points to the the exact database record, just a convenience for me right now).

Although each cell is populated with a string, I parse the string to populate each field with the fillInData handler. Quite simple ;)

From the user’s perspective there aren’t obvious fields to interact with, just 3 lines of text with very different formatting, location in the cell and different actions - but only one “cell”. The fields are there for my convenience only…

PS I should also mention the rowHeight is set to 60 ;)
In essence it’s like a Form, but tabular…

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7766
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to hilite only a single data grid cell?

Post by dunbarx » Wed Jun 16, 2021 6:10 pm

Stam.

OK.

But I bet you will have to live with a kludge if you want to color the "cell", the number and types of fields inside it notwithstanding.

One nice thing about kludges is that they are open to your own, er, kludges, that is, you can fool around to the extent that they become more versatile than anything built-in.

Craig

stam
Posts: 557
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to hilite only a single data grid cell?

Post by stam » Wed Jun 16, 2021 6:39 pm

dunbarx wrote:
Wed Jun 16, 2021 6:10 pm
But I bet you will have to live with a kludge if you want to color the "cell", the number and types of fields inside it notwithstanding.
Yeh i guessed - someone would have mentioned something (or i would have found something online) by now if it were easier.
I don't mind the kludge much - it's performant enough.

Just to give a more concrete example of what i was aiming for - this is the work in progress for the admin overview part of the app to show multiple studies with their analyses compactly and navigate to them... the purple is the hilited cell
funky table with hilite.jpg

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7766
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to hilite only a single data grid cell?

Post by dunbarx » Thu Jun 17, 2021 2:26 pm

Stam.

Looks great.

I am not sure, but as we both surmise, there is no object that "is" the "cell", only the field that lives there. So an extra object is required in order to have something at all to have a backColor that can be set.




Craig

stam
Posts: 557
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to hilite only a single data grid cell?

Post by stam » Thu Jun 17, 2021 3:12 pm

dunbarx wrote:
Thu Jun 17, 2021 2:26 pm
I am not sure, but as we both surmise, there is no object that "is" the "cell", only the field that lives there. So an extra object is required in order to have something at all to have a backColor that can be set.
Sure - i was just hoping there was some invisible thingamabob like the 'background' graphic that would take on that role, and that there may be a more elegant way, but kludging it all the way.

My problem now is how to delete a cell and have remains cells of that column move upwards. I guess i'll have to edit the entire dgText/dgData. I swear it would have been simpler to just use 3 data grids!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7766
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to hilite only a single data grid cell?

Post by dunbarx » Thu Jun 17, 2021 4:40 pm

guess i'll have to edit the entire dgText/dgData.
I use DG's only here and there, but that is how I always do it. I like getting the entire dataset "back" into LC, so I can see it and work it. It is nothing for me to have to run a get and set in order to be able to do that.

Craig

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”