String search in a Datagrid

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

String search in a Datagrid

Post by quailcreek » Wed Jul 31, 2013 9:50 pm

How do I do a search on a datagrid from a keyed in string while having the DG only show the lines that have data that match the string? I see this type of functionality all over the place on my iPhone but I don’t know how to write the script. I have a DG with data in it. I have a field called search. The script I have used to do this in a list field doesn’t work. Any help would be much appreciated.

Thanks
Tom
MacBook Pro OS Mojave 10.14

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: String search in a Datagrid

Post by dunbarx » Wed Jul 31, 2013 11:56 pm

Well, you could extract the data from the DG with the "dgText", run a search on the lines that match your search string in whatever way you want to do that, and then reload the DG with just that info. Probably prudent to store the original data in a custom property of the group so it can be restored later on.

Can you write a routine that will find all instances of your search string among what might be many lines with a match? Lots of ways to do this. Write back if you need more help.

Craig Newman

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: String search in a Datagrid

Post by quailcreek » Thu Aug 01, 2013 4:01 am

Hi Craig,
I'm new to DG so I'm not sure how to write this. Sorry.
Tom
MacBook Pro OS Mojave 10.14

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: String search in a Datagrid

Post by dunbarx » Thu Aug 01, 2013 5:11 am

Not your fault.

Any tab and return delimited data can be loaded directly in the clear via the "dgText", or if in an array via the "dgData" . These are both properties of the DG itself. You have to read the DataGrid manual, which takes some work, but is actually well written, and you only need to take from it what you need at this time. All it takes is time and effort.

But where are you asking for help? With loading or reading the data? With filtering based on the search string? Again, write back, but with more specific questions.

Craig Newman

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: String search in a Datagrid

Post by quailcreek » Mon Aug 05, 2013 3:00 am

Hi Craig,
I think I can figure out how to get the data into the DG by building arrays. I could use help dong the search and then filtering based upon the search string. I do this type of search in a number of places in my desktop apps but I would like to switch over to using datagrid instead of fields. Any help pointing me in the right direction would be very much appreciated.

Thanks,
Tom

dunbarx wrote:Not your fault.

Any tab and return delimited data can be loaded directly in the clear via the "dgText", or if in an array via the "dgData" . These are both properties of the DG itself. You have to read the DataGrid manual, which takes some work, but is actually well written, and you only need to take from it what you need at this time. All it takes is time and effort.

But where are you asking for help? With loading or reading the data? With filtering based on the search string? Again, write back, but with more specific questions.

Craig Newman
Tom
MacBook Pro OS Mojave 10.14

Klaus
Posts: 14212
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String search in a Datagrid

Post by Klaus » Mon Aug 05, 2013 2:33 pm

Hi Tom,

how do you search in a "normal" listfield?

Do something like this:
1. When you "fill" your datagrid the first time (openstack or opencard) with data you should store the complete data -> the dgtext of grp xyz in a variable or custom property!
I use this in my example: -> the cCompleteDGData of THIS stack
Since we will filter (-> reduce) the data for the search, we will always use the complete data as a base for our search!

2. In the search field script (on keyup?) put the content of the search field into a variable and filter the dgtext of the datagrid:

Code: Select all

...
## Search field script
put me into tSearchString
put the cCompleteDGData of THIS stack into tData
## Now we FILTER this data and add wildcards to the serach, so we will also find something
## that is somewhere in the middle of the datagrid, know what I mean?:
filter tData with ("*" & tSearchString & "*")

## Nothiong found_
if tData = emtpy then
  beep
  exit to top
end if

## We found something, so lets display the results:
set the dgdata of grp "Your datagrid here" to tData
...
And don't forget a "reset" button that will load the complete original data back into your datagrid if neccessary:
...
set the dgtext of grp "Your datagrid here" to the cCompleteDGData of THIS stack
...

Hint: DGTEXT will only work with datagrid of type TABLE!

Hope this helps!



Best

Klaus

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: String search in a Datagrid

Post by dave.kilroy » Wed Aug 07, 2013 9:25 am

Nice code Klaus!

I've never messed with datagrids in any meaningful way but am tempted and the search capability would make it much nicer ...

However does tData as in:

Code: Select all

filter tData with ("*" & tSearchString & "*")
contain a complete dg row? and how about headers?

Dave
"...this is not the code you are looking for..."

Klaus
Posts: 14212
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String search in a Datagrid

Post by Klaus » Wed Aug 07, 2013 11:21 am

Hi Dave,

in my example tData is the COMPLETE data of the datagrid: One line = One row

Headers?
I do not see headers as part of the actual data, and they are NOT part of the DGTEXT of a datagrid,
at least not when you retrieve the dgtext.


Best

Klaus

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: String search in a Datagrid

Post by dave.kilroy » Wed Aug 07, 2013 11:24 am

Smashing - thanks Klaus
"...this is not the code you are looking for..."

Post Reply