datagrid 嵌套单选按钮互斥方法

本文介绍了一种解决DataGrid内单选按钮互斥问题的方法。通过自定义itemRenderer并利用RadioButton组件,实现了单选按钮的功能。同时,记录了点击事件处理逻辑,确保每次仅有一个单选按钮被选中。

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

在一次使用datagrid的时候,嵌入了单选按钮组件:



 突然发现单选按钮的组不能用了,互斥不起了,后来想了这样一种写法,其实也没有什么技术含量的,

 这样就解决了,但是突然发现在dataGrid里面放单选按钮本就是一个没有意义的事情。呵呵。 还是分享下过程:

<?xml version="1.0"?>
<!-- DataGrid control example. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:XMLList id="employees">
        <employee>
            <name>Christina Coenraets</name>
            <phone>555-219-2270</phone>
            <email>ccoenraets@fictitious.com</email>
            <active>true</active>
        </employee>
        <employee>
            <name>Joanne Wall</name>
            <phone>555-219-2012</phone>
            <email>jwall@fictitious.com</email>
            <active>true</active>
        </employee>
        <employee>
            <name>Maurice Smith</name>
            <phone>555-219-2012</phone>
            <email>maurice@fictitious.com</email>
            <active>false</active>
        </employee>
        <employee>
            <name>Mary Jones</name>
            <phone>555-219-2000</phone>
            <email>mjones@fictitious.com</email>
            <active>true</active>
        </employee>
    </mx:XMLList>
	<mx:Script>
		<![CDATA[
			
			private var lastRadioButton:Object = new Object();
			
			public function  rdoHandler(data:Object,obj:Object):void{
				//将上一次点中的按钮状态设置为未点中
				lastRadioButton.selected = false;
				//通过对象导航到当前点击的单选按钮
				//更新最后一次的按钮为当前选中按钮
				lastRadioButton = obj.rdo;
				var temp:String = "姓名:";
				temp +=data.name;
				temp +="\remail:";
				temp +=data.phone;
				temp +="\r电话:";
				temp +=data.email;
				
				personContent.text = temp;
				
			}
		]]>
	</mx:Script>
    <mx:Panel height="360" width="100%" 
        paddingTop="10" paddingLeft="10" paddingRight="10" fontSize="12">

        <mx:Label width="100%" color="blue"
            text="单选按钮互斥问题"/>

        <mx:DataGrid id="dg" width="100%" height="175" rowCount="5" dataProvider="{employees}">
            <mx:columns>
            	<mx:DataGridColumn>
            		<mx:itemRenderer>
            			<mx:Component>
            				<!--这里随便使用的一个组件包含单选按钮,不然不能给按钮id赋值-->
            				<mx:Canvas>
            					<!--使用outerDocument就可以访问该页面的公有方法,data就是当前行的数据封装对象,this封装了改按钮的信息-->
            					<mx:RadioButton id="rdo" click="outerDocument.rdoHandler(data,this);">
            						
            					</mx:RadioButton>
            				</mx:Canvas>
            				
            			</mx:Component>
            		</mx:itemRenderer>
            	</mx:DataGridColumn>
                <mx:DataGridColumn dataField="name" headerText="Name"/>
                <mx:DataGridColumn dataField="phone" headerText="Phone"/>
                <mx:DataGridColumn dataField="email" headerText="Email"/>
            </mx:columns>
        </mx:DataGrid>
        <mx:TextArea id="personContent" width="266" height="84"/>
        
    </mx:Panel>
</mx:Application>        

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值