Search code examples
actionscript-3apache-flextreeviewflex4.5itemrenderer

Flex Tree does not selecting ("highlight”) the selectedIndices


I have an array of items that should be selected in my tree control. As you can see from my code below,I bind this array to the selectedIndices property of the tree. The selectedItems are not properly selecting in tree(selecting some other items and always root is selected).Flex seems to "ignore" my items (selects some other indices). Am I missing something?

Perhaps I'm going about this in wrong way?

Thanks for your help!

My XMLList:-

<fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <fx:XMLList id="XMLList">
        <node>
            <node name="max">
                <node name="Emanuele"
                      surname="Tatti" age="23" wage="1200"/>
                <node name="Francesco" 
                      surname="Rapana " age="22" wage="1000"/>
                <node name="Constantin"
                      surname="Moldovanu" age="23" wage="1200"/>
                <node name="Marco" 
                      surname="Casario" age="29" wage="1500">
                    <node name="Marco" 
                          surname="Casario" age="29" wage="1500">
                        <node name="Marco" 
                              surname="Casario" age="29" wage="1500">
                            <node name="Marco" 
                                  surname="Casario" age="29" wage="1500">
                            </node>
                        </node>
                    </node>                 
                </node>
            </node>

        </node>
        </fx:XMLList>
</fx:Declarations>

My Action Script Function:-

public function select_tree():Void 
{
tree.validateNow();
var allItems:Array = new Array();
for(var n:Int =2;n<7;n+2)       
{
        allItems[n]=n; // o/p- 2,4,6
}

tree.selectedIndices = allItems1;   //2,4,6 items should select ,but 0,2,4,5 are selected why?
      }

*****My MXML:-*****
<mx:Button id="btn" label="Find Unmatch Nodes" width="221" height="30" click="select_tree()"/>



<mx:Tree id="tree"  right="10" top="54" bottom="10" width="49.5%" dataProvider="{XMLList}"
    fontFamily="Verdana" fontSize="11" showScrollTips="true"
    allowMultipleSelection="true"
    alternatingItemColors="[#F5F5F5]"
    labelField="@label" selectionColor="#ECF335" showRoot="false"/>

Solution

  • You have a mistake in your code by setting tree.selectedIndices to allItems1 (instead of allItems).

    tree.selectedIndices = allItems1;
    

    Why don't you just created a Bindable Array, and have it set to the selectedIndices property of the tree?

    i.e.

    [Bindable]
    public var selectedTreeValues:Array = new Array();
    
    ...
    
    <mx:Tree id="tree"  right="10" top="54" bottom="10" width="49.5%" 
        dataProvider="{XMLList}"
        fontFamily="Verdana" fontSize="11" showScrollTips="true"
        allowMultipleSelection="true"
        alternatingItemColors="[#F5F5F5]"
        labelField="@label" selectionColor="#ECF335" showRoot="false"
        selectedIndices="selectedTreeValues"
    />
    

    Finally, trace that out, instead of having to do it manually? One of the beauties of Flex is being able to leverage Binding values.