6 Replies Latest reply on Jan 6, 2010 3:48 PM by tehxike1

    Grid's selectedItems is broken after filterFunction

    tehxike1 Level 1

      I have a grid bound to an ArrayCollection that has a filterFunction.  If I filter the collection, reset the grid's selectedItems (selectedItems = []), then click an item, the grid's selectedItems array is empty.  Here's the smallest app I could write to demonstrate the problem:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/halo"
                     xmlns:ns="library://ns.adobe.com/flex/mx"
                     minWidth="1024" minHeight="768"
                     initialize="initHandler(event)" >
          <s:layout><s:VerticalLayout/></s:layout>
          <fx:Script>
              <![CDATA[
                  import mx.collections.ArrayCollection;
                  import mx.events.FlexEvent;
                  import mx.events.ListEvent;
                 
                  import spark.events.TextOperationEvent;
                 
                  [Bindable] private var dp:ArrayCollection;
                 
                  private function initHandler(event:FlexEvent):void
                  {
                      dp = new ArrayCollection(["first","second","third","fourth","fifth","sixth","seventh","eighth"]);
                      dp.filterFunction = filterFunction;
                  }
                 
                  private function filterTxt_changeHandler(event:TextOperationEvent):void
                  {
                      dp.refresh();
                      grid.selectedItems = [];
                      grid_itemClickHandler(null);
                  }
                 
                  private function filterFunction(item:String):Boolean
                  {
                      if (filterTxt.text.length > 0)
                          return item == filterTxt.text;
                      else
                          return true;
                  }
                 
                  private function grid_itemClickHandler(event:ListEvent):void
                  {
                      grid2.dataProvider = grid.selectedItems;
                  }
              ]]>
          </fx:Script>
          <ns:HBox>
              <s:TextInput id="filterTxt" change="filterTxt_changeHandler(event)" />
          </ns:HBox>
          <ns:DataGrid id="grid" dataProvider="{dp}" itemClick="grid_itemClickHandler(event)">
              <ns:columns>
                  <ns:DataGridColumn dataField="name" headerText="name" />
              </ns:columns>
          </ns:DataGrid>
          <s:Label text="Selected Items:" />
          <ns:DataGrid id="grid2" >
              <ns:columns>
                  <ns:DataGridColumn dataField="name" headerText="name" />
              </ns:columns>
          </ns:DataGrid>
      </s:Application>

       

      To reproduce the problem with this code:

      1) click an item- notice that selectedItems is showing up properly in the lower grid

      2) type something in the textinput, then delete it

      3) click another item- notice that no selectedItems are showing up anymore in the lower grid