ItemRender---实例之动态计算表格中的一行结果

动态计算表格中的一行结果。

<?xml version="1.0" encoding="utf-8"?> <!-- this application can be named anything you want, but the itemRenderer file name must match the name specified in the Net Ship Weight Column --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="left" creationComplete="initApp();"> <mx:Script><!--[CDATA[ import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.utils.*; [Bindable]private var _acData:ArrayCollection; //dataProvider for DataGrid //called by application.creationComplete event //populates the dataProvider with sample data public function initApp():void { _acData = new ArrayCollection( [{unit_wt:30,quantity:1,net_wt:0}, {unit_wt:65,quantity:0,net_wt:0}, {unit_wt:25,quantity:1,net_wt:0}, {unit_wt:35,quantity:0,net_wt:0}, {unit_wt:45,quantity:2,net_wt:0}, {unit_wt:55,quantity:0,net_wt:0}, {unit_wt:75,quantity:3,net_wt:0}, {unit_wt:95,quantity:0,net_wt:0}, {unit_wt:105,quantity:9,net_wt:0}]); }//initApp //run by button click private function showDataProvider():void { var sTemp:String = "unit_wt, quantity, net_wt"; for (var i:int=0;i<_acData.length;i++) { sTemp += "/n" + _acData[i].unit_wt + ", " + _acData[i].quantity + ", " + _acData[i].net_wt; } Alert.show(sTemp); }// ]]></mx:Script> <mx:DataGrid id="dgShipWeight" dataProvider="{_acData}" editable="true" rowCount="5" > <mx:columns> <mx:Array> <mx:DataGridColumn headerText="Unit Weight" dataField="unit_wt" editable="false"/> <mx:DataGridColumn headerText="Quantity Shipped" dataField="quantity" editable="true" /> <mx:DataGridColumn headerText="Net Ship Weight" editable="false" > <mx:itemRenderer> <mx:Component> <mx:HBox> <mx:Script> <![CDATA[ import mx.utils.StringUtil; override public function set data( oItem:Object ) : void { super.data = oItem; if (data != null) { if (data.quantity != null &&StringUtil.trim(data.quantity) != "") { var nNetWt:Number = data.unit_wt * data.quantity; //calculate the total weight value myText.text = nNetWt.toString(); //display it in the column } } } ]]--> </mx:Script> <mx:Text id="myText" /> </mx:HBox> </mx:Component> </mx:itemRenderer> </mx:DataGridColumn> </mx:Array> </mx:columns> </mx:DataGrid> <mx:Button label="Show Data Provider" click="showDataProvider()" /> </mx:Application>

你可能感兴趣的:(ItemRender---实例之动态计算表格中的一行结果)