how to detect a certain date in a dg table column

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

CAsba
Posts: 384
Joined: Fri Sep 30, 2022 12:11 pm

Re: how to detect a certain date in a dg table column

Post by CAsba » Wed Sep 13, 2023 1:04 pm

Sorry Stam, I wrote before I read your piece. I'll try it now..

Klaus
Posts: 13864
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: how to detect a certain date in a dg table column

Post by Klaus » Wed Sep 13, 2023 1:26 pm

stam wrote:
Wed Sep 13, 2023 1:01 pm
Klaus wrote:
Wed Sep 13, 2023 12:53 pm
What am I overlooking?

And yes, in my example the column is named "tdate"! 8-)
Sorry Klaus, 'reviewDate' was the column name given in the OP...
???
Yes, I know, but I used "tdate" in MY example.
Get the same result if I change my example to use "reviewDate"! 8-)

So what's wrong with your syntax?

Klaus
Posts: 13864
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: how to detect a certain date in a dg table column

Post by Klaus » Wed Sep 13, 2023 1:30 pm

Hi CAsba,

with my script add this to the last IF THEN...

Code: Select all

...
  ## NOW check if we had found something and take action if yes:
   if tFound = TRUE then
      ## tKey = the INDEX of the array with the found date
      set the dghilitedIndexes of grp "your dg here..." to tKey
    
      ## tCompleteRow is an array with the content of the found row
      ## do your thing
   end if
...
Best

Klaus

CAsba
Posts: 384
Joined: Fri Sep 30, 2022 12:11 pm

Re: how to detect a certain date in a dg table column

Post by CAsba » Wed Sep 13, 2023 1:57 pm

Thanks again, Klaus, tested it and works. Splendid !

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

Re: how to detect a certain date in a dg table column

Post by stam » Wed Sep 13, 2023 2:37 pm

Klaus wrote:
Wed Sep 13, 2023 1:26 pm
So what's wrong with your syntax?
You are experienced enough to point out if I have made an error in my syntax, I'm not seeing it although I did write this off the top of my head and there may be a typo or some silly error. However, vague insinuations don't help.

The code is uncomplicated and hard to get wrong. It's fewer lines than writing a loop and certainly less processor intensive.
As to highlighting the row, that's fine until there are enough rows that can't be all displayed - then you have to actively scroll to it, exactly like I mentioned above. If you have 200 rows you'd have take into account the found line, the line height and set the dgVScroll appropriately.

By and large I also try to avoid spoon-feeding the full code to people learning. For obvious reasons... but I see this is needed, especially since even you seem to find some kind of fault here.

Whenever I write a handler, I make it as generalisable as possible so it can be re-used for other cards/stacks/projects.
This is a small handler (excluding comments and variable declarations, it is 8 lines of code), it will work for any situation where you want to search any datagrid column for a piece of data and hilite the row; this should reside in the stack script so it's accessible to all cards:

Code: Select all

command hiliteRow pFilter, pColumn, pDataGrid 
    # pFilter = text to search for,  pColumn is the column to search in
    # pDataGrid = the _LONG ID_ of the datagrid to search
    local tArray, tKeys
    put the dgData of pDataGrid into tArray
    filter elements of tArray where each[pColumn] = pFilter
    if tArray is empty then 
        set the dgHilitedIndexes of pDataGrid to empty
        exit hiliteRow
    end if
    put replaceText(the keys of tArray, "\R", comma) into tKeys // "\R" is regex for CR, LF or CRLF
    set the dgHilitedIndexes of pDataGrid to tKeys
end hiliteRow
to call it you simply issue the command:

Code: Select all

hiliteRow <the text to search for>, <column name>, <the long id of the datagrid to search>
I.e. in Casbah's case:

Code: Select all

hiliteRow the long date, "reviewDate", the long id of group "datagrid 1"
This will hilite all found rows. It will also clear hilites from the datagrid if the text searched for is not found (not sure if that was considered in the code you posted). Note that this does not include scrolling to the first found index. But that question hasn't arisen (yet, anyway..).
I can guarantee you this works exactly as advertised. If you think there is a fault, check spelling....
Last edited by stam on Wed Sep 13, 2023 3:02 pm, edited 2 times in total.

Klaus
Posts: 13864
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: how to detect a certain date in a dg table column

Post by Klaus » Wed Sep 13, 2023 2:51 pm

Yes, sorry, my fault, must have been a typo in my script.
Works fine now!

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

Re: how to detect a certain date in a dg table column

Post by stam » Wed Sep 13, 2023 3:16 pm

More generally this code can be changed to search for anything that contains the text searched for in the specified column, eg searching for "October" in the long date. just change the line

Code: Select all

filter elements of tArray where each[pColumn] = pFilter
to

Code: Select all

filter elements of tArray where each[pColumn] contains pFilter
Any comparison operator can be used in place of "=" making it extremely flexible and easy to maintain...
'each' makes this possible in a single line and is one of those things that the majority of other languages lack and makes it seem like magic...

CAsba
Posts: 384
Joined: Fri Sep 30, 2022 12:11 pm

Re: how to detect a certain date in a dg table column

Post by CAsba » Mon Sep 18, 2023 2:22 pm

Hi guys,
Firstly, the DG only holds two dates, and these are entered as long date and abbrev date, which effectively differentiates them, so searching the dgtext is not going to get a spurious date.
An update..I was still getting problems trying to hilite the line (in order to extract further data from the line, name of customer, and lastinvoice).
Then I hit on this code that obviates the need to hilite the line..

Code: Select all

if the dgtext of grp "Datagrid 1" of cd "custlist" contains the long system date then
      put the dgdata of grp "datagrid 1" of cd "custlist" into tData
      put the keys of tData into tKeys
      put the long system date into tDate
      repeat for each line tKey in tKeys
         if tData[tKey]["ReviewDate"] = tDate then 
            put tData[tKey]["Name of customer"]  into fld "fbizname" 
            put tData[tKey]["Lastinvoice"]  into fld "flastinvoice"      
            exit repeat
         end if
      end repeat      
   end if
It works !
Thanks for all your help and interesting insights.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”