有4种方法可以为数据表格定义项目渲染器和编辑器。
1.属性赋值
2.内嵌方式
3.用MXML自定义组件
4.用ActionScript自定义组件
举例:
1.属性赋值
<mx:DataGridColumn dataField="value"
headerText="Value"
itemEditor="mx.controls.TextInput"
editorDataField="text"/>
2.内嵌方式
<mx:DataGridColumn dataField="key" headerText="内嵌式列" editable="true">
<mx:itemEditor>
<mx:Component>
<mx:TextInput change="onchange()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function onchange():void{
var textInput:TextInput=TextInput(this);
if(this.text==""){
Alert.show("不能为空!");
}
if(this.text!=""){
if(this.text.indexOf(",",0)!=-1){
this.text=this.text.substr(0,text.length-1);
Alert.show("不能包含,");
}
}
}
]]>
</mx:Script>
</mx:TextInput>
</mx:Component>
</mx:itemEditor>
</mx:DataGridColumn>
3.用MXML自定义组件
<mx:DataGridColumn dataField="key" editable="true">
<mx:itemEditor>
<mx:Component>
<mycomponents:TextInputValidate/>
</mx:Component>
</mx:itemEditor>
</mx:DataGridColumn>
组件:TextInputValidate.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:TextInput xmlns:mx="http://www.adobe.com/2006/mxml" change="onchange()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function onchange(){
var textInput:TextInput=TextInput(this);
if(this.text!=""){
if(this.text.indexOf(",",0)!=-1){
this.text=this.text.substr(0,text.length-1);
Alert.show("不能包含,");
}
}
}
]]>
</mx:Script>
</mx:TextInput>
4.用ActionScript定义组件
<mx:DataGridColumn dataField="value"
itemEditor="org.sotower.bpm.webservice.client.view.mycomponents.TextInputRender"/>
组件:TextInputRender.as
package org.sotower.bpm.webservice.client.view.mycomponents
{
import flash.events.Event;
import mx.controls.Alert;
import mx.controls.TextInput;
public class TextInputRender extends TextInput
{
public function TextInputRender()
{
super();
this.addEventListener(Event.CHANGE,onchange);
}
public function onchange(e:Event){
var textInput:TextInput=TextInput(this);
if(this.text!=""){
if(this.text.indexOf(",",0)!=-1){
this.text=this.text.substr(0,text.length-1);
Alert.show("不能包含,");
}
}
}
}
}