0

我正在使用此页面顶部代码提供的自定义 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时,它在视觉上显示。我还缺少什么?

4

2 回答 2

1

问题不在于 DataGrid,而在于未正确实现的 Checkbox。有关如何覆盖设置数据的代码,请参阅我的评论,以便如果 dataField 包含 true,则 selected 将起作用。您需要查看 XMLList 的路径,以了解如何调整它以实现您的“True”值。或者您可以将该值转换为布尔值 true。

于 2012-08-09T00:29:20.420 回答
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

复选框项目渲染器应自动反映列的状态。

于 2012-08-09T01:24:18.620 回答