Quantcast
Channel: LiveCode Forums
Viewing all articles
Browse latest Browse all 783

Getting Started with LiveCode - Complete Beginners • Re: How to create a search auto-complete in livecode?

$
0
0
Hi lemodizon,

This is what I do - it may or may not suit you, but it's quick and easy.

First of all: I store the full data of the data grid into a custom property of the datagrid itself, as soon as I populate the datagrid (i.e. outside of any search/filter actions):

CODE:

set the allData of me to the dgData of me
When searchging, I filter the datagrid rather than find a line - it's just quicker. This is the type of handler I will put in the stack script, so I can use with any datagrid:

CODE:

command filterGrid pText, pKey, pDataGridLongID    local tArray    if pText is empty then         // no text in search field -> show all data, ie clear field to show all data        set the dgData of pDataGridLongID to the allData of pDataGridLongID    end if        put the dgData of pDataGridLongID into tArray    filter elements of tArray where each [pKey] contains pText     set the dgData of pDataGridLongID to tArrayend filterGrid
then in the filter field:

CODE:

on textChanged    filterGrid the text of me, "name", the long id of "<datagridToFilter>"end textChanged

The result is that as the user types, the data in the grid is restricted to the values that match the query. If you clear the search field, all data is shown.

As I said, that's what I do (please note I wrote this handler off the top of my head and may contain errors as not checked - but this is what I do frequently).
It is possible to search for the line and highlight it, but you then have to scroll the datagrid to show the highlighted line etc, just more work.
I find filtering suits my needs and is easier - maybe you'll find it helpful too...

Note: this can be used find with data grids just fine, even though the indexes may be random numbers not starting from 1. However as polygrid and polyList expect 1-based sequential numbering of the indexes, so if using with those, you'll need to re-number the indices.

Regards
Stam

Statistics: Posted by stam — Mon Jul 01, 2024 9:58 am



Viewing all articles
Browse latest Browse all 783

Trending Articles