Data Grid form search functionality (filtering)

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

Post Reply
markosborne
Posts: 15
Joined: Sat Mar 20, 2010 6:03 pm

Data Grid form search functionality (filtering)

Post by markosborne » Thu Feb 23, 2023 2:30 pm

Hello All,

I have a Data Grid form displaying a very long list (single-word and multi-word text descriptions).

There is a Field on the card, which is used to enter search terms in order to filter the items in the Data Grid form down to a manageable length in oder to make finding items easier. I have implemented basic search functionality using this field script:

global gDataGridArray_A

on textChanged
local tFilteredArray, tFilter
put text of me into tFilter
if tFilter is not empty then
filter elements of gDataGridArray_A where each["TXT"] contains tFilter into tFilteredArray
set the dgData of group "DataGrid_A" to tFilteredArray
else
set the dgData of group "DataGrid_A" to gDataGridArray_A
end if
end textChanged

This works well and only displays items in the Data Grid containing the text entered in the field. For example, if “ard” is entered, only items containing “ard” will be displayed in the Data Grid: "a block of lard", "piece of chard", etc.

Question 1: Is this a good approach in terms of using ‘filter’ and setting the dgData in this way?

I now need to enhance the search functionality so that it will search for multiple part matches of strings. For example, if an item in the list is: “pastel colours and stripes”, I want to be able to type “pas our” to locate it. Or “ast olor ice”. Or “rs tel pes”

Question 2: Any thoughts on how to approach that?

Thanks in advance (for answers or suggestions either question)

Mark

markosborne
Posts: 15
Joined: Sat Mar 20, 2010 6:03 pm

Re: Data Grid form search functionality (filtering)

Post by markosborne » Thu Feb 23, 2023 3:16 pm

Correction:

Data Grid table NOT form

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

Re: Data Grid form search functionality (filtering)

Post by stam » Sun Feb 26, 2023 4:28 pm

Hi Mark,
I use this approach all the time, so I guess it's fine :)
it will work for both table and form data grids

Regarding your other question: if I understood correctly, you want to use multiple word fragments to search filter by a key?
I haven't tested but assume just replacing spaces with wildcards (asterisks) in the text contained in the filter field should allow this.

Code: Select all

put "*" & replaceText(the text of me, "\s", "*") & "*" into tFilter 
This replaces all spaces with wildcards, pre- and postfixes the search text with wildcards as well.
This should provide what you need - alternatively you can use regex but that depends on how handy you are with regex (most aren't!)

HTH
Stam

markosborne
Posts: 15
Joined: Sat Mar 20, 2010 6:03 pm

Re: Data Grid form search functionality (filtering)

Post by markosborne » Mon Feb 27, 2023 12:14 pm

Thanks Stam,

I tried your code but it just filters everything out on the first character I type (empties/clears the Data Grid).

I'll give the regex a go (but I'm no expert...).

Best

Mark

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

Re: Data Grid form search functionality (filtering)

Post by stam » Mon Feb 27, 2023 7:19 pm

Hi Mark,

sorry that didn't work - didn't really have the data to test it with, although a quick muck-about in the multiline message box with strings rather than arrays did work.

if you post an example with the data you want to filter I'm sure it can be done fairly easily...

BW
Stam

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7257
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Data Grid form search functionality (filtering)

Post by jacque » Mon Feb 27, 2023 10:19 pm

I usually just do repeated filtering for as many matches as I need. For example:

Code: Select all

put “ast olor ice” into tFilters
put gDataGridArray_A into tFilteredArray
repeat for each word w in tFilters
   filter elements of tFilteredArray where each["TXT"] contains w
end repeat
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

markosborne
Posts: 15
Joined: Sat Mar 20, 2010 6:03 pm

Re: Data Grid form search functionality (filtering)

Post by markosborne » Tue Feb 28, 2023 6:33 am

Thank you Jacque,

That works just fine.

Much appreciated.

Mark

markosborne
Posts: 15
Joined: Sat Mar 20, 2010 6:03 pm

Re: Data Grid form search functionality (filtering)

Post by markosborne » Tue Feb 28, 2023 6:41 am

And thank you for your offer of further help Stam,

You were right, it was fairly easy -- I'm still trying to get used to the syntax and framework...

Best wishes

Mark

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”