2 Replies Latest reply on Sep 13, 2012 9:09 PM by Santanu Karar

    Need help for filterFunction with special char

    Santanu Karar Level 1

      Hi,

       

      I'm doing a simple search/filter process to a dataGrid collection upon text inputs with filterFunction. What I'm facing problem is, in one of my column field (on which I'm filtering) has dot "." as a suffix and it doesn't includes in the search process.

       

      Say I've a few values like - i_24., i_245., i_256.

       

      Now with the present filterfunction when an user inputs i_24. it shows the other record as well - i_245., where the expected entry should only be i_24. (since i_245. is not i_24.5)

      But it seems the search function is not taking the dot "." as a character in the search process, or at least when it is stays as suffix.

       

      I thouht maybe some RegExp could overcome the problem, but I'm not good in that either.

       

      Please, help.

       

       

      Thank you.

       

      Regards,

       

      Santanu

        • 1. Re: Need help for filterFunction with special char
          NMed

          aSuitableBoy,

          Use the String's "indexOf" method.  (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#indexOf()).
          The syntax would be: "String(<YOUR_COLLECTION>.<FIELD_TO_FILTER_ON>).indexOf(<COMPARE_STRING_FROM_TEXT_INPUT>)

           

          Here's a really small code snippet that I just wrote up, that works:

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="cc();">

                    <mx:Script>

                              <![CDATA[

                                   import mx.controls.Alert;

                                   import mx.collections.ArrayCollection;

                                   [Bindable] private var ac:ArrayCollection = new ArrayCollection();

           

                              private function cc():void

                              {

                                             var o:Object = new Object();

                                             o.field1 = "Value 1:";

                                             o.field2 = "i_24.";

                                             o.field3 = "Some other value...";

                                             ac.addItem(o);

                                             o = new Object();

                                             o.field1 = "Value 2:";

                                            o.field2 = "i_245.";

                                             o.field3 = "Some other value...";

                                             ac.addItem(o);

                                             o = new Object();

                                            o.field1 = "Value 3:";

                                             o.field2 = "i_256.";

                                             o.field3 = "Some other value...";

                                             ac.addItem(o);

                              }

           

                              private function filterFunc(o:Object):Boolean

                              {

                                             return String(o.field2).indexOf("i_24.") != -1;

                              }

                              ]]>

                    </mx:Script>

                    <mx:HBox width="100%" height="100%">

                                   <mx:DataGrid id="dg" height="100%" width="100%" dataProvider="{ac}">

                                                  <mx:columns>

                                                                 <mx:DataGridColumn headerText="Column 1" dataField="field1" />

                                                                 <mx:DataGridColumn headerText="Column 2" dataField="field2" />

                                                                 <mx:DataGridColumn headerText="Column 3" dataField="field3" />

                                                  </mx:columns>

                                   </mx:DataGrid>

                                   <mx:Button id="b1" label="Filter" click="{ac.filterFunction = filterFunc; ac.refresh();}" />

                    </mx:HBox>

          </mx:Application>

          • 2. Re: Need help for filterFunction with special char
            Santanu Karar Level 1

            Ah! that actually works! Since indexOf search string in a straight line way and returns the first occurance! Previously I was using 'search', 'match' etc. Thank you! It was a simple treatment but I distracted from it! Thanks!