5 Replies Latest reply on Apr 20, 2009 2:37 PM by wvxvw

    Struggling with XMLListCollection filter. . .

    bitwyse Level 1

      Hi All,

       

      I have an xmllistCollection as a data provider to a data grid.  I want to add a simple filter to the datagrid.  I do this with other datagrids by adding a text input field above the datagrid with a changeEvent that sets a filter function to the arraycollection and then refreshes the arraycollection.  It works great.

       

      Now I'm trying to do the same with a datagrid that has an xmlListCollection as it's dataproivider and I can not figure out how to get this to work.

      Below is the code I am using to do the filtering and below that is the xml that is being passed into the filter function.  I'm hoping someone can help me figure this out.  I'm stuck!!!!

       

      //  Search is called on the change event of my text input field.

      private function search():void
              {
                  if(searchField.text !='' )
                      filter()
                  else
                      filterReset()
              }

       

      private function filter():void
              {
                  gLMSNewsLetterXMLCollection.filterFunction = filterRoutine;
                  gLMSNewsLetterXMLCollection.refresh();
              }

       

              private function filterRoutine( items : Object ) : Boolean
              {
                  for each( var anItemValue : XML in items )
                  {  //  See a sample snap shot of the xml that anItemValue contains.
                      if( anItemValue != null )
                      {
                          var tempStr : String = anItemValue.viewentry.entrydata.text;

                           //  tempStr is always NULL.  I've tried anItemValue.viewentry.entrydata.(@tex)

                          //  and many other combo's and can't get anything but null
                          if( tempStr.match( new RegExp( searchField.text, 'i' ) ) )
                              return true
                      }
                  }
                  return false;
              }

       

       

      <viewentry position="1" unid="A49D4FB27DF18442862573CF00241F27" noteid="FE32" siblings="32">
        <entrydata columnnumber="0" name="docTitle">
          <text>
            2007-October - Financing Options For Special Assessments
          </text>
        </entrydata>
        <entrydata columnnumber="1" name="AnnouncementText">
          <text>
            In a perfect world there would be no special assessments. Every Association would adequately fund for the future and the upcoming major projects would be paid for out of
          </text>
        </entrydata>
        <entrydata columnnumber="2" name="$13">
          <text>
            /STAR\EstarDoc.nsf/vwLMSNews/A49D4FB27DF18442862573CF00241F27/$File/LMSATB200710.pdf?Open Element
          </text>
        </entrydata>
      </viewentry>

        • 1. Re: Struggling with XMLListCollection filter. . .
          Flex harUI Adobe Employee

          XML is hierarchical so for-each may miss things.  I always trace toXMLString of items and other temp vars to see what they really contain.  It is almost always the case that I'm off one level or another.

           

           

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Struggling with XMLListCollection filter. . .
            wvxvw Level 1
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                <mx:Script>
                    <![CDATA[
                        import flash.events.Event;
                        import mx.controls.TextInput;
                       
                        private function filterList(event:Event):void
                        {
                            var input:TextInput = event.currentTarget as TextInput;
                            lisToFilter.dataProvider = resetDataProvider(input.text);
                        }
                       
                        private function resetDataProvider(text:String = null):XMLList
                        {
                            var filteredList:XMLList;
                            if (text && text.length)
                            {
                                var matchRE:RegExp = new RegExp(text.split("").join("|"));
                                filteredList =
                                    xmlToFilter..entrydata.(String(@columnnumber).match(matchRE)).text.text();
                                    lisToFilter.dataProvider = filteredList;
                            }
                            else
                            {
                                filteredList = xmlToFilter..text.text();
                            }
                            return filteredList;
                        }
                    ]]>
                </mx:Script>
                <mx:XML id="xmlToFilter">
                    <viewentry
                        position="1"
                        unid="A49D4FB27DF18442862573CF00241F27"
                        noteid="FE32"
                        siblings="32">
                        <entrydata
                            columnnumber="0"
                            name="docTitle">
                            <text>
                            2007-October - Financing Options For Special Assessments
                            </text>
                        </entrydata>
                        <entrydata
                            columnnumber="1"
                            name="AnnouncementText">
                            <text>
                            In a perfect world there would be no special assessments. Every Association would adequately fund for the future and the upcoming major projects would be paid for out of
                            </text>
                        </entrydata>
                        <entrydata
                            columnnumber="2"
                            name="$13">
                            <text>
                            /STAR\EstarDoc.nsf/vwLMSNews/A49D4FB27DF18442862573CF00241F27/$File/LMSATB20071 0.pdf?OpenElement
                            </text>
                        </entrydata>
                    </viewentry>
                </mx:XML>
                <mx:Label
                    text="Type numbers 0, 1 or 2, or any combination to filter the list below"
                    />
                <mx:TextInput
                    id="theInput"
                    change="filterList(event)"
                    />
                <mx:List
                    id="lisToFilter"
                    dataProvider="{resetDataProvider()}"
                    />
            </mx:Application>

             

            Enjoy

            • 3. Re: Struggling with XMLListCollection filter. . .
              bitwyse Level 1

              Thank you for the help !!!

               

              It turns out after reading your code I was able to make a simple tweak to my code to get things to work.

               

              I was trying to update tempStr with anItemValue.viewentries.entrydata.text . . .

              So I changed the code below to loop through the entrydata nodes and now I'm able to fetch anItemValue.text

               

              To be honest, I still don't get why this works and my original code doesn't.  I guess I'm an XML dork .  Everytime I have to deal with XML I struggle for hours trying to figure this out.

               

              Thanks for the help!!!!!!

               

              private function filterRoutine( items : Object ) : Boolean
                      {
                          for each( var anItemValue : XML in items.entrydata )
                          {
                              if( anItemValue != null )
                              {
                                  var tempStr : String = anItemValue.text;
                                  if( tempStr.match( new RegExp( searchField.text, 'i' ) ) )
                                      return true
                              }
                          }
                          return false;
                      }

              • 4. Re: Struggling with XMLListCollection filter. . .
                wvxvw Level 1

                Well, that's because your collection was populated with <text/> nodes, not the top-level nodes which you originally wanted to access.

                Glad to be of any help

                • 5. Re: Struggling with XMLListCollection filter. . .
                  m_hartnett Level 3

                  You can shorten your find function to be like this:  You can address the value you want by index or by shortcut of ..

                   

                   

                   

                   

                   

                   

                   

                   

                   

                  private function filterRoutine( items : Object ) : Boolean {

                   

                   

                       var testVal:String = items[0].text;

                   

                   

                       var textVal1:String = items..text;

                   

                   

                   

                       if( testVal.match( new RegExp( searchField.text, 'i' ) ) )

                   

                   

                            return true

                   

                   

                  return false

                  ;