Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-01-2006, 03:51 AM   #1
BCSWebStudio
grasshopper
 
Join Date: May 2005
Location: college station, texas
Posts: 21
Default Filter list as you type interface like on an iPod

I'm working on a filter-as-you-type list like on an iPod: partial matches show below the text field, and as you type, your search gets more specific and less strings show in the list below. This is to be implemented in a name-lookup. My problem is both with algorithm and speed.

Current setup is this:
-One text field, one ListBox. This is the search setup.
-One hidden text field, one ListBox. This is the "last search string" and original, complete list of names. On init, this list populates the filtered search ListBox 1:1.

As the user proceeds to type a name in the search box, the list below gets filtered like this:
- first check that the previously typed text is contained in the current/new search string. If it is, we know to simply filter out the mismatches and trim the list. If it is not, then this is a new search (or backed-up a character), and the entire list is recreated based on the hidden, original list.
- To recreate the list: Clear the ListBox and copy 1:1 from the hidden original.
- To remove mismatches: Loop the entire ListBox and remove items that don't match with .removeItemAt().

This is incredibly slow on my 1.6GHz machine, and on my 700MHz production kiosk, it is impossible. In addition to that, something in my implementation is buggy, and it seems to miss every other name upon filtering...

Would someone please take a crack at it and advise me where I'm going wrong or even offer a better way? Oh, and I have commented out removeItemAt in order to verify that that's a slow-down: the thing is quick as lightening when not having to alter the ListBox items...

Code:
function onSearchChange(str) {
	// if search text contains previously searched text, remove items that do not contain search text,
	//	otherwise, rebuild list based on search
	var stxt:String = txtSearch.text.toLowerCase();
	var prevstxt:String = txtPrevSearch.text;
	var arrstxt:Array = stxt.split(" ");
	//trace("\"" + stxt + "\"" + "\t" + "\"" + prevstxt + "\"");
	if (stxt.indexOf(prevstxt) > -1) {
		// search text contains previous search-- remove filtered
		var curnam:String = "";
		// loop thru entire current list
		var numitems:Number = liNames.length;
		for (var i=0; i < numitems; i++) {
			curnam = liNames.getItemAt(i).label.toLowerCase();
			// loop thru all space-delimited individual search words; compare against current item in list
			if (curnam.indexOf(stxt) < 0) {
				//trace("removed " + curnam + " for " + stxt);
				liNames.removeItemAt(i);
				numitems--; i--;	// reset counters: number of items and current, b/c removeItemAt is immediate
			}
		}
//		trace("Removed item(s)");
	} else {
		// search text does not contain prev search (backspace, replace, etc)-- rebuild list based on current search
		liNames.removeAll();
		var curnam:String = "";
		var curitem;
		for (var i=0; i < liNamesSrc.length; i++) {
			curitem = liNamesSrc.getItemAt(i);
			curnam = curitem.label.toLowerCase();
			var blninc:Boolean = false;
			// loop thru all space-delimited individual search words; compare against current item in list
			// - only add item if all search terms are contained within current item (like an iterative AND)
			for (stxti in arrstxt) {
				if (curnam.indexOf(arrstxt[stxti]) > -1) {
					blninc = true;
					continue;
				} else {
					blninc = false;
					break
				}
			}
			if (blninc) {
				liNames.addItem(curitem);
			}
		}
		trace("rebuilt list.");

	}
	txtPrevSearch.text = stxt;
	txtCnt.text = liNames.length;
}
BCSWebStudio is offline   Reply With Quote
Old 10-01-2006, 04:13 AM   #2
random
Indy Flash Director
 
random's Avatar
 
Join Date: Feb 2006
Location: In your mind
Posts: 136
Default

i am pretty sure the reason it is so slow is because of indexOf.

instead try using something like:

ActionScript Code:
if(substring(curnam,0,length(stxt)) == stxt){ blninc = true; continue; } else { blninc = false; break } if (blninc) { liNames.addItem(curitem); }

Plus this method gives you true results, as apposed to before you might get elephant if you typed in ph. In the exapmple above it matches the begining of the string.
random is offline   Reply With Quote
Old 10-01-2006, 04:20 AM   #3
BCSWebStudio
grasshopper
 
Join Date: May 2005
Location: college station, texas
Posts: 21
Default

Thanks for the quick response-- Goodness!
I am actually wanting to allow partial matches: "atr" should keep "patrick". In order for sub(0..) to work, I would need to further split both needle and haystack strings on spaces and search them individually, I think. I am about ready to just do a basic "type-then-click-search" iterative solution...
BCSWebStudio is offline   Reply With Quote
Old 10-01-2006, 04:44 AM   #4
random
Indy Flash Director
 
random's Avatar
 
Join Date: Feb 2006
Location: In your mind
Posts: 136
Default

It would be alot more practical and efficient... But either way it is going to have some lag time.
random is offline   Reply With Quote
Old 10-01-2006, 06:54 AM   #5
BCSWebStudio
grasshopper
 
Join Date: May 2005
Location: college station, texas
Posts: 21
Default

Just an update on this ill quest: I am certain that the lag is the removeItemAt method-- again, commenting it out is like lightning. Also, I have tried converting the ListBox to use setDataProvider with an array of data and using Array.splice to pluck the values-- just as slow.

I guess this means only one thing: implement the iterative "type and click search" way. Grr..
BCSWebStudio is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Any tutorials for this type of interface? godzilla ActionScript 2.0 2 07-18-2006 10:43 PM
Trying to Filter List Box using XML Path Expression? westleyct ActionScript 2.0 0 03-13-2006 10:29 AM
ipod interface developing ActionScript 1.0 (and below) 1 07-30-2004 02:30 PM
Creating a list of words in Flash interface rabblerouser ActionScript 1.0 (and below) 1 08-12-2003 05:55 PM
Macromedia Central CyanBlue ActionScript 1.0 (and below) 13 08-01-2003 09:05 PM


All times are GMT. The time now is 06:25 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.