flex dataGrid 中的editable问题 (急,急,急)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.events.FlexEvent;
import mx.collections.ArrayCollection;
[Bindable]
private var dataSource:ArrayCollection=new ArrayCollection([
{id: '一班',visible:false},
{id: 1, name: "test1", age: 12, selected: true,visible:true},
{id: 2, name: "test2", age: 40, selected: false,visible:true},
{id: 3, name: "test3", age: 20, selected: false,visible:true},
{id: '二班',visible:false},
{id: 4, name: "test4", age: 18, selected: true,visible:true},
{id: 5, name: "test5", age: 60, selected: true,visible:true},]);
public function checkBoxClickHandler(event:Event, record:Object):void
{
var checkBox:CheckBox = event.target as CheckBox;
if (checkBox != null) {
record.selected = checkBox.selected;
}
}
]]>
</mx:Script>
<mx:DataGrid id="adGrid"
width="300"
height="253"
fontSize="14"
dataProvider="{dataSource}"
x="262.5"
y="203"
editable="true">
<mx:columns>
<mx:DataGridColumn headerText="序号"
dataField="id"/>
<mx:DataGridColumn headerText="名称"
dataField="name" editable="true"/>
<mx:DataGridColumn headerText="年龄"
dataField="age" editable="true"/>
<mx:DataGridColumn headerText="是否选择"
dataField="selected"
textAlign="center"
editable="false">
<mx:itemRenderer>
<mx:Component>
<mx:HBox verticalAlign="middle" horizontalAlign="center">
<mx:CheckBox label="是" visible="{data.visible}"
selected="{data.selected}"
click="outerDocument.checkBoxClickHandler(event, data)"/>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>
代码如上 :怎么实现第一行和第五行不可编辑,其他行科技编辑,谢谢.
[解决办法]
监听表格itemEditBeginHandler事件,不满足条件的终止event.stopImmediatePropagation();
这样就达到了不可编辑的效果。
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.events.DataGridEvent; import mx.events.AdvancedDataGridEvent; import mx.controls.Alert; import mx.controls.CheckBox; import mx.events.FlexEvent; import mx.collections.ArrayCollection; [Bindable] private var dataSource:ArrayCollection=new ArrayCollection([{id: '一班', visible: false}, {id: 1, name: "test1", age: 12, selected: true, visible: true}, {id: 2, name: "test2", age: 40, selected: false, visible: true}, {id: 3, name: "test3", age: 20, selected: false, visible: true}, {id: '二班', visible: false}, {id: 4, name: "test4", age: 18, selected: true, visible: true}, {id: 5, name: "test5", age: 60, selected: true, visible: true},]); public function checkBoxClickHandler(event:Event, record:Object):void { var checkBox:CheckBox=event.target as CheckBox; if (checkBox != null) { record.selected=checkBox.selected; } } private function itemEditBeginHandler(event:DataGridEvent):void { var rowIndex:int = event.rowIndex; var record:Object = dataSource.getItemAt(rowIndex); if (record == null || !record.visible) { event.stopImmediatePropagation(); } } ]]> </mx:Script> <mx:DataGrid id="adGrid" width="300" height="253" fontSize="14" dataProvider="{dataSource}" itemEditBegin="itemEditBeginHandler(event)" x="262.5" y="203" editable="true"> <mx:columns> <mx:DataGridColumn headerText="序号" dataField="id"/> <mx:DataGridColumn headerText="名称" dataField="name" editable="true"/> <mx:DataGridColumn headerText="年龄" dataField="age" editable="true"/> <mx:DataGridColumn headerText="是否选择" dataField="selected" textAlign="center" editable="false"> <mx:itemRenderer> <mx:Component> <mx:HBox verticalAlign="middle" horizontalAlign="center"> <mx:CheckBox label="是" visible="{data.visible}" selected="{data.selected}" click="outerDocument.checkBoxClickHandler(event, data)"/> </mx:HBox> </mx:Component> </mx:itemRenderer> </mx:DataGridColumn> </mx:columns> </mx:DataGrid></mx:Application>