2010-12-12 111 views
2

如何將項目添加到多維數組?我想補充的價格ITEM2,但我得到「推是不是一個函數」將項目添加到數組

<fx:Script> 
    <![CDATA[ 

     [Bindable] 
     public var dp:Array = [ 
      { label: "item1", desc: "this is item 1" }, 
      { label: "item2", desc: "this is item 2" }, 
      { label: "item3", desc: "this is item 3" } 
     ]; 


     private function addItem():void{ 
      var v:String = '$8.99'; 
      dp[1].push(("price:", v)); 
      dp.refresh(); 

     } 
    ]]> 
</fx:Script> 


<mx:VBox> 
    <mx:Repeater id="r" dataProvider="{myAC}"> 
     <mx:RadioButton label="{r.currentItem.label}"/> 
     <mx:Text text="{r.currentItem.desc}"/> 
     <mx:Text text="{r.currentItem.price}"/> 
    </mx:Repeater> 


<s:Button label="push" click="addItem()"/> 


</mx:VBox> 


</mx:Application> 

回答

4

你有對象的數組,而不是數組的數組。試試這個:

[Bindable] 
    public var dp:Array = [ 
     { label: "item1", desc: "this is item 1", price : "" }, 
     { label: "item2", desc: "this is item 2", price : "" }, 
     { label: "item3", desc: "this is item 3", price : "" } 
    ]; 

    private function addItem():void{ 
     var v:String = '$8.99'; 
     dp[1]["price"] = v; 
     dp.refresh(); 

    } 
+0

...'addItem'真的應該改變,以反映行動... – 2010-12-12 17:30:54

+0

現在我得到一個錯誤:「刷新()」不是一個函數 – 2010-12-12 19:27:31

+0

使用ArrayCollection的,而不是陣列:公var dp:ArrayCollection = new ArrayCollection {label:「item1」,desc:「this is item 1」,price:「」}, {label:「item2」,desc:「this is item 2」,price :「」}, {label:「item3」,desc:「this is item 3」,price:「」} ]); 看到:http://blog.flexdevelopers.com/2009/03/flex-basics-arraycollection.html – wajiw 2010-12-12 19:36:20