Set the tooltip of a datagrid

Bob Sneidar bobsneidar at iotecdigital.com
Wed Jul 12 12:36:38 EDT 2017


Great tip! (pun intended)

So here is what I came up with. Keep in mind that tKeyList is a list of columns I want in the tooltip. The idea is to present the user with a tooltip containing summary information on the datagrid data record. I had to prepend a counter to each key so that it would sort based on the order of the items in tKeyList. Arrays. SHEESH! Sure would be nice if the keys were in the order they were added! 

I will probably move the relevant code into another handler in my Utilities backscript, then pass the long id of the target, and the key list to that handler. I will also probably update this handler so that in the event no keylist is passed, I will get the datagrid column labels, and use those for a key list. 

on mouseEnter
   local tKeyList 
   wait 1 second with messages
   put the target into tTargetName
   put tTargetName into tTarget -- we need to preserver the original name
   
   if the first word of tTarget is not "field" then
      pass mouseEnter
   end if
   
   put "sitename,addr1,city,state,zip,contactname,contactphone,contactemail," into tKeyList
   put "itname,itphone,itemail" after keyList
   replace quote with empty in tTargetName
   put the last word of tTargetName into tLine
   put the dgDataOfLine [tLine] of me into aLineData
   
   repeat for each item tKey in tKeyList
      add 1 to tCounter
      put aLineData [tKey] into aRecordData [tCounter & ". " & tKey]
   end repeat
   
   put arrayToText(aRecordData, "record") into tText
   sort lines of tText
   set the tooltip of tTarget to tText
   show the tooltip of tTarget
   breakpoint
   pass mouseEnter
end mouseEnter

function arrayToText aArrayData, pFormat, pStripRecordKeys
    if aArrayData is not an array then
        put "ERROR: Data passed is not an array!" into theValue
        return theValue
    end if
    
    if pStripRecordKeys is not in "true|false" then put false into pStripRecordKeys
    -- sort tArrayKeys ascending
    
    -- may be an array of values
    put the keys of aArrayData into tArrayKeys
    sort lines of tArrayKeys numeric ascending
    put line 1 of tArrayKeys is 0 into tHasHeader
    
    -- convert single array to numeric array of arrays
    if line 1 of tArrayKeys is not a number then
        put aArrayData into aTempArray [1]
        put aTempArray into aArrayData
        put the keys of aArrayData into tArrayKeys
    end if
    
    switch pFormat
        case "record"
            repeat for each line tArrayKey in tArrayKeys
                put aArrayData [tArrayKey] into aArrayRecord
                put the keys of aArrayRecord into tColumnKeys
                
                repeat for each line tColumnKey in tColumnKeys
                    put tColumnKey & ": " & aArrayRecord [tColumnKey] & cr after tValue
                end repeat
            end repeat
            break
        case "table"
            -- table header
            if not tHasHeader then put cr into tValue
            
            repeat for each line tArrayKey in tArrayKeys
                add 1 to tCount
                put aArrayData [tArrayKey] into aArrayRecord
                put the keys of aArrayRecord into tColumnKeys
                sort lines of tColumnKeys numeric ascending
                
                repeat for each line tColumnKey in tColumnKeys
                    if pStripRecordKeys and (char 1 of tColumnKey is "@") then next repeat
                    
                    if tCount = 1 and not tHasHeader then put tColumnKey & tab after line 1 of tValue
                    put aArrayRecord [tColumnKey] & tab after tValue
                end repeat
                
                put cr into last char of tValue
            end repeat
            break
    end switch
    
    return tValue
end arrayToText

Bob S


> On Jul 11, 2017, at 19:09 , Mike Bonner via use-livecode <use-livecode at lists.runrev.com> wrote:
> 
> There should be a couple ways.. Assuming the tooltip data and the displayed
> data are already associated, you can do as suggested and pass in the popop
> information as part of the dgdata.. Then in the fillindata you 'set the
> tooltip of field 1 of me to ... the data that was passed in with the array.
>  To do this though i _Think_ you'd need a custom behavior for every
> column.
> 
> To get around this, you could use a mouseenter in the script of the
> datagrid itself that does something like..
> 
> on mouseenter
>    get the text of the target
>    -- then use the text of the target to look up your tooltip data and..
>   set the tooltip of the target to "whateveryouwant"
> 
> end mouseenter
> 
> i tested by setting the tooltip to a random number and it seems to work
> fine.
> 
> If you're using a form rather than a grid, its much easier, just use the
> first method in the behavior script to set whatever tooltips you like as
> part of the fillindata.   A single script edit to handle all tooltips.





More information about the use-livecode mailing list