我正在使用本页顶部代码提供的自定义DataGrid子类:
http://blogs.adobe.com/aharui/2008/02/checkbox_selection_in_datagrid.html
我正在为它提供一个从VB.NET Web服务中提取的两列DataRows的ArrayCollection。其中一列称为“isSelected”,设置为“True”或“False”,它是唯一由复选框表示的列。一切都很顺利,除了我希望代码能够进入并检查和取消选中复选框 - 至少取决于isSelected的值是什么。我的代码可以很好地读取这些复选框中的所有内容,用户可以将它们设置得很好;只是DataGrid组件非常隐蔽并且难以使用,所以我还没有找到一种方法来让代码本身设置特定的复选框来选择或取消选择。
你是怎么做到的?谢谢!
编辑:目前渲染器的代码是:
package
{
import flash.display.DisplayObject;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.ListBase;
/**
* The Renderer.
*/
public class CheckBoxRenderer extends CheckBox
{
private var _data:*;
private var selectedSet:Boolean;
override public function set data(value:Object):void {
var newSelected:*;
//so we don't have to rewrite the label stuff
super.data=value;
_data = value;
if (listData && listData is DataGridListData && DataGridListData(listData).dataField != null)
{
newSelected = _data[DataGridListData(listData).dataField];
}
else if (listData)
{
if (selectedField)
newSelected = _data[selectedField];
}
else
{
newSelected = _data;
}
if (newSelected !== undefined)
{
/*if (newSelected is XMLList) {
newSelected = newSelected[0];
selected = (newSelected == 'true' ? true : false);
} else {
selected = newSelected as Boolean;
}*/
selected = (newSelected == "True" ? true : false);
}
}
public function CheckBoxRenderer()
{
focusEnabled = false;
}
/* override public function set data(value:Object):void
{
super.data = value;
invalidateProperties();
}*/
override protected function commitProperties():void
{
super.commitProperties();
if (owner is ListBase)
selected = ListBase(owner).isItemSelected(data);
}
/* eat keyboard events, the underlying list will handle them */
override protected function keyDownHandler(event:KeyboardEvent):void
{
}
/* eat keyboard events, the underlying list will handle them */
override protected function keyUpHandler(event:KeyboardEvent):void
{
}
/* eat mouse events, the underlying list will handle them */
override protected function clickHandler(event:MouseEvent):void
{
}
/* center the checkbox if we're in a datagrid */
override protected function updateDisplayList(w:Number, h:Number):void
{
super.updateDisplayList(w, h);
if (listData is DataGridListData)
{
var n:int = numChildren;
for (var i:int = 0; i < n; i++)
{
var c:DisplayObject = getChildAt(i);
if (!(c is TextField))
{
c.x = (w - c.width) / 2;
c.y = 0;
}
}
}
}
}
}
复选框的选定字段设置为正确的内容,但是当选中的选项设置为true时,它不会在视觉上显示为检查。我还缺少什么?
答案 0 :(得分:1)
问题不在于DataGrid,而在于Checkbox,但是没有正确实现。有关如何覆盖设置数据的代码,请参阅我的注释here,以便在dataField包含true时,selected将起作用。您需要查看XMLList的路径,了解如何调整它以使您的值为“True”的一些想法。或者您可以将该值转换为布尔值true。
答案 1 :(得分:0)
首先,将arraycollection中包含的对象设置为可绑定。如果要以编程方式选中或取消选中复选框,只需将值直接指定给对象的属性即可。
假设:
<mx:DataGrid id="mygrid" dataProvider="{mycollection}"/>
...
[Bindable]
var mycollection:ArrayCollection = new ArrayCollection([....]);
...
将第一行设置为选中:
var firstItem:MyObject = mycollection.getItemAt(0);
firstItem.isSelected = true; // or false
复选框项呈示器应自动反映列的状态。