0
votes

I have couple of dropdownlist controls, that shares the same dataprovider(same reference).

I had overridden the set dataprovider method for a sort function.(code below). The issue is that, when I set this shared dataprovider to a new dropdownlist, all the existing dropdown contorls sharing the dataprvider gets unselected(loses its previously selected values).

    override public function set dataProvider(value:IList):void{
            if(value is ArrayCollection){
            var sort:Sort=new Sort();
            var sortField:SortField = new SortField();
            sortField.numeric=false;
            sort.fields=[sortField];

            ArrayCollection(value).sort=sort;
            ArrayCollection(value).refresh();
        }
        super.dataProvider=value; 
    }
2
Sorting the dataProvider in the each component seems like it would introduce random issues like this now and/or in the future. Is it possible to just sort it once outside of the component? In general, a component should not modify the data is it rendering (some of the Flex chart components do this, and they warn you that they "corrupt" the data). The suggestion below from @Markus Rossler is spot on, except be careful if the index changes as a result of the sort.Sunil D.

2 Answers

1
votes

There are a ton of isues sharing the dataProvider between components. We've run into this with a lot of clients using our AutoCompleteComboBox.

You can easily use the same source, but a different--separate--collection for each of your dataProviders.

var dataProvider1 :ArrayCollection = new ArrayCollection(someArray);
var dataProvider2 :ArrayCollection = new ArrayCollection(someArray);
var dataProvider3 :ArrayCollection = new ArrayCollection(someArray);

Each collection is just a wrapper around the base source. Sorting one will not affect any of the others, leaving your other ComboBoxes or DropDownLists untouched.

0
votes

I did no research on this, but there are two issues/ideas coming up:

  1. if you literally use the same reference to the same arraycollection, you do not need to sort this array more than once (and you actually do this by assigning the same arraycollection more than once)
  2. if it about only single-selection dropdowns, then there is a simple solution:

    var oldSelected : TypeOfItem = selectedItem as TypeOfItem;
    // do the sort (like in your code)
    super.dataProvider=value;
    selectedIndex = getItemIndex(oldSelected);