Macromedia Flex Macromedia Flex
Set Initial Items of a mult-select List
  Home

Jul 08, 2005 - Set Initial Items of a mult-select List
Using a delimited string, select the items in a mult-select list

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" horizontalAlign="left"
  initialize="initApp()">
<mx:Script><![CDATA[
 private var gaDP1:Array;
 
 private function initApp():Void
 {
  gaDP1 = [ {label:"A", data:0},
     {label:"B", data:1},
     {label:"C", data:2},
     {label:"D", data:3},
     {label:"E", data:4},
     {label:"F", data:5},
     {label:"G", data:6},
     {label:"H", data:7},
     {label:"I", data:8},
     {label:"J", data:9}];  //build the dataProvider
 }
 
 //runs on click of button
 private function setInitialItem():Void
 {
  var aDP:Array = mx.utils.ArrayUtil.toArray(list1.dataProvider);
  var sDataValuesInit:String = tiInitialData.text;
  var aSetItems:Array = sDataValuesInit.split(","); //split the initial data into an array
  var sDPItemDataCur:String;
  var aSelectedIndices:Array = new Array();
  for ( var i:Number=0; i<aDP.length; i++ )  {  //loop over the items in the dataProvider
   sDPItemDataCur = aDP[i].data;  //get the current item.data value
   for (var j:Number=0;j<aSetItems.length;j++)  {              //loop over the setItems array
    if ( sDPItemDataCur == aSetItems[j] )  {  //compare desired value to current item.data value
     aSelectedIndices.push(i)  //put the matching item index on the array
    }
   }
  }//for ( var i:Number=0;....
  list1.selectedIndices = aSelectedIndices;  //set the seletedIndex of the combo box
 }
 
 //example of labelFunction.  Creates a label out of both properties
 private function lfCB1(oItem:Object):String
 {
  return oItem.label + "  ( data:" + oItem.data + ")";
 }
]]></mx:Script>
   <mx:HBox>
  <mx:Label text="Initial Data Value (0-9)" />
  <mx:TextInput id="tiInitialData" text="1,3,5" />
  <mx:Button label="Set Item" click="setInitialItem()"/>
 

    <mx:List id="list1" dataProvider="{gaDP1}" width="150" labelFunction="lfCB1" multipleSelection="true" />

  </mx:HBox>
</mx:Application>

File Details
Created On Jul, 08, 2005 by Tracy Spratt
Last Modified On Jul, 08, 2005 by Tracy Spratt
Group: Tips and Articles
Flex Versions: All
Category: General
Type: Complete Lesson
Difficulty: Beginner
Keywords:
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80