这个页面的最终效果如果所示:
当CheckBox没有被选中时第二列不能被编辑,第三列可以被编辑。
当CheckBox被选中时,第二列可以被编辑,第三列不能被编辑。
今天做了个在Flex中使用DataGrid,并且DataGrid里面嵌套CheckBox。最终实现全选,也可以实现都选择了那几个记录。先把概况和代码分享下。
概况1:建立提供数据的ArrayCollection,可以直接在MXML里面创建,也可以引入XML文件,我用的后者。HttpService里面的URL是该文件所在的路径。
概况2:在数据源里面加入一个额外的列,比如:selected,里面的值为布尔型,跟CheckBox绑定,可以循环遍历以确定都有哪一记录被选中。
下面直接上代码:
<?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"
creationComplete="showDataGrid.send()"
minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var employeeList:ArrayCollection = new ArrayCollection();
private var checkbox:CheckBox;
protected function showDataGrid_resultHandler(event:ResultEvent):void
{
employeeList = showDataGrid.lastResult.employees.employee;
}
protected function showDataGrid_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
public function checkbox_clickHandler(event:Event):void
{
dataGrid.dataProvider = employeeList;
}
public function selectedAllorNot(event:MouseEvent):void{
checkbox = event.currentTarget as CheckBox;
for each(var item:Object in employeeList){
item.selected = checkbox.selected;
}
dataGrid.dataProvider = employeeList;
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService id="showDataGrid" url="employeeList/employees.xml" result="showDataGrid_resultHandler(event)"
fault="showDataGrid_faultHandler(event)"/>
<mx:DateFormatter id="dateFormatter" formatString="DD-MM-YY"/>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Panel id="showData" horizontalCenter="0" verticalCenter="0" title="Show The DataGrid">
<mx:DataGrid id="dataGrid" dataProvider="{employeeList}">
<mx:columns>
<mx:DataGridColumn width="200" editable="false">
<mx:headerRenderer>
<fx:Component>
<mx:CheckBox label="Selected/Unselected All" textAlign="center" click="outerDocument.selectedAllorNot(event)"/>
</fx:Component>
</mx:headerRenderer>
<mx:itemRenderer>
<fx:Component>
<mx:CheckBox textAlign="center" selected="@{data.selected}" click="outerDocument.checkbox_clickHandler(event)">
</mx:CheckBox>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Employee Name" width="105">
<mx:itemRenderer>
<fx:Component>
<mx:TextInput text="{data.firstName} {data.lastName}" editable="{data.selected}" borderVisible="false" visible="false" />
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Hire Date">
<mx:itemRenderer>
<fx:Component>
<mx:TextInput text="{data.hireDate}" editable="{!data.selected}" borderVisible="false" visible="false"/>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</s:Panel>
</s:Application>
实现CheckBox居中的代码:
package utils
{
import flash.display.DisplayObject;
import mx.controls.CheckBox;
import flash.text.TextField;
public class CenterCheckBox extends CheckBox
{
// 居中展现
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
var n:int = numChildren;
for(var i:int = 0; i < n; i++)
{
var c:DisplayObject = getChildAt(i);
if( !(c is TextField))
{
c.x = Math.round((unscaledWidth - c.width) / 2);
c.y = Math.round((unscaledHeight - c.height) /2 );
}
}
}
}
}
实现圆形布局的代码:
package customLayout
{
import mx.core.ILayoutElement;
import spark.layouts.supportClasses.LayoutBase;
public class CircleLayout extends LayoutBase
{
public function CircleLayout()
{
super();
}
override public function updateDisplayList(width:Number, height:Number):void{
super.updateDisplayList(width, height);
if (!target)
return;
var layoutElement:ILayoutElement;
var count:uint = target.numElements;
var angle : Number = 360/count;
var radius : Number = Math.min( target.width/2, target.height/2 ) - 25;
var width2 : Number = target.width/2;
var height2 : Number = target.height/2;
for (var i:int = 0; i < count; i++)
{
layoutElement = target.getElementAt(i);
if (!layoutElement || !layoutElement.includeInLayout)
continue;
var radAngle : Number = (angle * i) * (Math.PI / 180) ;
var _x : Number = Math.sin( radAngle );
var _y : Number = - Math.cos( radAngle );
layoutElement.setLayoutBoundsPosition( width2 + (_x * radius) - 25, height2 + (_y * radius) - 10 );
}
}
}
}
分享到:
相关推荐
成功的实现了,datagrid中嵌套checkbox实现全选的功能。分享代码供大家分享。项目为flex3。
flex Datagrid checkbox全选 Datagrid
NULL 博文链接:https://taomujian.iteye.com/blog/294748
NULL 博文链接:https://tony0101.iteye.com/blog/579404
在DataGrid中实现了 简单的全选与非全选。
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选、全选。 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellControlTemplate,Binding, OnPropertyChanged等。
主要写了一个全选列,功能包括设置选择框的默认选择状态, 默认可用状态, 全选以及快速获取选中的值,呵呵!
Flex dataGrid 全选、反选 checkBox 不是很完善,但很有借鉴意义
在flex中的datagrid中运用checkbox实现全选和单选
NULL 博文链接:https://dazhi4java.iteye.com/blog/1849623
winform DataGridView表头带CheckBox全选;winform DataGridView表头带CheckBox全选
Datagridview控件没有全选或取消全选的复选框,本资源用VB2015通过扩展DataGridViewColumnHeaderCell类,在列头绘制一个复选框checkbox控件,通过定义checkbox鼠标单击事件来实现行的全选或取消全选。checkbox还可以...
Flex_全选或取消DataGrid中的checkbox, 解决了, flex的checkbox的问题
其中包含一个三种状态的checkbox,DataGrid代码中引用了 带多选框的DataGrid(AdvancedDataGrid) 表头有全选CheckBox,三种状态:全选、部分选择、无选择 如果改变目录结构,需要在CustomCheckBox.mxml中更改inner....
最经典的datagrid全选/反选控件,自定义渲染类,带有使用说明。如果不明白可随时联系
实现了向dataGrid中添加checkBox的功能,可以支持多选,单选,全选。
NULL 博文链接:https://yunzhongxia.iteye.com/blog/652902
最近懒 想在网上找个DataGrid ColumnHeader里面添加CheckBox全选,但是网上没找到,于是就自己写了一个这样的样式,具体功能实现你们就自己去写吧。 分享给大家。 有什么问题可以直接加WPF/Silverlight 找晴天就...
WPF 实现CheckBox全选、反选功能,界面简单实用,功能代码可以很好地套用到自己的项目,希望对有需要的小伙伴有帮助。