当前位置: 代码迷 >> Web前端 >> 在DataGrid 中运用LinkButtion(重写LinkButtion)
  详细解决方案

在DataGrid 中运用LinkButtion(重写LinkButtion)

热度:134   发布时间:2012-10-28 09:54:44.0
在DataGrid 中使用LinkButtion(重写LinkButtion)

在DataGrid 中使用LinkButtion(重写LinkButtion):
(1) MyGrid.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
????? xmlns:s="library://ns.adobe.com/flex/spark"
????? xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
????? creationComplete="application1_creationCompleteHandler(event)">
?<fx:Declarations>
??<!-- Place non-visual elements (e.g., services, value objects) here -->
?</fx:Declarations>
?
?<mx:DataGrid id="mygrid">
??<mx:columns>
???<mx:DataGridColumn dataField="name" headerText="姓名"/>
???<mx:DataGridColumn dataField="sex" headerText="性别"/>?
???<mx:DataGridColumn dataField="age" headerText="年龄"/>
???<mx:DataGridColumn dataField="name2" headerText="姓名2" itemRenderer="{item}"/>
???<mx:DataGridColumn dataField="name2" headerText="姓名2" itemRenderer="{item2}"/>
??</mx:columns>
?</mx:DataGrid>
?
?<fx:Script>
??<![CDATA[
???import but.MyLinkButton;
???
???import mx.collections.ArrayCollection;
???import mx.controls.LinkButton;
???import mx.events.FlexEvent;
???[Bindable]? //必须绑定,否则不显示
???private var item:ClassFactory; //没有重写
???[Bindable]
???private var item2:ClassFactory; //必须重写,否则无法处理,点击之后需要处理的

???protected function application1_creationCompleteHandler(event:FlexEvent):void
???{
????var arr:ArrayCollection=new ArrayCollection();
????var obj:Object=new Object();
????obj.name="1";
????obj.age="20"
????obj.sex="男";
????arr.addItem(obj);
????
????obj=new Object();
????obj.name="11";
????obj.age="22"
????obj.sex="男";
????arr.addItem(obj);
????
????obj=new Object();
????obj.name="12";
????obj.age="23"
????obj.sex="女";
????arr.addItem(obj);
????
????mygrid.dataProvider=arr;
????
????item=new ClassFactory(LinkButton);?//没有重写的button,没有什么用处
????item.properties={label:'修改',data:'edit'};
????
????//重写LinkButton,重写protected function clickHandler(event:MouseEvent):void,只有继承才可以改变这个方法,子类覆盖父类
????item2=new ClassFactory(MyLinkButton);
????//item2.properties={label:'删除',data:'delete'}; //获取的data=null
????var dobj:Object=new Object();
????dobj.name="abc";
????item2.properties={label:'删除',data:dobj}; //data是个object,可以获取到值
???}

??]]>
?</fx:Script>
</s:Application>

?

(2) MyLinkButton.as:

package but
{
?import flash.events.MouseEvent;
?
?import mx.controls.Alert;
?import mx.controls.LinkButton;
?
?public class MyLinkButton extends LinkButton
?{
??public function MyLinkButton()
??{
???super();
??}
??
??//重写一个方法,必须加override
??override protected function clickHandler(event:MouseEvent):void{
???super.clickHandler(event); //可写可不写
???Alert.show("--"+this.data.name,"提示");
???Alert.show(super.data.name); //this.data.name 与super.data.name一样
??}
??
??//如果没有data set方法,上面的this.data as String =null
??//如果页面上data 传的是个object,不需要 data set方法
??/*override public function set data(value:Object):void
??{
???super.data=value;
??}*/
?}
}

?

还有一个问题,就是LinkButton的样式?

  相关解决方案