Flex practice -- display array data changes in time in the DataGrid

Source: Internet
Author: User
Tags addchild

I spent two hours studying how to dynamically reflect the changes of array data when the DataGrid displays array data. Here, I will share the results of the study with you ....

Test. mxml

<? XML version = "1.0" encoding = "UTF-8"?> <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" backgroundgradientalphas = "[1.0, 1.0]" backgroundgradientcolors = "[# f3cbcb, # e9ee9a] "fontsize =" 15 ">
<Mx: States>
<Mx: State name = "result">
<Mx: removechild target = "{canvas1}"/>
<Mx: addchild position = "lastchild">
<Mx: DataGrid x = "290" Y = "52" width = "570" Height = "260" id = "DG" dataprovider = "{parray}"> <mx: columns>
<Mx: datagridcolumn headertext = "Pizza name" datafield = "name"/> <mx: datagridcolumn headertext = "size" datafield = "size"/> <mx: datagridcolumn headertext = "unit price" datafield = "price"/>
</MX: columns>
</MX: DataGrid>
</MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: button x = "539" Y = "402" label = "continue to purchase" Click = "currentstate = & quot;"/> </MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: Label x = "261" Y = "337" text = "Pizza name:"/>
</MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: Label x = "358" Y = "337" text = "{DG. selecteditem. name} "width =" 127 "id =" itemname "/> </MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: Label x = "487" Y = "337" text = "Size:"/>
</MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: Label x = "545" Y = "337" text = "{DG. selecteditem. size} "width =" 96 "id =" itemsize "/> </MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: Label x = "659" Y = "337" text = "unit price:"/>
</MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: Label x = "717" Y = "337" text = "{DG. selecteditem. price} "width =" 107 "id =" itemprice "/> </MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: button x = "832" Y = "335" label = "delete" Click = "collectinfo1 ()"/>
</MX: addchild>
</MX: State>
<Mx: State name = "deletesuccess">
<Mx: removechild target = "{canvas1}"/>
<Mx: addchild position = "lastchild">
<Mx: Label x = "461" Y = "187" text = "This item is deleted successfully! "/>
</MX: addchild>
<Mx: addchild position = "lastchild">
<Mx: button x = "480" Y = "246" label = "return" Click = "currentstate = & quot; Result & quot;"/> </MX: addchild>
</MX: State>
</MX: States>
<Mx: script source = "myscript. As"/>
<Mx: SCRIPT>
<! [CDATA [
Import class .*;
Private function collectinfo (pname: String, psize: String, pprice: string): void {
VaR pizza: Pizza = new pizza ();
Pizza. setname (pname );
Pizza. setsize (INT (psize ));
Pizza. setprice (number (pprice ));
Addpizza (pizza );
}
Private function collectinfo1 (): void {
VaR pizza: Pizza = new pizza ();
Pizza. setname (itemname. Text );
Pizza. setsize (INT (itemsize. Text ));
Pizza. setprice (number (itemprice. Text ));
Deletepizza (pizza );
}
]>
</MX: SCRIPT>
<Mx: canvas x = "188.5" Y = "97" width = "670" Height = "404" id = "canvas1">
<Mx: Image x = "23" Y = "10" Source = "../image/pizza1.jpg" width = "242" Height = "208"/>
<Mx: Label x = "117" Y = "281" text = "Italian pizza" fontsize = "15" id = "pname"/>
<Mx: Label x = "23" Y = "318" text = "unit price:"/>
<Mx: Label x = "81" Y = "318" text = "30" id = "pprice"/>
<Mx: Label x = "114" Y = "318" text = "RMB/block"/>
<Mx: Label x = "457" Y = "318" text = "RMB/block"/>
<Mx: Label x = "182" Y = "318" text = "Size:"/>
<Mx: Label x = "515" Y = "318" text = "Size:"/>
<Mx: Label x = "273" Y = "318" text = "Inch"/>
<Mx: Label x = "607" Y = "318" text = "Inch"/>
<Mx: Label x = "240" Y = "318" text = "13" id = "psize"/>
<Mx: button x = "139" Y = "350" label = "buy" Click = "collectinfo (pname. text, psize. text, pprice. text) "/> <mx: button x =" 441 "Y =" 350 "label =" buy "Click =" collectinfo (pname2.text, psize2.text, pprice2.text) & quot;/> <mx: Image x = "334" Y = "10" Source = ".. /image/moric.jpg "width =" 231 "Height =" 253 "/>
<Mx: Label x = "411" Y = "281" text = "Mexican pizza" id = "pname2"/>
<Mx: Label x = "366" Y = "318" text = "unit price:"/>
<Mx: Label x = "424" Y = "318" text = "40" id = "psize2"/>
<Mx: Label x = "561" Y = "318" text = "12" id = "pprice2"/>
</MX: canvas>

</MX: Application>

Myscript.

Import class .*;
Import MX. Controls. Alert;
Public var parray: array = new array ();
Public Function addpizza (pizza: Pizza): void {
VaR OBJ: Object = new object ();
OBJ. Name = pizza. getname ();
OBJ. size = pizza. getsize ();
OBJ. Price = pizza. getprice ();
Parray. Push (OBJ );
Currentstate = "result ";
}
Public Function deletepizza (pizza: Pizza): void {
VaR sign: String = "1 ";
For (var I: Number = 0; I <parray. length; I ++ ){
If (parray [I]. Name = pizza. getname ()){
Sign = "0"; parray. splice (I, 1); break;
}
} If (Sign = "0 "){
Currentstate = "deletesuccess ";
} Else if (Sign = "1 "){
Alert. Show ("You have not ordered" + pizza. getname ());
}
}

Pizza. As (put in the class package)

Package class {
Public class pizza {
Private var name: string;
Private var size: int;
Private var price: number;
Public Function pizza (){
}
Public Function setname (Name: string): void {
This. Name = Name;
}
Public Function setsize (size: INT): void {
This. size = size;
}
Public Function setprice (price: Number): void {
This. Price = price;
}
Public Function getname (): String {
Return name;
}
Public Function getsize (): int {
Return size;
}
Public Function getprice (): number {
Return price;
}
}
}

Running result:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.