On Dec 28, 2004, at 3:56 PM, Tony Cerrato wrote:
I am considering implementing "type-ahead" functionality in an
editfield (as is so helpful in RB's text editor). I have not found an
example yet and don't know if the result would even be worth the
effort (I expect it would have to be done in a very precise manner as
to not be "klutzy."
It is a lot of work, but it can be done.
There are basically two parts to it... the UI and the backend. The UI
I will leave up to you since that is usually more project specific.
For the backend, you can use the Dictionary Class to build your list or
you can create a custom class. Your own class would probably be faster
and would allow you to ignore case-sensitivity (if you want).
This example is one way to do it, but I am certain that there are many
more examples. This approach is time consuming when it searches toward
the beginning of the string since there are so many possible matches.
Basically, you have a list of all possible characters you are looking
for. In most cases, this would be A-Z and 0-9 and a word ending
identifier. You need the word ending to notify you that there is a
possible valid substring. In the example below I am using '&h0' for
the word ending, which is hexadecimal in REALbasic for Null.
I will explain using the Dictionary, and given the following list of
words to add to the autocomplete:
car
cat
catch
dog
As you are creating your autocomplete list, you just look to see if the
first character of the list item has already been added -- if not, then
add it. The first word in our list is "dog" so we will look for "d".
When you finish you should have a nested dictionary structure something
like this (please forgive the ASCII art):
[root] -+- ( c ) --- ( a ) -+- ( r ) --- (&h0)
| |
| +- ( t ) -+- (&h0)
| |
| +- ( c ) --- ( h ) --- (&h0)
|
+- ( d ) --- ( o ) --- ( g ) --- (&h0)
In the ASCII art above, the [root] is a Dictionary object and it has
two objects: (c) and (d). The Keys for these objects are "c" and "d"
(as String Variants). The Value for both objects are new dictionaries.
The (c) object contains a single object: (a) with a key "a". So we
have (c)(a) connecting the links together.
(c)(a) contains two items (r) and (t).
Looking at (r) we see that it contains the Null (0 As Integer Variant).
So now we have one complete word in our structure: (c)(a)(r)(&h0).
Since (r) only contains the single item, the chain ends here.
Looking at (c)(a)(t)_ we see that it contains two objects, a (&h0) and
a (c). So now we have the word (c)(a)(t)(&h0) and we also know that
there is another word that extends beyond with (c)(a)(t)(c).
That is the logic behind the structure. To implement autocomplete in
your code, you would return all possible strings which start with the
existing string. So if the user were to enter "ca", the matching
strings would be "car", "cat" and "catch" but "dog" would be excluded.
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>
|