使用制表符排序防止默认混乱 - AdvancedDataGrid itemeditor

时间:2010-08-08 06:38:27

标签: flex advanceddatagrid

结帐以下示例代码

<?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.CheckBox;
        import mx.events.CollectionEventKind;
        import mx.events.CollectionEvent;
        import mx.events.DataGridEventReason;
        import mx.events.AdvancedDataGridEvent;
        private function calculateCommission(evt:AdvancedDataGridEvent):void
        {
            if(evt.reason == DataGridEventReason.CANCELLED)
                return;
            var dtIndex:int = evt.currentTarget.selectedIndex;
        var dtField:String = evt.dataField;
        var gross:*, commPer:*, comm:*, net:*;

            var dataGross:* = evt.itemRenderer.data.gross;
            var dataCommPer:* = evt.itemRenderer.data.commPer;
            var dataComm:* = evt.itemRenderer.data.comm;
            var dataNet:* = evt.itemRenderer.data.net;
            switch(dtField)
            {
                case "gross":
                    trace("gross column edited.");
                    gross = evt.currentTarget.itemEditorInstance.text;
                    if( (dataCommPer != null) && (dataCommPer!="") )
                    {
                        comm = (gross * dataCommPer) / 100;
                        net = gross - comm;
                    }else if( (dataComm != null) && (dataComm!=""))
                    {
                        commPer = (dataComm * 100) / gross;
                        net = gross - dataComm;
                    }else{
                        net = gross;
                    }
                    break;
                case "commPer":
                    trace("comm % column edited.");
                    commPer = evt.currentTarget.itemEditorInstance.text;
                    if( (dataGross != null) && (dataGross!="") )
                    {
                        comm = dataGross * (commPer / 100);
                        net = dataGross - comm;
                    }
                    break;
                case "comm":
                    trace("comm column edited.");
                    comm = evt.currentTarget.itemEditorInstance.text;
                    if( (dataGross != null) && (dataGross!="") )
                    {
                        commPer = (100 * comm) / dataGross;
                        net = dataGross - comm;
                    }
                    break;
                case "net":
                    trace("net column edited.");
                    net = evt.currentTarget.itemEditorInstance.text;
                    break;
            }

            var item:Object = evt.currentTarget.dataProvider.getItemAt(dtIndex);

            if(gross!=null)
                item.gross = gross;
            if(commPer!=null)
                item.commPer = commPer;
            if(comm!=null)
                item.comm = comm;
            if(net!=null)
                item.net = net;
            //evt.currentTarget.dataProvider.itemUpdated(item);

            var colEvent:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE,true,false, CollectionEventKind.UPDATE);
            colEvent.items = [item];
            colEvent.location =  dtIndex;
            (evt.currentTarget.dataProvider as ArrayCollection).dispatchEvent(colEvent);
        }

        private function preventEditing(evt:AdvancedDataGridEvent):void
        {
            if(evt.reason == DataGridEventReason.CANCELLED)
                return;
            var dtIndex:int = evt.currentTarget.selectedIndex;
            var item:Object = evt.currentTarget.dataProvider.getItemAt(dtIndex);
            if(item.markAsReadOnly && evt.dataField!="markAsReadOnly")
                evt.preventDefault();
        }
    ]]>
</mx:Script>
<mx:ArrayCollection id="dgArr">
    <mx:Array>
        <mx:Object firstName="Jake" lastName="Tyler" gross="" commPer="" comm="" net="" markAsReadOnly=""/>
        <mx:Object firstName="Ryan" lastName="McCarthy" gross="" commPer="" comm="" net=""  markAsReadOnly=""/>
        <mx:Object firstName="Jill" lastName="Miller" gross="" commPer="" comm="" net=""  markAsReadOnly=""/>
        <mx:Object firstName="John" lastName="Rico" gross="" commPer="" comm="" net=""  markAsReadOnly=""/>
        <mx:Object firstName="Diz" lastName="Watson" gross="" commPer="" comm="" net=""  markAsReadOnly=""/>
        <mx:Object firstName="Lolo" lastName="Hurley" gross="" commPer="" comm="" net=""  markAsReadOnly=""/>
    </mx:Array>
</mx:ArrayCollection>

<mx:AdvancedDataGrid dataProvider="{dgArr}" x="100" y="100" 
    editable="true" rowCount="5" 
    itemEditEnd="calculateCommission(event)" itemEditBeginning="preventEditing(event)">
    <mx:groupedColumns>
        <mx:AdvancedDataGridColumn headerText="Read Only" dataField="markAsReadOnly" headerWordWrap="true" editorDataField="selected" editable="true" itemRenderer="mx.controls.CheckBox" rendererIsEditor="true"/>
        <mx:AdvancedDataGridColumn headerText="First Name" dataField="firstName" editable="false"/>
        <mx:AdvancedDataGridColumn headerText="Last Name" dataField="lastName" editable="false"/>
        <mx:AdvancedDataGridColumnGroup headerText="Commission Calculation">
            <mx:AdvancedDataGridColumn dataField="gross" headerText="Gross" itemRenderer="mx.controls.TextInput" rendererIsEditor="true"/>
            <mx:AdvancedDataGridColumn dataField="commPer" headerText="Comm %" itemRenderer="mx.controls.TextInput" rendererIsEditor="true"/>
            <mx:AdvancedDataGridColumn dataField="comm" headerText="Comm" itemRenderer="mx.controls.TextInput" rendererIsEditor="true"/>
            <mx:AdvancedDataGridColumn dataField="net" headerText="Net" itemRenderer="mx.controls.TextInput" rendererIsEditor="true"/>
        </mx:AdvancedDataGridColumnGroup>
    </mx:groupedColumns>
</mx:AdvancedDataGrid>
</mx:Application>

如果您在总列中输入一些值,然后在comm%中输入,则会计算佣金,然后计算净额。一切都运行良好,但是当您选中第一列中的复选框并尝试导航时,选项卡无法对该特定行起作用。它在两个方向都不起作用。

如果我删除了evt.preventDefault,那么tabing工作正常,但它完全无法将行标记为readonly。任何人都可以帮助我获得一种方法来将行标记为只读,但控制焦点也应该正常工作。

2 个答案:

答案 0 :(得分:1)

尝试在itemEditBeginning事件中操作datagrid的editedItemPositiong,如下所示:

dg.editedItemPosition = {rowIndex: event.rowIndex, columnIndex: event.columnIndex + 1};

此行将重点放在网格中的下一列。当然,如果要阻止编辑数据网格中的最后一列,则需要调整此解决方案。

答案 1 :(得分:0)

在黑暗中刺伤..标签命令是否需要手动更新?