在spark datagrid中禁用几行

时间:2011-09-01 09:41:53

标签: actionscript-3 flex flex4.5 flex-spark

在spark datagrid中是否有一种方法可以通过编程方式禁用某些行,在flex 3中,可以使用函数mouseEventToItemRenderer以这种方式完成:

override protected function mouseEventToItemRenderer (
            event: MouseEvent): IListItemRenderer {
    var listItem: IListItemRenderer;// = super.mouseEventToItemRenderer(event);
    if (_disableFlag) 
    {
        if (listItem) 
        {
            if (listItem.data) 
            {
                if (disabledRow(listItem.data)) 
                {
                    return null;
                }
            }
        }
    }
    return listItem;
}

然后我实现函数disabledRow以返回true或false,具体取决于某些条件,条件将指定是否将呈现所选项目。在spark datagrid中有没有办法做同样的事情?

2 个答案:

答案 0 :(得分:1)

我认为spark datagrid支持gridrollover事件。在那之下我们可以得到

itemrender我认为这可能适合你

如需进一步参考,请参阅Documentation

答案 1 :(得分:0)

好的,这就是我的做法

第一步

我创建了类SelectionIndicator,HoverIndicator和CaretIndicator,就像我们在datagrid皮肤中找到的皮肤部分一样,所以皮肤部分是caretIndicator:

<!--- @private -->        
<fx:Component id="caretIndicator">
    <s:Rect implements="spark.components.gridClasses.IGridVisualElement">
        <fx:Script>
            <![CDATA[
                import spark.components.DataGrid;
                import spark.components.Grid;

                /**
                 * @private
                 */
                public function prepareGridVisualElement(grid:Grid, rowIndex:int, columnIndex:int):void
                {
                    const dataGrid:DataGrid = grid.dataGrid;
                    if (!dataGrid)
                        return;

                    const color:uint = dataGrid.getStyle("caretColor");
                    caretIndicatorFill.color = color;
                }
            ]]>
        </fx:Script>

        <s:stroke>
            <!--- @private -->
            <s:SolidColorStroke id="caretIndicatorFill" color="0x0167FF" weight="1"/>
        </s:stroke>
   </s:Rect>
</fx:Component>

变为

package com.tn.zuro.components
{
    import mx.graphics.SolidColorStroke;

    import spark.components.DataGrid;
    import spark.components.Grid;
    import spark.components.gridClasses.IGridVisualElement;
    import spark.primitives.Rect;

    public class CaretIndicator extends Rect implements IGridVisualElement
    {
        private var caretIndicatorFill:SolidColorStroke ;

        public function CaretIndicator()
        {
            super();
            caretIndicatorFill = new SolidColorStroke();
            this.stroke = caretIndicatorFill;
        }

        public function prepareGridVisualElement(grid:Grid, rowIndex:int, columnIndex:int):void
        {
            const dataGrid:DataGrid = grid.dataGrid;
            if (!dataGrid)
                return;

            const color:uint = dataGrid.getStyle("caretColor");
            caretIndicatorFill.color = color;
        }
    }    
}

对于SelectionIndicator和HoverIndicator也是如此,只是不要忘记使用SolidColor而不是solidColorStroke和属性fill或Rect而不是属性笔触。

第二步

在个性化数据网格中,我为事件gridClick,gridRollOver和gridMouseDown添加了事件监听器:

this.addEventListener(GridEvent.GRID_CLICK, gridClickHandler);
this.addEventListener(GridEvent.GRID_ROLL_OVER, gridEventHandler);
this.addEventListener(GridEvent.GRID_MOUSE_DOWN, mouse_down_handler);

以下是事件监听器:

protected function mouse_down_handler(event:GridEvent):void
{
    if (_disableFlag)
    {
        if (event.item)
        {
            if (disabledRow(event.item))
            {
                event.grid.caretIndicator = null;
                event.grid.selectionIndicator = null;
            }
        }
    }
}

protected function gridClickHandler(event:GridEvent):void
{
    if (_disableFlag)
    {
        if (event.item)
        {
            if (!disabledRow(event.item))
            {
                event.grid.selectionIndicator = new ClassFactory(SelectionIndicator);
                event.grid.caretIndicator = new ClassFactory(CaretIndicator);
            }
        }
    }
}

protected function gridEventHandler(event:GridEvent):void
{
    if (_disableFlag)
    {
        if (event.item)
        {
            if(disabledRow(event.item))
            {   
                event.grid.hoverIndicator = null;
            }
            else
            {
                event.grid.hoverIndicator = new ClassFactory(HoverIndicator);
            }
        }
    }
}

您可能已经猜到_disableFlag和disabledRow分别是布尔值和函数。现在最后一步是最简单的:

第三步

当您使用个性化dataGrid时,如果要禁用列,则将_disableFlag设置为true并实现disabledRow,如果满足条件则返回true,否则返回false

<custom:NinjaGrid id="ninja"
                 _disableFlag=true
                  creationComplete="ninja_trainingCompleteHandler(event)"/>

protected function ninja_trainingCompleteHandler(event:FlexEvent):void
{
    ninja.disabledRow = beNinja;
}
private function beNinja(ninja:Object):Boolean
{
    if (ninja.knowHowToWalkOnWater == true)
        return true;
    return false;
}

此解决方案不适用于多行选择模式,我找到了多行选择模式的另一种解决方案。


第二种解决方案

在此解决方案中,我仅使用HoverIndicator组件,rollover事件的事件侦听器将保持不变。我不再使用事件监听器来处理click事件,而在mouse_down_handler函数中,我现在有了这段代码:

protected function mouse_down_handler(event:GridEvent):void
{
    if(_disableFlag)
    {
        if (event.item)
        {
            if (!disabledRow(event.item))
            {
                if (!event.ctrlKey)
                {
                    tempItem = event.item;
                    tempSelecteditems = new Vector.<Object>();
                    tempSelecteditems.push(tempItem);
                }
                else
                {
                    if (tempSelecteditems.indexOf(event.item) < 0)
                    {
                        tempSelecteditems.push(event.item);
                    }
                    else
                    {
                        tempSelecteditems[tempSelecteditems.indexOf(event.item)] = null;
                    }
                }

            } 
            else
            {
                if (!event.ctrlKey)
                {
                    selectedItem = tempItem;
                    tempSelecteditems = new Vector.<Object>();
                } 
                else
                {
                    if (tempSelecteditems.length)
                    {
                        selectedItems = tempSelecteditems;
                    }
                    else
                    {
                        selectedItem = tempItem;
                    }
                }
            }
        }
    }
}