对itemrenderer的一些认知(2)-与父容器的交互

本文介绍在Flex中如何实现在ItemRenderer中与父容器DataGrid进行交互,特别是通过获取父容器及其dataProvider来实现复杂的数据展示逻辑。此外,还讨论了如何使用validateNow()方法来更新ItemRenderer的显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在编写itemrenderer时,会有时候用到与父容器的交互的需求,实现的方法不难

比如在一个datagrid里,相邻的行之前的数据属性有联系,这里给一个例子是上一行的背景颜色是下一行的决定的:


override public function set data(value:Object):void {
super.data = value;
this.dataProvider = null;

//在data setter里调用自己写的方法
checkActionSelect();
}

public function checkActionSelect():void{

 currentOwner = new DataGrid();
//获取父容器,这里是DataGrid
currentOwner = owner as DataGrid;
//取出父容器的dataProvider,这里是arraycollection
currentOwnerDataProvider = currentOwner.dataProvider as ArrayCollection;
//取出这行data在父容器的dataprovider里的index
index = currentOwnerDataProvider.getItemIndex(data);

//这里就可以得到下一行的数据了,index + 1就行
var obj:Object = currentOwnerDataProvider.getItemAt(index + 1);

//根据下一行的数据来改变颜色
if(obj.selectedAction == "Approved"){
this.setStyle("chromeColor", "green");
}else if(obj.selectedAction == "Rejected"){
this.setStyle("chromeColor", "red");
}else this.setStyle("chromeColor", "white");
}





另外再提一下validateNow()这个方法,如果想要重画这个itemrenderer,可以override这个方法,当然你也可以用invalidate系列方法去做,但如果你不是对这个itemrenderer掌控那么细致,validateNow()就相对方便,比如:


override public function validateNow():void {
super.validateNow();
this.y += 7;
this.x += 5;
this.backgroundColor = 0xcccccc;
this.wordWrap = true;
this.height = 40;
this.width = 9
this.toolTip = labelStr;
this.setStyle("textAlign", "left");
this.alpha = 0.8;
}


但是也不要过度使用,本来itemrenderer就是一个会拖performance的东西,用的不谨慎甚至会stack overflow.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值