Mar
13

Filtering a list as you type in a ColFusion Flash Form

43 comments Posted by: Nahuel Foronda

This is an example to show an input text that filters a list when the user types some characters. The image below shows the filtered list with only the names that match what is in the input text.

Code:

<cfsavecontent variable="changeForInput">
   if(_global.arrMembers == undefined) _global.arrMembers = data.dataProvider.slice(0);
   var arrMembers = _global.arrMembers;
   var arrDisplay:Array = [];
   var fortext = forInput.text.toLowerCase();

   for(var i = 0; i < arrMembers.length; i++)
   {
      if(arrMembers[i].label.substr(0,fortext.length).toLowerCase() == fortext)
      {
      arrDisplay.push(arrMembers[i]);
      }
   }
   data.dataProvider = arrDisplay;
</cfsavecontent>

<cfform name="myForm" format="flash" width="400" height="300">
   <cfformgroup type="panel" label="Search our Members">
      <cfinput type="text" name="forInput" onchange="#changeForInput#" label="Search for:">
      <cfselect name="data" query="memberList" size = "10" value="id" display="name"></cfselect>
   </cfformgroup>
</cfform>

View the example
Download the source

Category: CFForm | ColdFusion |

43 Comments so far

Write yours
Chuck
1. Chuck wrote on March 25, 2005 at 7:10 AM
I hope you can help. I’ve been trying to achieve the same functionality from the drill down search on a cfgrid created from a cfquery. But, for the life of me I can’t find any reference on this.
Susan
3. Susan wrote on March 31, 2005 at 9:32 AM
Laura,
Thanks so much for this code. I was able to adapt the filter to my application but don't know how to reset/unset/clear the array variable. When the page is reloaded it has the last value selected so I don't get the full list.
Thanks
Susan
Randy
4. Randy wrote on April 07, 2005 at 11:38 AM
Nahuel,

Thanks! I was trying to do the same thing but this code is far more elegant. Oddly, though I have not been able to get this code to work on a non-Flash CF form, specifically: format=&quot;xml&quot;? I want to do this (and your grid example too!) using format=&quot;xml&quot;, but I'm just not sure how to go about it. Although I'm quite new to CF, Flash &amp; Actionscript it doesn't seem as if there is anything in the actionscript that would prevent it from working in a format=&quot;xml&quot; situation... What am I missing?

Thanks again!

Randy
Randy
5. Randy wrote on April 07, 2005 at 6:11 PM
Nahuel,

Ahh... now that I've checked the CF documentation... allow me to rephrase that question... ;~)

The javascript that I put together to accomplish almost the same thing inside a flash form is not nearly as clean or elegant... if you were to try to do this in Javascript, how would you do it?

Thanks!

Randy
Justin Kozuch
It would be great to have something like this that would filter all the data from a column, as opposed to filtering data that starts with the criteria that you type in

For instance, if I wanted to search names that contain the letter &quot;D&quot;, and not necessarily start with the letter &quot;D&quot;, that would be awesome.
Laura
Justin,
That has been answered in the comments of http://www.asfusion.com/blog/entry/filtering-a-cfgrid-as-you-type--revisited-

change this line:
if(item[selected].substr(0,fortext.length).toLowerCase() == fortext)
to:
if(arrMembers[i][selected].toLowerCase().indexOf(fortext) != -1)
mafdoc
8. mafdoc wrote on August 26, 2005 at 1:18 PM
This is great! ! ! Thanks so much...
Can you tell me if I can CFOUTPUT from the results of this filter?
Any help would be greatly appreciated.
Thanks again.
mafdoc
9. mafdoc wrote on September 13, 2005 at 12:24 PM
I've got this working on a list on members but I am now trying to get this working on a list of id numbers. The grids shows all the data but when I start to type to filter the grid goes blank.
(BTW. I found the answer to the cfoutput question above using bind)
Thanks for any help.
mafdoc
10. mafdoc wrote on September 14, 2005 at 10:41 AM
I would like to filter data from a column that comes in as an INTEGER from an SQL query.

For instance, if I wanted to search numbers that start with &quot;2&quot;, that would be awesome. I see the other link does this on the age field but it doesn't work on the integer field on my data. As soon as I type a number in the &quot;Filter By&quot; field all the data disappears.

I thought I read something telling someone how to eliminate numbers. I can't find it again.
Laura
mafdoc,
try changing this line
if(arrMembers[i].label.substr(0,fortext.length).toLowerCase() == fortext)

with this:
if(arrMembers[i].label.toString().substr(0,fortext.length).toLowerCase() == fortext)
mafdoc
12. mafdoc wrote on September 15, 2005 at 7:57 AM
Thanks very much Laura ! !
Worked like a charm. You're the greatest !
Shivang
13. Shivang wrote on September 22, 2005 at 11:06 AM
Hi Laura,

This is my first attempt on flash forms so may sound crazy. I am trying to build a search interface and options to search are first or last name. I do not want to refresh to screen to display results what would you suggest I can use? Any help is appreciated.
mafdoc
14. mafdoc wrote on September 23, 2005 at 7:06 AM
Is there a way to do two filters on the same cfform? I want to do them on separate pages on a tabnavigator.
I have one filtering on names and would like to do another on assets. Completely different/separate databases.
Thanks for any help.
mafdoc
15. mafdoc wrote on September 23, 2005 at 8:52 AM
Nahuel,
I got both filters to show on two different pages of the tabnavigator.
I duplicated the cfsavecontent and renamed to “astFilter”
Replaced data.dataProvider, Item.data and the 2nd grid to “astGrid”
(read somewhere that “data” was the name of the grid)
it’s now astGrid.dataProvider and Item.astGrid

Changed cfinput name from “forInput”
to “forAstInput”
And cfselect name from “column”
to “astcolumn”

They both show up BUT the duplicate doesn’t work.
When something is entered into the Filter by: field all the data disappears.
Thanks for any help.
mafdoc
16. mafdoc wrote on September 23, 2005 at 9:40 AM
Found this
var selected = column.selectedItem.data;
Changed to
var selected = astcolumn.selectedItem.data;
The data doesn't disappear any more but it doesn't filter either.
mafdoc
17. mafdoc wrote on September 23, 2005 at 10:17 AM
I found on http://www.asfusion.com/blog/index.cfm?mode=entry&amp;entry=DB477742-3048-525A-B2D68A2476E4DC69
“… I had to rename forInput to forInput1 and forInput2, rename column to column1 and column 2, and then rename the arrays to arr1 and arr2 …” posted by michael white.

I tried to rename the forInput to forInput1 and use it in the onChange but this made my cfform not show at all. How do I rename the forInput and the arr?
thanks for any help
mafdoc
18. mafdoc wrote on September 23, 2005 at 12:12 PM
&lt;cfsavecontent variable=&quot;invFilter&quot;&gt;
if(_global.arr1Members == undefined) _global.arr1Members = invgrid.dataProvider.slice(0);
var arr1Members = _global.arr1Members;
var arr1Display:Array = [];
var fortext1 = forMyInput.text.toLowerCase();
var selected = invcolumn.selectedItem.invgrid;

for(var i = 0; i &lt; arr1Members.length; i++)
{if(arr1Members[i][selected].toString().substr(0,fortext1.length).toLowerCase() == fortext1)
{arr1Display.push(arr1Members[i]); } }
invgrid.dataProvider = arr1Display;
&lt;/cfsavecontent&gt;

I've tried to change everything.

cfinput name=&quot;forMyInput&quot; onChange=&quot;#invFilter#&quot;
cfselect name=&quot;invcolumn&quot; onChange=&quot;forMyInput.text=''&quot;
cfgrid name=&quot;invgrid&quot;
Can you please tell me why this filter will not work. I get the data but when I type into the field all the data disappears.
Laura
Shivang,
If you are willing to download all the data, then you can use a filter like:
http://www.asfusion.com/blog/entry/filtering-a-cfgrid-as-you-type

Otherwise, you can use remoting:
http://www.asfusion.com/blog/entry/populating-a-cfgrid-with-flash-remoting
Philip Bedi
20. Philip Bedi wrote on February 04, 2006 at 2:32 AM
Hi Nahuel,

Thanks for the select filter it worked like magic, first I tried it with loading select values at the time form loads and it works fine, but to improve performance and cache I had to change the way form loads, now after loading the flash form in browser then I am populating the select values and after that the filter stops working, I don't know what have I done wrong, could you please help me.

Now I am loading form like this:
Cform on load event calls this function:loadPub();
function loadPub()
   {
      var pubThis = this;
      var pubService:Object = getRemoteService(function (results){ pubThis.finishedPublication(results); });
      pubService.getPublicationList();
   }
Then following function to populate.
function finishedPublication(results)
   {
      selectPublication.removeAll();
      selectPublication.dataProvider = results;
      selectPublication.addItemAt(0,'Select a Publication',-1);
      selectPublication.selectedIndex=0;
   }

Could you please enlight me to sort this thing.

Thanks
Philip Bedi
21. Philip Bedi wrote on February 08, 2006 at 6:05 AM
Hi Nahuel,

I am still waiting for some suggestion to my previous reply, If you need more clarification, please let me know?

Thanks
Laura
Philip,
Use
selectPublication.dataProvider = results.items;

Hope that helps
Philip Bedi
23. Philip Bedi wrote on February 18, 2006 at 6:01 AM
Thanks Laura, It worked like magic.

Thanks again,
Rey
24. Rey wrote on May 26, 2006 at 6:54 AM
I'm having some trouble applying this code to my forms, you guys are filtering the information from a list you guys created. How can I get the same results from a dynamic query coming from the database (please show code if possible) Thanks.
Laura
Rey,
Your problem must be somewhere else, most likely a case issue (AS is case sensitive, so check that your columns match exactly your query/db), because there is NO difference between a query manually constructed and a query from an actual database.
Rey
26. Rey wrote on May 30, 2006 at 11:07 AM
Nope I checked the syntaxt and everything seemed fine, can you show me the code for the actual query connection?
Rey
27. Rey wrote on May 30, 2006 at 11:11 AM
Is there a way I can submit information from a form with a button using a cfsavcontent such as: onClick="#insert#"

what I want to do is insert data into a DB without having to refresh the page, because what good are these form is we still have to deal with the POST/GET issues. I would like my web apps to behave like desktop apps. If anyone knows how to accomplish this please share it with me. thanks!
Laura
Rey,
I would recommend that you read the Real Estate sample application article. It explains how to add/delete/update data without refreshing the page:
http://www.macromedia.com/devnet/coldfusion/articles/flashforms_pt2.html
Rey
29. Rey wrote on May 31, 2006 at 5:22 AM
ok I'm looking over the search filter code, do where is this (if(_global.arrMembers == undefined) _global.arrMembers = data.dataProvider.slice(0);
   var arrMembers = _global.arrMembers;) being created? Because I copied it exactly as is from the source code you guys provided and change the query name to reflect the code and nothing works, I also updated to CF 7.01(latest update). I would really appreciate it if you could help me out with this. Thanks
Rey
30. Rey wrote on May 31, 2006 at 5:45 AM
Oh never mind I got it to work, it seems that it does not like when you replace "data" for something else, this was my problem I kept changing this. It works great!
Mark Cadle
31. Mark Cadle wrote on June 21, 2006 at 10:28 AM
I will post this in the other sections as well involving filtering a grid. If you are not using remoting and do not have an editable grid so to speak, if you run a cfgridupdate it will fail saying that it can not find the grid named XXXX.

This is only once you have filtered the grid. If you do not filter the grid then it will work. So at what point does this rename the grid? How can you keep the same grid name?

I update the grid via a cfinput that is bound to the selectedIndex of the grid and then have a button with onclick to:
gridname.dataProvider.editField(gridname.selectedIndex, 'COLUMN', textname.text);

This works great and the cfgridupdate works great even when you filter the grid, but once you filter the grid, you can not run the cfgridupdate.

I hope this makes sense.
SteveB
32. SteveB wrote on November 13, 2006 at 8:30 AM
This works great, but I need help with a variation on it...I have a query with ID and Name. I'd like to filter on the ID the user types, but Display the Name instead. How would I do this?? Thanks --Steve
SteveB
33. SteveB wrote on November 13, 2006 at 8:32 AM
This works great, but I need help with a variation on it...I have a query with ID and Name. I'd like to filter on the ID the user types, but Display the Name instead. How would I do this?? Thanks --Steve
Matthew Ross
Laura et. al.,

First off, huge fan of asfusion.com. I have learned so much from reading these posts and trying them at work. Great job keep it up!

Ok, to business. I am using the "sort as you type" technique with a cfgrid to display quite a bit of information. I am tring to add something like this underneath the cfgrid:

Displaying 1 of: ___

Where the ___ is a numeric value containing the current item count in the cfgrid. So if the grid starts with 200 items it is decreased as I type for the filter.

I have been trying in the actionFilter code to do something like:

countField = arrMember.length;

But this only yields the lenght once and does not change as I type in data for the filter.

Thoughts?

Respectfully,

- Matt R.
thomary
35. thomary wrote on February 08, 2007 at 5:29 AM
Replace arrMember.length
with ="{gridname.dataProvider.length}"
Tony
36. Tony wrote on April 02, 2007 at 11:46 AM
can anyone assist on having the list/dropdown revert back or go to the last record?
i'm using a query to get data with additional option for "ALL" value, but after removing filter it reverts to first record (slice(0)).
--
statements i've tried that do not work, or return null list,

if(arrMembers[i].label.toString().substr(0,fortext.length).toLowerCase() == "")
{ arrDisplay.push(arrMembers[arrMembers.length]);}
----
created a var to capture arrmembers.length and assign it to slice count

any guidance is greatly appreciated. thanks
tony
37. tony wrote on May 08, 2009 at 10:36 AM
i try the if(arrMembers[i][selected].toLowerCase().indexOf(fortext) != -1) to filter on any part of the word and not just the beginning.... and my flash movie wont load when coldfusion renders the page. is anyone else having a similar issue? i'm using mx7
tony
38. tony wrote on May 08, 2009 at 10:40 AM
Hi Laura,

when I change the code line from

if(item[selected].substr(0,fortext.length).toLowerCase() == fortext)
to:
if(arrMembers[i][selected].toLowerCase().indexOf(fortext) != -1)

it will not load the movie for me. I'm using mx7 and i'm trying to filter on any portion of the record and not just the beginning.

<cfsavecontent variable="changeForInput">
   if(_global.arrMembers == undefined) _global.arrMembers = data.dataProvider.slice(0);
   var arrMembers = _global.arrMembers;
   var arrDisplay:Array = [];
   var fortext = forInput.text.toLowerCase();
   
   for(var i = 0; i < arrMembers.length; i++)
    {
      if(arrMembers[i][selected].toLowerCase().indexOf(fortext) != -1)
      {
         arrDisplay.push(arrMembers[i]);
      }
   }
   data.dataProvider = arrDisplay;
</cfsavecontent>
tony
39. tony wrote on May 15, 2009 at 1:19 PM
just for a follow up... i discovered how to filter on a list without starting from the beginning of a record. you can filter anywhere in the record for the list. the "[selected]" piece i think is for grids only.

from
f(arrMembers[i].label.toString().substr(0,fortext.length).toLowerCase() == fortext)

to

if(arrMembers[i].label.toLowerCase().indexOf(fortext) != -1)
tony
40. tony wrote on May 15, 2009 at 1:20 PM
just for a follow up... i discovered how to filter on a list without starting from the beginning of a record. you can filter anywhere in the record for the list. the "[selected]" piece i think is for grids only.

from
if(arrMembers[i].label.toString().substr(0,fortext.length).toLowerCase() == fortext)

to

if(arrMembers[i].label.toLowerCase().indexOf(fortext) != -1)
Kim
41. Kim wrote on July 15, 2009 at 11:49 AM
Hi all

Have you come across where the filter behaves like an auto-suggest (i.e. same box). I found code for form=html but not form=flash (of course, the html doesn't work for flash).

Open to text boxes or cfselects.

Thanks for any help!
jim
42. jim wrote on November 17, 2009 at 8:33 PM
hi laura, im havin trouble when UPDATING CFGRID after filtering data using actionfilter variable on asfusion,

if i dont filter the data then the update works fine

Please tell me whats wrong?
Steve
43. Steve wrote on February 03, 2010 at 7:39 PM
I doubt youre still answering these blogs... Im updating some older code, tried this script on CF9 64 bit. Works on 32 bit, CF8 just fine. CF9 has a probelm with

onchange="#changeForInput#"

Leave your comment

Comment etiquette: As a gesture to those subscribed to this post, please keep your comments relevant to the post.

Your email address will never be displayed.
Email is gravatar enabled.Gravatar are the pictures you see next to the comments. If you like to have one, visit gravatar



Allowed tags:

<code>
All other tags will be shown as such, when in doubt, use the preview.

Leave this field empty:


Preview:

Refresh Preview
1. You wrote on